1

私は2つのテーブルを持っています:

  1. usersuser_id, statusカラム付き
  2. changesuser_id, type列のある

から行を削除したいchanges場合type = 5は、次を設定します。

users.status = NULL Where users.user_id = changes.user_id

どうすればいいですか?

4

2 に答える 2

2

これを試して:

-- table variable to store deleted changes
declare @deleted_changes table(user_id int, [type] int);

-- saving info from deleted changes into temp table
delete from changes 
output deleted.user_id, deleted.[type] into @deleted_changes;
-- where [some condition]

-- updating users only if corresponding changes with type = 5 were deleted
update users
set status = null
where user_id in (select user_id from @deleted_changes where [type] = 5);
于 2013-11-10T15:05:21.297 に答える