2

SQL Server 2008 R2 で実行する複数の削除ステートメントがあります。

DELETE FROM A WHERE A_id in (SELECT B_id FROM B WHERE B_name = 'Target')
DELETE FROM B WHERE B_id in (SELECT B_id FROM B WHERE B_name = 'Target')
DELETE FROM C WHERE C_id in (SELECT B_id FROM B WHERE B_name = 'Target')
DELETE FROM D WHERE D_id in (SELECT B_id FROM B WHERE B_name = 'Target')

変数を使用して繰り返しサブクエリを置き換える方法はありますか? サブクエリまたはその結果を保持する変数の型はありますか?

ありがとうございました

4

1 に答える 1

0

削除する ID を保持するテーブル変数...

Declare @BIDs Table
(
  B_Id Int Not Null
)

Insert Into @BIDs (B_Id)
SELECT B_Id FROM B WHERE B_Name = 'Target'

DELETE FROM A WHERE A_id in (SELECT B_id FROM @BIDs)
DELETE FROM B WHERE B_id in (SELECT B_id FROM @BIDs)
DELETE FROM C WHERE C_id in (SELECT B_id FROM @BIDs)
DELETE FROM D WHERE D_id in (SELECT B_id FROM @BIDs)
于 2015-08-18T04:16:50.123 に答える