如果我正确理解了这个问题,您可以通过使用 select 获取每个表的键值来做到这一点。然后使用 3 个删除语句
DECLARE @RecIds AS TABLE (sTableName VARCHAR(100), recidTableXRecord INT)
INSERT INTO @RecIds (recidTable1 , recidTableXRecord)
SELECT 'Table1', <Unique Record Id for table 1>
FROM .... ( ) AS TableRecords1
INSERT INTO @RecIds (recidTable2 , recidTableXRecord)
SELECT 'Table2', <Unique Record Id for table 1>
FROM .... ( ) AS TableRecords1
INSERT INTO @RecIds (recidTable3 , recidTableXRecord)
SELECT 'Table3', <Unique Record Id for table 1>
FROM .... ( ) AS TableRecords1
...然后删除
DELETE FROM TABLE1 WHERE recidTable1 IN (SELECT recidTableXRecord FROM @RecIds WHERE sTableName ='TABLE1')
DELETE FROM TABLE2 WHERE recidTable1 IN (SELECT recidTableXRecord FROM @RecIds WHERE sTableName ='TABLE2')
DELETE FROM TABLE3 WHERE recidTable1 IN (SELECT recidTableXRecord FROM @RecIds WHERE sTableName ='TABLE3')
...可能有更好的方法,但这会起作用并且易于维护。建议把所有的都放在一个交易中。