zoukankan      html  css  js  c++  java
  • SqlServer查询某数据在某表某列中

    create proc spFind_Column_In_DB

    (

        @type int,--类型:为文字类型、为数值类型

        @str nvarchar(100)--需要搜索的名字

    )

     

     

     

    as

        --创建临时表存放结果

        create table #tbl(PK int identity primary key ,tbl sysname,col sysname)

        declare @tbl nvarchar(300),@col sysname,@sql nvarchar(1000)

        if @type=1 

        begin

            declare curTable cursor fast_forward

            for

                select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id

                where o.type_desc='user_table' and user_type_id in (167,175,231,239,35,99)

        end

        else

        begin

            declare curTable cursor fast_forward

            for

            select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id

                where o.type_desc='user_table' and user_type_id in (56,48,52,59,60,62,106,108,122)

        end

        open curtable

        fetch next from curtable into @tbl,@col

        while @@FETCH_STATUS=0

        begin

            set @sql='if exists (select * from '+@tbl+' where '

            if @type=1

            begin

                set @sql += @col + ' like ''%'+@str +'%'')'

            end

            else

            begin

                set @sql +=@col + ' in ('+@str+'))'

            end

     

            set @sql += ' INSERT #TBL(tbl,col) VALUES('''+@tbl+''','''+@col+''')'

            --print @sql

            exec (@sql)

            fetch next from curtable into @tbl,@col

        end

        close curtable 

        deallocate curtable

        select * from #tbl

     

     

     

     

    exec spFind_Column_In_DB 1,'查询条件'

  • 相关阅读:
    Max Function
    动态语句语法:EXEC\SP_EXECUTESQL语法(转载)
    [转帖]在SQL SERVER中实现RSA加密算法
    [转]C#实现QQ接口软件QQ的HTTP接口协议探究
    用一条sql语句删除表中所相同的记录
    SQL Server存储过程 对数组参数的循环处理
    在WinDBG中, 使用.shell命令来搜索字符串
    SharePoint提供的一些javascript函数
    IP协议里的Time To Live(TTL)
    SharePoint Application Pool的推荐设置
  • 原文地址:https://www.cnblogs.com/momjs/p/8385739.html
Copyright © 2011-2022 走看看