1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| DECLARE @Column NVARCHAR(255) = '12345';--把12345替换成完整的列数据 DECLARE @DeletedIDs TABLE (id INT); BEGIN TRANSACTION; BEGIN TRY DELETE FROM TABLE_A OUTPUT deleted.id INTO @DeletedIDs WHERE Column = @Column; DELETE FROM TABLE_B WHERE pid IN (SELECT id FROM @DeletedIDs); COMMIT TRANSACTION; END TRY BEGIN CATCH ROLLBACK TRANSACTION; DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); END CATCH;
|