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

    结果:

  • 相关阅读:
    21.栈的压入、弹出序列(python)
    19.顺时针打印矩阵(python)
    18.二叉树的镜像(python)
    [leetcode] 82. 删除排序链表中的重复元素 II
    [leetcode] 83. 删除排序链表中的重复元素
    [leetcode] 81. 搜索旋转排序数组 II
    [leetcode] 80. 删除排序数组中的重复项 II
    [leetcode] 208. 实现 Trie (前缀树)(Java)
    [leetcode] 212. 单词搜索 II(Java)
    [leetcode] 79. 单词搜索
  • 原文地址:https://www.cnblogs.com/xielianghui/p/6902259.html
Copyright © 2011-2022 走看看