Sql 使用特定值从多个表中删除记录
今天,, 我想从SQLServer中的几个表中删除所有测试记录。 以下是我想要实现的目标Sql 使用特定值从多个表中删除记录,sql,sql-server,sql-delete,Sql,Sql Server,Sql Delete,今天,, 我想从SQLServer中的几个表中删除所有测试记录。 以下是我想要实现的目标 Select ID from sourceTable where acctType='S' and acctroot<>0 现在这些是我需要清理的桌子 delete from tmpA where ID=1 delete from tmpB where acctID=1 delete from tmpC where userID = 1 delete from tmpD where sID=1
Select ID from sourceTable where acctType='S' and acctroot<>0
现在这些是我需要清理的桌子
delete from tmpA where ID=1
delete from tmpB where acctID=1
delete from tmpC where userID = 1
delete from tmpD where sID=1
.
.
.
delete from tmpA where ID=2
delete from tmpB where acctID=2
delete from tmpC where userID =2
delete from tmpD where sID=2
我可以循环源表并提取ID,然后从表中删除吗?我知道我可以使用连接,但我想使用While循环来实现这一点。你不需要While循环,你可以像下面那样使用WHERE EXISTS
您还需要对其他表执行相同的操作。您不需要WHILE循环,您可以使用WHERE EXISTS执行以下操作
您还需要对其他表执行相同的操作。就这么简单
... WHERE ID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
像这样:
delete from tmpA where ID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
delete from tmpB where acctID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
delete from tmpC where userID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
delete from tmpD where sID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
就这么简单
... WHERE ID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
像这样:
delete from tmpA where ID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
delete from tmpB where acctID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
delete from tmpC where userID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
delete from tmpD where sID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
你不需要while循环
delete from tmpA where ID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
你不需要while循环
delete from tmpA where ID IN (Select ID from sourceTable where acctType='S' and acctroot<>0)
确保您的ID存在于sourcetable上的另一种方法是使用内部联接
DELETE T
FROM tmpA T
INNER JOIN sourcetable S ON S.id = T.id AND S.accttype = 'S' AND S.acctroot <> 0
确保您的ID存在于sourcetable上的另一种方法是使用内部联接
DELETE T
FROM tmpA T
INNER JOIN sourcetable S ON S.id = T.id AND S.accttype = 'S' AND S.acctroot <> 0
试试这个
Declare @Temp_Table Table (Id int identity(1,1),Table_Name varchar(30),Column_Name varchar(30))
insert into @Temp_Table
select 'tmpA','ID'
union all
select 'tmpB','acctID'
union all
select 'tmpC','userID'
union all
select 'tmpD','sID'
Declare @Counter int=1
,@Tot_Count int=0
,@Table_Name varchar(30)=''
,@Column_Name varchar(30)=''
select @Tot_Count=count(1)
from @Temp_Table
While @Tot_Count >= @Counter
Begin
Select @Table_Name=Table_Name
,@Column_Name=Column_Name
From @Temp_Table
Where Id=@Counter
Exec
(
'
Delete A
From '+@Table_Name+' A
inner join sourceTable B
on A.'+@Column_Name+'=B.ID
where B.acctType=''S''
and B.acctroot<>0
'
)
Set @Counter+=1
End
试试这个
Declare @Temp_Table Table (Id int identity(1,1),Table_Name varchar(30),Column_Name varchar(30))
insert into @Temp_Table
select 'tmpA','ID'
union all
select 'tmpB','acctID'
union all
select 'tmpC','userID'
union all
select 'tmpD','sID'
Declare @Counter int=1
,@Tot_Count int=0
,@Table_Name varchar(30)=''
,@Column_Name varchar(30)=''
select @Tot_Count=count(1)
from @Temp_Table
While @Tot_Count >= @Counter
Begin
Select @Table_Name=Table_Name
,@Column_Name=Column_Name
From @Temp_Table
Where Id=@Counter
Exec
(
'
Delete A
From '+@Table_Name+' A
inner join sourceTable B
on A.'+@Column_Name+'=B.ID
where B.acctType=''S''
and B.acctroot<>0
'
)
Set @Counter+=1
End
您总是可以编写t-sql或使用动态查询。我想知道为什么您说您不想要带有联接的解决方案,而接受带有EXISTS?的解决方案?。SQL Server引擎也是如此。您始终可以编写t-SQL或使用动态查询。我想知道您为什么说不希望使用联接的解决方案,而接受使用EXISTS?的解决方案?。对于SQL Server引擎,它们是相同的。对不起,问这个问题:Doops对不起,问这个问题:D