Solved

Update table field based on results from a query

Posted on 2016-08-14
4
48 Views
Last Modified: 2016-08-14
I am using sql 2008. I have a table with a field called 'prioritydate'
I need to update it with the value from the query below:

SELECT  a.SDID ,
        a.CatalogId ,
        a.UserId ,
        a.Expiration ,
        a.Docurrent ,
        a.Preferencetype 
FROM    BlueDotAdditionalDocs a
WHERE   a.docsection = 'contact'
        AND a.Docurrent = 1
        AND CatalogId = 3

Open in new window


The query above will return one record per 'userid'
I need to take the 'expiration' and copy it to table 'users'  where users.userid = a.userid   from the above query.


something like
update users
set prioritydate = a.expiration
where users.userid = a.userid

but from the query above :)  ... I hope that makes sense.
Not all records in the users table will have a value from the query, that is fine.
0
Comment
Question by:amucinobluedot
  • 2
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41755422
update u
set prioritydate = a.expiration
from users u join BlueDotAdditionalDocs a
on u.userid = a.userid
where a.docsection = 'contact'
and a.Docurrent = 1
and a.CatalogID = 3

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41755424
Perfect, just one more thing, I want to copy it only if prioritydate has no value.
Would I simply add at the end:

and (u.prioritydate is not null or u.prioritydate <> '')

Is the above correct?  I tried but it still updated the value even though u.prioritydate already had a value.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41755430
The additional condition is reversed. You only update if there is a value.
and (u.prioritydate is null or u.prioritydate = '')

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41755432
ugh .. its too early in the morning  :$  thx
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now