Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Update table field based on results from a query

Posted on 2016-08-14
4
Medium Priority
?
72 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
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:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

722 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