彩世界平台-彩世界时时app-彩世界开奖app苹果下载

热门关键词: 彩世界平台,彩世界时时app,彩世界开奖app苹果下载

您的位置:彩世界平台 > 工作委员会 > SQL Server 查询某个字段值在哪张表的哪个字段

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

发布时间:2019-11-30 00:04编辑:工作委员会浏览(69)

    具体方法如下

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

    一:update 表2 set (要插入的列名)= select 表1.某一列 from 表1 left jion 表2 on 表1和表2的关联 where .....  

    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='

    二:update 表1 set 表1.列=表2.列 from 表2 where 表2.id=表1.id 

    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

    三:update _a set 列=_b.列 from 表1 _a join 表2 _b on _a.id=_b.id

    '
    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

    本文由彩世界平台发布于工作委员会,转载请注明出处:SQL Server 查询某个字段值在哪张表的哪个字段

    关键词:

上一篇:没有了

下一篇:没有了