zoukankan      html  css  js  c++  java
  • 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

    结果:

  • 相关阅读:
    poj 4005 Moles
    牛客 2C 圈圈
    牛客 2B 树 (组合计数)
    AC日记——校门外的树(增强版) 洛谷 P1276
    AC日记——寻找道路 洛谷 P2296
    AC日记——挤牛奶 洛谷 P1204
    AC日记——最大数 洛谷 P1198 [JSOI2008]
    AC日记——中位数 洛谷 P1168
    AC日记——校门外的树 洛谷 P1047
    AC日记——约瑟夫问题 codevs 1282
  • 原文地址:https://www.cnblogs.com/xielianghui/p/6902259.html
Copyright © 2011-2022 走看看