2

我是 MsSql 的新手,我不确定这是否可以完成,但我想在我想要使用当前流程之前我会先问一下..

我需要创建一个脚本来遍历数据库中的所有表并删除 CorporationId = "xxx" 的行。有一些表没有此列,但在我的约 50 个表中,只有 1 或 2 个没有。

我可以用这个单独删除表中的记录:

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..

但是,为每个表创建一个变得乏味。

当然,有些表是有关系的。

有没有一种简单的方法可以做到这一点,还是我需要为每个表手动完成?

更新

我尝试过的大多数选项都遇到了关系问题并给了我一个错误。

4

4 回答 4

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
于 2013-02-05T20:15:14.033 回答
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
于 2013-02-05T20:18:07.217 回答
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语句执行每个结果。

于 2013-02-05T20:20:01.550 回答
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
于 2013-02-05T20:12:15.680 回答