码迷,mamicode.com
首页 > 其他好文 > 详细

暴力搜索数据

时间:2016-07-31 11:35:38      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!