• SQL Server 查询某个字段值在哪张表的哪个字段


    我要查找值为‘WSCOL1525’的字段。

    declare @cloumns varchar(40)
    declare @tablename varchar(40)
    declare @str varchar(40)
    declare @counts int
    declare @sql nvarchar(2000)
    declare MyCursor Cursor For
    Select a.name as Columns, b.name as TableName from syscolumns a,sysobjects b,systypes c
    where a.id = b.id
    and b.type = 'U'
    and a.xtype=c.xtype
    and c.name like '%char%'
    set @str='

    declare @cloumns varchar(40)
    declare @tablename varchar(40)
    declare @str varchar(40)
    declare @counts int
    declare @sql nvarchar(2000)
    declare MyCursor Cursor For
    Select a.name as Columns, b.name as TableName from syscolumns a,sysobjects b,systypes c
    where a.id = b.id
    and b.type = 'U'
    and a.xtype=c.xtype
    and c.name like '%char%'
    set @str='WSCOL1525'
    Open MyCursor
    Fetch next From MyCursor Into @cloumns,@tablename
    While(@@Fetch_Status = 0)
    Begin
    set @sql='select @tmp_counts=count(*) from ' +@tablename+ ' where ' +@cloumns+' = ''' +@str+ ''''
    execute sp_executesql @sql,N'@tmp_counts int out',@counts out
    if @counts>0
    begin
    print '表名为:'+@tablename+',字段名为'+@cloumns
    end
    Fetch next From MyCursor Into @cloumns,@tablename
    End
    Close MyCursor
    Deallocate MyCursor

    '
    Open MyCursor
    Fetch next From MyCursor Into @cloumns,@tablename
    While(@@Fetch_Status = 0)
    Begin
    set @sql='select @tmp_counts=count(*) from ' +@tablename+ ' where ' +@cloumns+' = ''' +@str+ ''''
    execute sp_executesql @sql,N'@tmp_counts int out',@counts out
    if @counts>0
    begin
    print '表名为:'+@tablename+',字段名为'+@cloumns
    end
    Fetch next From MyCursor Into @cloumns,@tablename
    End
    Close MyCursor
    Deallocate MyCursor

    结果:

  • 相关阅读:
    jQuery插件开发之定宽输出
    全国所有省市县地理坐标Json格式
    diff/merge configuration in TFS
    How to accelerate your VPC 2007
    TechNet Magazine/TechNet杂志
    net helpmsg get error message for error id in cmd script
    devenv.exe /DebugExe debug unitcase
    ZoomIt for demo
    diskpart to deal with driver letter changed after reboot in Virtual Machine
    几个关于web开发资源的站点
  • 原文地址:https://www.cnblogs.com/xielianghui/p/6902259.html
Copyright © 2020-2023  润新知