SQL Server Select in if (subquery Result 0 rows use different statement) -
i have table in sql server 2008 r2 need result subquery. in case subquery returns no results, , in case need these query null condition.
declare @insid int =12288 declare @docentry int = 28 select * [table1] a.u_insid = @insid , a.docentry <> @docentry , a.u_objecttype in ('1','5') , (month(a.u_periode) in (select max(month(u_periode)) [table1] e e.u_insid = a.u_insid , e.docentry <> @docentry , (a.u_status <> '2' or a.u_status null)) or (a.u_periode null , a.docentry <> @docentry , (a.u_status <> '2' or a.u_status null)) )
in query "or" result on time... want result of "or" when subquery result nothing...
any ideas?
best regards oliver
okay 1 table. query exclude row i'm working on , should return result based on max date(month) , if not there, there row has no value in period. , need this.
in first example "or" these sentence on time... figuerd out working solution minute ago... geues not nice 1 working..
declare @insid int =12288 declare @docentry int = 31 declare @var varchar(30) --- max month u_insid , not actual row '31' set @var = (select max(month(u_periode)) [table1] e e.u_insid=@insid , e.docentry <> @docentry , (e.u_status <> '2' or e.u_status null)) --- if there no result this.... if @var null begin( select * [table1] a.u_insid = @insid , a.docentry <> @docentry , a.u_periode null , (a.u_status <> '2' or a.u_status null) ) end else ---- if there result... select * [table1] a.u_insid = @insid , a.docentry <> @docentry , month(a.u_periode)=@var , (a.u_status <> '2' or a.u_status null)
and i'm looking smoother way of doing instead using if... else
i thought there should solution within query itself... best regards oliver
Comments
Post a Comment