主select 语句大致如下,把它称作major:SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...现在要做如下改动,当major结果集不为空时,就返回该结果集,否则
再执行另一条select语句: select B ,请问应该怎么写?多谢。
UNION ALL
SELECT ...
UNION ALL
SELECT ...现在要做如下改动,当major结果集不为空时,就返回该结果集,否则
再执行另一条select语句: select B ,请问应该怎么写?多谢。
as
begin
set nocount on
declare @count int
select @count=count(*) from major
if @count<>0
select * from major
else
select * from B
end
--或者
create proc prc
as
begin
set nocount on
select * from major
if @@rowcount<>0
select * from major
else
select * from B
endexec prc
UNION ALL
SELECT ...
UNION ALL
SELECT ...)
begin
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...
end
else
begin
select B
end
UNION ALL
SELECT ...
UNION ALL
SELECT ...)
begin
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...
end
else
begin
select B
end
一次在if exists执行,一次在不为空时?有效率问题吗?
begin
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...
end
else
begin
select B
end
临时表里面吧,是不是可以呢?
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ..先存到一个字符串变量里,这样的话,每次对major的修改就不用担心要同时改两个地方了?
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ..if not existes (select * from #aaa)
select B
else
select * from #aaa