Solved

Update table field based on results from a query

Posted on 2016-08-14
4
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 70

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 70

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
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:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

734 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