日期:2014-05-17  浏览次数:20489 次

求助:如何在整库中查询特定的值?
本帖最后由 xiao1682 于 2013-09-10 10:23:26 编辑
现有一数据库,含数百个表,现要在整个库中查询出包含有“100元”的表,或显示出包含此值的表名即可。
数据库 MSSQL SQL语句 游标 动态语句

------解决方案--------------------
我这里怎么就不是全部代码?
你把我代码中alter 改成create,用来创建这个存储过程,然后执行:
exec spFind_Column_In_DB 1,'100元'这还说的不够清楚吗?
第一步:
use 需要查找的数据库名
go
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