我正在清理ERP,需要摆脱对未使用用户和用户组的引用。有许多外键约束,因此,我想确保真正摆脱所有痕迹!
我发现了这段整洁的代码花絮,以查找数据库中具有特定列名的所有表,在这种情况下,让我们看一下用户组:
select table_name from information_schema.columns
where column_name = 'GROUP_ID'
通过结果,我可以在40多个表中搜索未使用的ID ...但这是很麻烦的。因此,我想自动执行此操作,并创建一个遍历所有这些表的查询,并删除在
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解决方案...)。理想情况下,我将拥有一个脚本来查询以查找具有特定列名的表,如上所述查询每个表,并向我吐出匹配项列表,然后执行第二个类似脚本以删除行。谁能帮助我指出正确的方向?
最佳答案
好吧,尝试一下,有三个变量;列,colValue和预览。 Column应该是您要检查(Group_ID)是否相等的列,colValue您要查找的值(Unused_Group),并且Preview应该是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
我用代码注释掉了地狱,以帮助您了解它的作用。祝好运!
关于sql - SQL:查询许多具有相同列名但结构不同的表以获取特定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14820540/