如果 SQL Server 2008 R2 中不存在则插入值
Insert value if not exists in SQL Server 2008 R2
create table #tableA(id int, val varchar(50))
create table #tableB(id int, val varchar(50))
create table #tableC(id int, val varchar(50))
create table #tableD(id int, val varchar(50))
insert into #tableB values (1, '11');
insert into #tableB values (2, '22');
我想为#tableD 插入值,我的条件是
如果#tableA
有值则
insert into #tableD
select *
from #tableA;
如果 #tableA
为空则
insert into #tableD
select *
from #tableB;
如果 #tableA
和 #tableB
为空则
insert into #tableD
select *
from #tableC;
我怎样才能以最简单的方式做到这一点?
像这样的东西应该可以工作:
if exists (select 1 from #tableA)
begin
insert into #tableD
select * from #tableA
end
else
begin
insert into #tableD
select * from #tableB
end
if not exists
(select 1 from #tableA union select 1 from #tableB)
begin
insert into #tableD
select * from #tableC
end
使用(Transact-SQL)EXISTS
根据条件插入值
if exists (select * from #tableA)
begin
insert into #tableD select * from #tableA;
end
else
begin
insert into #tableD select * from #tableB;
end
if not exists (select * from #tableA)
begin
if not exists (select * from #tableB)
begin
insert into #tableD select * from #tableC;
end
end
尝试使用Transact-SQL语句IF..THEN..ELSE
IF EXISTS(SELECT * FROM #TableA)
BEGIN
insert into #tableD select * from #tableA;
END
ELSE IF EXISTS(SELECT * FROM #TableB)
BEGIN
insert into #tableD select * from #tableB;
END
ELSE
BEGIN
insert into #tableD select * from #tableC;
END
两种基本方法,但如果临时表的数量是动态的,则效果不佳。请注意,我在查询中使用 *,但最佳做法是指定列
第一个工作正常但在大数据上可能会很慢,并且比需要做更多的工作,但如果是小数据集应该没问题
insert into #tabled
select * from #tablea union all
select * from #tableb where 0 = (select count(*) from #tableA) union all
select * from #tablec where 0 = (select count(*)
from (select top 1 id from #tablea
union all
select top 1 id from #tableb
) x
)
或者第二种方法可以,只做必要的工作。
insert into #tableD select * from #tableA
if @@rowcount = 0
begin
insert into #tableD select * from #tableB
if @@rowcount = 0
begin
insert into #tableD select * from #tableC
if @@rowcount = 0
begin
print('no rows inserted')
end
else
begin
print('rows inserted from C')
end
end
else
begin
print('inserted from B')
end
end
else
begin
print('insert from A')
end
使用这个
INSERT INTO #tabled
SELECT * FROM #tableA
UNION ALL
SELECT * FROM #tableB
UNION ALL
SELECT * FROM #tableC
create table #tableA(id int, val varchar(50))
create table #tableB(id int, val varchar(50))
create table #tableC(id int, val varchar(50))
create table #tableD(id int, val varchar(50))
insert into #tableB values (1, '11');
insert into #tableB values (2, '22');
我想为#tableD 插入值,我的条件是
如果
#tableA
有值则insert into #tableD select * from #tableA;
如果
#tableA
为空则insert into #tableD select * from #tableB;
如果
#tableA
和#tableB
为空则insert into #tableD select * from #tableC;
我怎样才能以最简单的方式做到这一点?
像这样的东西应该可以工作:
if exists (select 1 from #tableA)
begin
insert into #tableD
select * from #tableA
end
else
begin
insert into #tableD
select * from #tableB
end
if not exists
(select 1 from #tableA union select 1 from #tableB)
begin
insert into #tableD
select * from #tableC
end
使用(Transact-SQL)EXISTS
根据条件插入值
if exists (select * from #tableA)
begin
insert into #tableD select * from #tableA;
end
else
begin
insert into #tableD select * from #tableB;
end
if not exists (select * from #tableA)
begin
if not exists (select * from #tableB)
begin
insert into #tableD select * from #tableC;
end
end
尝试使用Transact-SQL语句IF..THEN..ELSE
IF EXISTS(SELECT * FROM #TableA)
BEGIN
insert into #tableD select * from #tableA;
END
ELSE IF EXISTS(SELECT * FROM #TableB)
BEGIN
insert into #tableD select * from #tableB;
END
ELSE
BEGIN
insert into #tableD select * from #tableC;
END
两种基本方法,但如果临时表的数量是动态的,则效果不佳。请注意,我在查询中使用 *,但最佳做法是指定列
第一个工作正常但在大数据上可能会很慢,并且比需要做更多的工作,但如果是小数据集应该没问题
insert into #tabled
select * from #tablea union all
select * from #tableb where 0 = (select count(*) from #tableA) union all
select * from #tablec where 0 = (select count(*)
from (select top 1 id from #tablea
union all
select top 1 id from #tableb
) x
)
或者第二种方法可以,只做必要的工作。
insert into #tableD select * from #tableA
if @@rowcount = 0
begin
insert into #tableD select * from #tableB
if @@rowcount = 0
begin
insert into #tableD select * from #tableC
if @@rowcount = 0
begin
print('no rows inserted')
end
else
begin
print('rows inserted from C')
end
end
else
begin
print('inserted from B')
end
end
else
begin
print('insert from A')
end
使用这个
INSERT INTO #tabled
SELECT * FROM #tableA
UNION ALL
SELECT * FROM #tableB
UNION ALL
SELECT * FROM #tableC