zoukankan      html  css  js  c++  java
  • 查询数据库中某个表中某一列是否包含某个值的proc(sqlserver)

    if object_id('spFind_Column_In_DB') is not null drop proc spFind_Column_In_DB
    go

    --date:2013-5-10
    --author:dyq
    --decs:搜索数据库中是否存在输入的值
    --results:tablename,columname
    create proc spFind_Column_In_DB
    (
    @type int,--类型:1为文字类型、2为数值类型
    @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
    drop table #tbl

    exec spFind_Column_In_DB 1,'需要查询包含的值'

  • 相关阅读:
    期望
    更改开机默认操作系统及等待时间修改
    Python排序
    Python IDLE入门 + Python 电子书
    Python基础教程——1基础知识
    Java:谈谈protected访问权限
    三星I9100有时不能收发彩信完美解决!中国移动
    java继承的权限问题
    Python基础教程——2列表和元组
    访问控制和继承(Java)
  • 原文地址:https://www.cnblogs.com/guzhengtao/p/14034666.html
Copyright © 2011-2022 走看看