Solved

Update table field based on results from a query

Posted on 2016-08-14
4
44 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

20 Experts available now in Live!

Get 1:1 Help Now