插入到从SQL Server中选择
表1中是与表2相对的一些变化。我只需要进入表3中的值,而表1和表2中缺少这些值 表1插入到从SQL Server中选择,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,表1中是与表2相对的一些变化。我只需要进入表3中的值,而表1和表2中缺少这些值 表1 Column1 a b d e g h i 表2 Column1 a b c d e f 代码: 我得到一个错误: Msg 213,16级,状态1,第1行 提供的值的列名或数目与表定义不匹配 我需要: 表3 Column1 c f 昨天我的剧本还不错,但今天出了问题。。我不知道为什么 感谢您在使用“插入列表”列时发表的意见: INSERT INTO [dbo].[table3](column1)
Column1
a
b
d
e
g
h
i
表2
Column1
a
b
c
d
e
f
代码:
我得到一个错误:
Msg 213,16级,状态1,第1行
提供的值的列名或数目与表定义不匹配
我需要:
表3
Column1
c
f
昨天我的剧本还不错,但今天出了问题。。我不知道为什么
感谢您在使用“插入列表”列时发表的意见:
INSERT INTO [dbo].[table3](column1)
SELECT t2.column1
FROM [dbo].[table2] t2
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[table1] t1
WHERE t2.column1 = t1.column1
);
当然,插入可以有多个列。只需在insert和select中列出它们
我不知道该怎么办。这不是你问题的一部分。当然,您可以向子查询中的WHERE子句添加其他子句
如果表3不存在,则使用选择插入而不是插入:
使用“插入”时,请列出列:
INSERT INTO [dbo].[table3](column1)
SELECT t2.column1
FROM [dbo].[table2] t2
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[table1] t1
WHERE t2.column1 = t1.column1
);
当然,插入可以有多个列。只需在insert和select中列出它们
我不知道该怎么办。这不是你问题的一部分。当然,您可以向子查询中的WHERE子句添加其他子句
如果表3不存在,则使用选择插入而不是插入:
您可以使用如下所示的not in-sub查询
select * into #yourTable3 from #yourtable2
where column1 not in (select column1 from #yourtable1 where column1 is not null)
您的桌子:
create table #yourtable1 (column1 varchar(10) )
insert into #yourtable1 (
Column1 ) values
('a')
,('b')
,(null )
,('d')
,('e')
, ( null )
,('g')
,('h')
,('i')
create table #yourtable2 (column1 varchar(10))
insert into #yourtable2 (column1 ) values
('a')
,('b')
,('c')
,('d')
,('e')
,('f')
您可以使用如下所示的not in-sub查询
select * into #yourTable3 from #yourtable2
where column1 not in (select column1 from #yourtable1 where column1 is not null)
您的桌子:
create table #yourtable1 (column1 varchar(10) )
insert into #yourtable1 (
Column1 ) values
('a')
,('b')
,(null )
,('d')
,('e')
, ( null )
,('g')
,('h')
,('i')
create table #yourtable2 (column1 varchar(10))
insert into #yourtable2 (column1 ) values
('a')
,('b')
,('c')
,('d')
,('e')
,('f')
What's.Pernr以及为什么要将一列与另一个表中的两个不同列进行比较??What's.Pernr以及为什么要将一列与另一个表中的两个不同列进行比较??