标签:
DECLARE @guid NVARCHAR(MAX) SET @guid = ‘CF789C0E-B844-E511-8100-00155D0A6F04‘ DECLARE @index INT , @count INT SELECT IDENTITY( INT,1,1 ) AS id , objects.name AS tablename , columns.name AS ColumnName , CONVERT(INT, NULL) AS existsdata INTO #columns FROM sys.columns LEFT JOIN sys.objects ON objects.object_id = columns.object_id WHERE system_type_id = 36 AND type = ‘U‘ --AND objects.name LIKE ‘y_%‘ ORDER BY objects.name SELECT @index = 1 , @count = @@ROWCOUNT WHILE @index <= @count BEGIN DECLARE @sql NVARCHAR(MAX) SELECT @sql = ‘if exists(select 1 from ‘ + tablename + ‘ where ‘ + ColumnName + ‘ = ‘‘‘ + @guid + ‘‘‘) begin update #columns set existsdata = 1 where id = ‘ + CONVERT(NVARCHAR(MAX), @index) + ‘ end‘ FROM #columns WHERE id = @index --EXEC @sql EXEC (@sql) SET @index = @index + 1 END SELECT * FROM #columns WHERE existsdata = 1 DROP TABLE #columns
DECLARE @guid NVARCHAR(MAX)SET @guid = ‘CF789C0E-B844-E511-8100-00155D0A6F04‘
DECLARE @index INT , @count INT
SELECT IDENTITY( INT,1,1 ) AS id , objects.name AS tablename , columns.name AS ColumnName , CONVERT(INT, NULL) AS existsdataINTO #columnsFROM sys.columns LEFT JOIN sys.objects ON objects.object_id = columns.object_idWHERE system_type_id = 36 AND type = ‘U‘--AND objects.name LIKE ‘y_%‘ORDER BY objects.name
SELECT @index = 1 , @count = @@ROWCOUNTWHILE @index <= @count BEGIN DECLARE @sql NVARCHAR(MAX) SELECT @sql = ‘if exists(select 1 from ‘ + tablename + ‘ where ‘ + ColumnName + ‘ = ‘‘‘ + @guid + ‘‘‘)beginupdate #columns set existsdata = 1 where id = ‘ + CONVERT(NVARCHAR(MAX), @index) + ‘end‘ FROM #columns WHERE id = @index--EXEC @sql EXEC (@sql) SET @index = @index + 1 END
SELECT *FROM #columnsWHERE existsdata = 1
DROP TABLE #columns
标签:
原文地址:http://www.cnblogs.com/KingUp/p/5722454.html