Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Update table field based on results from a query

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.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

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.
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

Avatar of Aleks

ASKER

ugh .. its too early in the morning  :$  thx