我正在努力清理ERP,我需要摆脱对未使用的用户和用户组的引用。有许多外键约束,因此我想确保真正摆脱所有痕迹!
我发现这个整洁的代码来查找我的数据库中具有特定列名的所有表,在这种情况下让我们看一下用户组:
select table_name from information_schema.columns
where column_name = 'GROUP_ID'
结果我可以在40多个表中搜索我未使用的ID ...但这是tedius。所以我想自动执行此操作并创建一个循环遍历所有这些表的查询,并删除Unused_Group
列中找到GROUP_ID
的行。
在删除任何内容之前我想要显示现有数据,所以我开始使用字符串连接来构建这样的东西:
declare @group varchar(50) = 'Unused_Group'
declare @table1 varchar(50) = 'TABLE1'
declare @table2 varchar(50) = 'TABLE2'
declare @tableX varchar(50) = 'TABLEX'
select @query1 = 'SELECT ''' + rtrim(@table1) + ''' as ''Table'', '''
+ rtrim(@group) + ''' = CASE WHEN EXISTS (SELECT GROUP_ID FROM ' + rtrim(@table1)
+ ' WHERE GROUP_ID = ''' + rtrim(@group) + ''') then ''MATCH'' else ''-'' end FROM '
+ rtrim(@table1)
select @query2 = [REPEAT FOR @table2 to @tableX]...
EXEC(@query1 + ' UNION ' + @query2 + ' UNION ' + @queryX)
这给了我结果:
TABLE1 | Match
TABLE2 | -
TABLEX | Match
这适用于我的目的,我可以在不更改任何其他代码的情况下为任何用户组运行它,并且当然可以很容易地从这些相同的表中适应DELETE
,但对于75个左右的表来说是无法管理的我必须在用户和群组之间进行处理。
我遇到了this link on dynamic SQL,这种情绪非常密集,足以让我暂时吓跑......但我认为解决方案可能就在那里。
我非常熟悉JS和其他语言中的FOR()
循环,这对于结构良好的数组来说是一块蛋糕,但显然它在SQL中并不那么简单(我还在学习,但是发现很多关于FOR和GOTO解决方案的负面评论......)。理想情况下,我有一个脚本查询查找具有特定列名的表,查询上面的每个表,然后向我吐出匹配列表,然后执行第二个类似的脚本来删除行。
任何人都可以帮我指出正确的方向吗?
答案 0 :(得分:1)
好的,试试这个,有三个变量;列,colValue和预览。列应该是您正在检查相等的列(Group_ID),colValue您要查找的值(Unused_Group),预览应该是1以查看您将删除的内容,0应该删除它。
Declare @column Nvarchar(256),
@colValue Nvarchar(256),
@preview Bit
Set @column = 'Group_ID'
Set @colValue = 'Unused_Group'
Set @preview = 1 -- 1 = preview; 0 = delete
If Object_ID('tempdb..#tables') Is Not Null Drop Table #tables
Create Table #tables (tID Int, SchemaName Nvarchar(256), TableName Nvarchar(256))
-- Get all the tables with a column named [GROUP_ID]
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 = @column
Select *
From #tables
Declare @SQL Nvarchar(Max),
@schema Nvarchar(256),
@table Nvarchar(256),
@iter Int = 1
-- As long as there are tables to look at keep looping
While Exists (Select 1
From #tables)
Begin
-- Get the next table record to look at
Select @schema = SchemaName,
@table = TableName
From #tables
Where tID = @iter
-- If the table we're going to look at has dependencies on tables we have not
-- yet looked at move it to the end of the line and look at it after we look
-- at it's dependent tables (Handle foreign keys)
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
-- Move the table to the end of the list to retry later
Update t
Set tID = (Select Max(tID) From #tables) + 1
From #tables t
Where tableName = @table
And schemaName = @schema
-- Move on to the next table to look at
Set @iter = @iter + 1
End
Else
Begin
-- Delete the records we don't want anymore
Set @Sql = Case
When @preview = 1
Then 'Select * ' -- If preview is 1 select from table
Else 'Delete t ' -- If preview is not 1 the delete from table
End +
'From [' + @schema + '].[' + @table + '] t
Where ' + @column + ' = ''' + @colValue + ''''
Exec sp_executeSQL @SQL;
-- After we've done the work remove the table from our list
Delete t
From #tables t
Where tableName = @table
And schemaName = @schema
-- Move on to the next table to look at
Set @iter = @iter + 1
End
End
将其转换为存储过程只需将顶部的变量声明更改为sproc创建,这样就可以摆脱...
Declare @column Nvarchar(256),
@colValue Nvarchar(256),
@preview Bit
Set @column = 'Group_ID'
Set @colValue = 'Unused_Group'
Set @preview = 1 -- 1 = preview; 0 = delete
...
并将其替换为......
Create Proc DeleteStuffFromManyTables (@column Nvarchar(256), @colValue Nvarchar(256), @preview Bit = 1)
As
...
你打电话给它......
Exec DeleteStuffFromManyTable 'Group_ID', 'Unused_Group', 1
我评论了代码中的地狱,以帮助您了解它正在做什么;祝你好运!
答案 1 :(得分:0)
您使用INFORMATION_SCHEMA
个对象走在正确的轨道上。在查询编辑器中执行以下操作,它会为包含SELECT
列DELETE
值的表生成GROUP_ID
和'Unused_Group'
语句。
-- build select DML to manually review data that will be deleted
SELECT 'SELECT * FROM [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] WHERE [GROUP_ID] = ''Unused_Group'';'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'GROUP_ID';
-- build delete DML to remove data
SELECT 'DELETE FROM [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] WHERE [GROUP_ID] = ''Unused_Group'';'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'GROUP_ID';
由于这似乎是一次性清理工作,特别是因为您需要在数据被删除之前对其进行检查,所以我认为这不会使这个问题变得更加复杂。
答案 2 :(得分:0)
如果可以,请考虑添加参照完整性并强制执行级联删除。在删除数据之前对数据进行可视化无济于事,但有助于控制孤立的行。