Aleks
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:
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The additional condition is reversed. You only update if there is a value.
and (u.prioritydate is null or u.prioritydate = '')
ASKER
ugh .. its too early in the morning :$ thx
ASKER
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.