question

mickyd avatar image
mickyd asked

Update query

Hi everyone trying to put together an update query based on this select query. select u.login_name, e.first_name, e.last_name, e.e_mail, u.last_login,u.status from table_user U inner join table_employee e on e.employee2user=u.objid where u.status=1 and last_login <'2010-05-22 00:01:00.000' or last_login ='1753-01-01 00:00:00.000' order by last_login Essentially I want to do is update u.status to 0 where the 2 last login critera is met. Cheers for any advice.
tsqlupdate
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Mrs_Fatherjack avatar image
Mrs_Fatherjack answered
Try this: begin tran go UPDATE table_user SET [STATUS] = 0 WHERE [status] = 1 AND last_login ]
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Pavel Pawlowski avatar image Pavel Pawlowski commented ·
Only a note, is your condition correct? Aren't you missing brackets there? AS the **AND** operator has higher priority than **OR** operator. So it will be processed AS `([status] = 1 AND last_login ]
1 Like 1 ·
mickyd avatar image mickyd commented ·
many thanks for reply get this back , when trying it. Msg 4104, Level 16, State 1, Line 4 The multi-part identifier "u.status" could not be bound.
0 Likes 0 ·
Mrs_Fatherjack avatar image Mrs_Fatherjack commented ·
Sorry, the where statement needs to read: WHERE [status] = 1 AND last_login
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
Whilst @Mrs_Fatherjack is right, I just wanted to add that if the join is important here, then it should also be included in the update - there may be employees in the table_user table that are not in the table_employee table - it's a subtle difference Update U.[Status] = 0 from table_user U inner join table_employee e on e.employee2user=u.objid where u.status=1 and last_login
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image Scot Hauder commented ·
add U SET after update and it should be good to go. You wouldn't want parens around the 2 last_login datetimes as it changes the logic
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.