从一张桌子转移到另一张桌子

时间:2020-04-15 21:18:15

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

我有两个名为final class DetailViewBuilder { static func make(object: Something) -> DetailView { let viewModel = DetailViewModel(object: object) let view = DetailView(viewModel: viewModel) return view } } code的表。

在代码表中,我将使用

codeTemporary

这个差异大约是一千万。

select Code from codeTemporary with (nolock) EXCEPT select Code from code with (nolock) 表具有“ Id(guid)”,“ Code(string)”,“ boxId(guid)”

codeTemporary表将具有相同的ID,getdate,getdate,默认guid,默认guid,相同的代码,相同的boxId,1。

code

但是该代码不起作用:

子查询返回了多个值。当 子查询遵循=,!=,<,<=,>,> =,或当子查询用作 表达式。

如何将丢失的代码从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)' 转移到codeTemporary

2 个答案:

答案 0 :(得分: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)'

答案 1 :(得分:2)

我将其表达为:

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 ...