SQL Server Select in if (subquery Result 0 rows use different statement)
SQL Server Select in if (subquery Result 0 rows use different statement)
我在 SQL Server 2008 R2 中有一个 table,我需要通过子查询获得结果。在某些情况下,子查询 returns 没有结果,在这种情况下,我需要这些具有空条件的查询。
declare @InsID as int =12288
declare @docentry as int = 28
select *
from [Table1] a
where a.U_InsID = @InsID
and a.DocEntry <> @docentry
and a.U_ObjectType in ('1','5')
and (month(a.U_Periode) in
(select max(MONTH(U_Periode))
from [Table1] e
where e.U_InsID = a.U_InsID
and e.DocEntry <> @docentry
and (a.U_Status <> '2' or a.U_Status is null))
or (a.U_Periode is null and a.DocEntry <> @docentry and (a.U_Status <> '2' or a.U_Status is null))
)
在这个查询中,我随时得到 "OR" 结果...但是当子查询没有结果时我只想要 "OR" 的结果...
有什么想法吗?
此致
奥利弗
好的,这是一个 Table。查询排除了我正在处理的行,并且应该 return 基于最大日期(月)的结果,如果不存在,则有一个行在 Period 中没有值。我需要得到这个。
在我的第一个例子中 "OR" 得到这些句子但是在任何时候......我在一分钟前想出了一个可行的解决方案......但我认为这不是一个很好的但它是工作..
declare @InsID as int =12288
declare @docentry as int = 31
declare @var as varchar(30)
--- get the max Month to U_InsID and not the actual Row '31'
set @var = (select max(MONTH(U_Periode))
from [TABLE1] e
where e.U_InsID=@InsID
and e.DocEntry <> @docentry
and (e.U_Status <> '2' or e.U_Status is null))
--- If there was no result do this....
if @var is null begin(
select * from [Table1] a
where a.U_InsID = @InsID
and a.DocEntry <> @docentry
and a.U_Periode is null and (a.U_Status <> '2' or a.U_Status is null)
)
end
else ---- do this if there was a result...
select * from [TABLE1] a
where a.U_InsID = @InsID
and a.DocEntry <> @docentry
and month(a.U_Periode)=@var and (a.U_Status <> '2' or a.U_Status is null)
我正在寻找一种更流畅的方法来代替使用此 IF...Else
我认为查询本身应该有一个解决方案...
最好的祝福
奥利弗
我在 SQL Server 2008 R2 中有一个 table,我需要通过子查询获得结果。在某些情况下,子查询 returns 没有结果,在这种情况下,我需要这些具有空条件的查询。
declare @InsID as int =12288
declare @docentry as int = 28
select *
from [Table1] a
where a.U_InsID = @InsID
and a.DocEntry <> @docentry
and a.U_ObjectType in ('1','5')
and (month(a.U_Periode) in
(select max(MONTH(U_Periode))
from [Table1] e
where e.U_InsID = a.U_InsID
and e.DocEntry <> @docentry
and (a.U_Status <> '2' or a.U_Status is null))
or (a.U_Periode is null and a.DocEntry <> @docentry and (a.U_Status <> '2' or a.U_Status is null))
)
在这个查询中,我随时得到 "OR" 结果...但是当子查询没有结果时我只想要 "OR" 的结果...
有什么想法吗?
此致 奥利弗
好的,这是一个 Table。查询排除了我正在处理的行,并且应该 return 基于最大日期(月)的结果,如果不存在,则有一个行在 Period 中没有值。我需要得到这个。
在我的第一个例子中 "OR" 得到这些句子但是在任何时候......我在一分钟前想出了一个可行的解决方案......但我认为这不是一个很好的但它是工作..
declare @InsID as int =12288
declare @docentry as int = 31
declare @var as varchar(30)
--- get the max Month to U_InsID and not the actual Row '31'
set @var = (select max(MONTH(U_Periode))
from [TABLE1] e
where e.U_InsID=@InsID
and e.DocEntry <> @docentry
and (e.U_Status <> '2' or e.U_Status is null))
--- If there was no result do this....
if @var is null begin(
select * from [Table1] a
where a.U_InsID = @InsID
and a.DocEntry <> @docentry
and a.U_Periode is null and (a.U_Status <> '2' or a.U_Status is null)
)
end
else ---- do this if there was a result...
select * from [TABLE1] a
where a.U_InsID = @InsID
and a.DocEntry <> @docentry
and month(a.U_Periode)=@var and (a.U_Status <> '2' or a.U_Status is null)
我正在寻找一种更流畅的方法来代替使用此 IF...Else
我认为查询本身应该有一个解决方案... 最好的祝福 奥利弗