我是MsSql的新手,我不确定是否可以这样做,但我想在我想要当前流程之前先问一下..
我需要创建一个循环遍历数据库中所有表的脚本,并删除其中的CorporationId =“xxx”的行。有几个表没有这个列,但在我的~50个表中,只有1个或两个表没有。
我可以单独删除表格中的记录:
USE MyDatabase
DECLARE @CorporationId UniqueIdentifier
DECLARE @TokenId UniqueIdentifier
DECLARE @CompanyCode nChar(3)
SET @CorporationId = '52D3AEFE-8EBD-4669-8096-4596FE83BB36'
print 'Starting Web.GasOrder'
DELETE FROM Web.GasOrder
WHERE CorporationId = @CorporationId
print 'Starting Web.GasOrderNumber'
DELETE FROM Web.GasOrderNumber
WHERE CorporationId = @CorporationId
etc..
但是为每张桌子制作一张桌子会变得乏味。
当然,某些表格上有关系。
有没有一种简单的方法可以做到这一点,还是我需要为每个表手动完成?
更新
我尝试过的大多数选项会遇到关系问题并给我一个错误。
答案 0 :(得分:6)
sp_MSForEachTable
是一个未记录的存储过程,它将为数据库中的每个表运行一个命令:
USE MyDatabase
DECLARE @CorporationId VARCHAR(50)
SET @CorporationId = '52D3AEFE-8EBD-4669-8096-4596FE83BB36'
DECLARE @Sql VARCHAR(MAX)
SET @Sql = '
IF COL_LENGTH(''?'',''CorporationId'') IS NOT NULL
BEGIN
DELETE FROM Web.?
WHERE CorporationId = ''' + @CorporationId + '''
END
'
EXEC sp_MSForEachTable @Sql
答案 1 :(得分:4)
这是另一个......可以轻松更改为存储过程......
Declare @corpID Nvarchar(256)
Set @corpID = 'xxx'
If Object_ID('tempdb..#tables') Is Not Null Drop Table #tables
Create Table #tables (tID Int, SchemaName Nvarchar(256), TableName Nvarchar(256))
Insert #tables
Select Row_Number() Over (Order By s.name, so.name), s.name, so.name
From sysobjects so
Join sys.schemas s
On so.uid = s.schema_id
Join syscolumns sc
On so.id = sc.id
Where so.xtype = 'u'
And sc.name = 'CorporationId'
Declare @SQL Nvarchar(Max),
@schema Nvarchar(256),
@table Nvarchar(256),
@iter Int = 1
While Exists (Select 1
From #tables)
Begin
Select @schema = SchemaName,
@table = TableName
From #tables
Where tID = @iter
If Exists (Select 1
From sysobjects o
Join sys.schemas s1
On o.uid = s1.schema_id
Join sysforeignkeys fk
On o.id = fk.rkeyid
Join sysobjects o2
On fk.fkeyid = o2.id
Join sys.schemas s2
On o2.uid = s2.schema_id
Join #tables t
On o2.name = t.TableName Collate Database_Default
And s2.name = t.SchemaName Collate Database_Default
Where o.name = @table
And s1.name = @schema)
Begin
Update t
Set tID = (Select Max(tID) From #tables) + 1
From #tables t
Where tableName = @table
And schemaName = @schema
Set @iter = @iter + 1
End
Else
Begin
Set @Sql = 'Delete t
From [' + @schema + '].[' + @table + '] t
Where CorporationId = ''' + @corpID + ''''
Exec sp_executeSQL @SQL;
Delete t
From #tables t
Where tableName = @table
And schemaName = @schema
Set @iter = @iter + 1
End
End
答案 2 :(得分:2)
您可以运行如下查询:
SELECT 'DELETE FROM [' + s.name + '].[' + t.name + '] WHERE CorporationId = ''52D3AEFE-8EBD-4669-8096-4596FE83BB36'''
FROM sys.columns c
inner join sys.tables t
ON t.object_id= c.object_id
inner join sys.schemas s
ON s.schema_id = t.schema_id
where c.name = 'CorporationId'
然后将结果复制并粘贴到新的查询窗口并执行新查询或使用游标迭代结果并使用exec
语句执行每个结果。
答案 3 :(得分:1)
这是证明以下查询的Sql Fiddle
您可以从此查询中获取表格:
SELECT Name, IsChecked = 0
INTO #Tables
FROM sys.Tables
WHERE EXISTS
(
SELECT * FROM sys.columns
WHERE object_id = OBJECT_ID(sys.Tables.Name) AND sys.columns.Name = 'blah'
)
然后,您可以创建动态查询并为找到的表执行
WHILE(SELECT COUNT(*) FROM #Tables WHERE IsChecked = 0) > 0
BEGIN
SELECT TOP 1 @TableName = Name FROM #Tables WHERE IsChecked = 0
SET @DeleteQuery = 'DELETE FROM ' + @TableName + ' WHERE CorporationID = ''' + @CorporationId + ''''
EXECUTE sp_executeSQL @DeleteQuery;
UPDATE #Tables SET IsChecked = 1 WHERE Name = @TableName
END