插入 select 语句,将所有其他列设置为空
Insert into select statement that sets all other columns to null
我正在尝试 运行 此插入 select 语句:
insert into xyz (select t1.column1, t1.column2 from table1 t1, table2 t2
where t1.column1 = t2.column1)
但是得到错误:"The number of values assigned is not the same as the number of specified or implied columns or variables"
我想将所有其他列设置为空(例如 t1.column3 到 t1.column50)。
有没有一种方法可以做到这一点而不必在 select 语句中明确列出所有列?
insert into xyz (select t1.column1, t1.column2, null as column3, null as column4, null as
column5... from table1 t1, table2 t2
where t1.column1 = t2.column1)
您可以列出插入列表中的列。例如
insert into xyz ( column1, column2 )
select t1.column1, t1.column2
from table1 t1
, table2 t2
where t1.column1 = t2.column1
所以,例如
create table xyz( column1 int not null, column2 int not null, column3 int);
create table table1( column1 int not null, column2 int not null);
create table table2( column1 int not null);
insert into table1 values (1,2),(2,3);
insert into table2 values (1),(2);
select * from xyz;
给予
COLUMN1 COLUMN2 COLUMN3
------- ------- -------
1 2 NULL
2 3 NULL
我正在尝试 运行 此插入 select 语句:
insert into xyz (select t1.column1, t1.column2 from table1 t1, table2 t2
where t1.column1 = t2.column1)
但是得到错误:"The number of values assigned is not the same as the number of specified or implied columns or variables"
我想将所有其他列设置为空(例如 t1.column3 到 t1.column50)。
有没有一种方法可以做到这一点而不必在 select 语句中明确列出所有列?
insert into xyz (select t1.column1, t1.column2, null as column3, null as column4, null as
column5... from table1 t1, table2 t2
where t1.column1 = t2.column1)
您可以列出插入列表中的列。例如
insert into xyz ( column1, column2 )
select t1.column1, t1.column2
from table1 t1
, table2 t2
where t1.column1 = t2.column1
所以,例如
create table xyz( column1 int not null, column2 int not null, column3 int);
create table table1( column1 int not null, column2 int not null);
create table table2( column1 int not null);
insert into table1 values (1,2),(2,3);
insert into table2 values (1),(2);
select * from xyz;
给予
COLUMN1 COLUMN2 COLUMN3
------- ------- -------
1 2 NULL
2 3 NULL