【问题标题】:Transfer from one table to another从一张桌子转移到另一张桌子
【发布时间】:2020-07-29 00:22:35
【问题描述】:

我有两个表,分别名为 codecodeTemporary

在我将使用的代码表中

select Code from codeTemporary with (nolock)
EXCEPT
select Code from code with (nolock)

这个差值大约是1000万。

codeTemporary 表有 "Id(guid)","Code(string)","boxId(guid)"

code 表将具有相同的 Id、getdate、getdate、默认 guid、默认 guid、相同的代码、相同的 boxId、1。

insert into Code
select Id,GETDATE(),GETDATE(),'00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000',
(
select Code from code with(nolock)
EXCEPT
select Code from codeTemporary with(nolock)
) as Code
,boxId,1 
from codeTemporary with(nolock)
where boxId='xxxxxx (guid)'

但该代码不起作用:

子查询返回超过 1 个值。这是不允许的,当 子查询遵循 =、!=、、>= 或当子查询用作 一个表达式。

如何将丢失的代码从codeTemporary 转移到code

【问题讨论】:

    标签: sql sql-server tsql select sql-insert


    【解决方案1】:

    我会这样说:

    insert into code
    select
        id,
        getdate(),
        getdate(),
        '00000000-0000-0000-0000-000000000000',
        '00000000-0000-0000-0000-000000000000',
        code
    from codeTemporary ct
    where 
        boxId = 'xxxxxx (guid)'
        and not exists (select 1 from code c where c.code = ct.code)
    

    请注意,枚举insert 的列是一种很好的做法,例如:

     insert into code(col1, col2, col3, ...)
     select ...
    

    【讨论】:

      【解决方案2】:

      我想你想要:

      insert into Code ( . . . ) -- list the columns here
          select Id, GETDATE(), GETDATE(),
                '00000000-0000-0000-0000-000000000000',
                '00000000-0000-0000-0000-000000000000',
                tc.code
          from codeTemporary ct
          where not exists (select 1 
                            from code c
                            where c.code = ct.code
                           ) and
                ct.boxId ='xxxxxx (guid)'
      

      【讨论】:

        猜你喜欢
        • 2011-10-09
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 2012-05-23
        • 2019-02-25
        • 2015-06-20
        • 2014-01-24
        相关资源
        最近更新 更多