Tuesday, 15 May 2012

sql - Where condition based on parameter value -



sql - Where condition based on parameter value -

i have next procedure:

create procedure loadsolditens @datestart datetime, @dateend datetime, @sectionid int = null begin select sectionid, name #sections loadsections(@sectionid) select s.id soldid, s.quantity soldquantity, solditens s with(nolock) left bring together #sections sc on s.sectionid = sc.sectionid (isnull(s.solddate, @datestart) between @datestart , @dateend) -- , --here i'm stuck .... end

the parameter @sectionid optional , if user sends null or -10 @sectionid parameter, need retrieve info have s.sectionid null value or value.

if user sends @sectionid > 0, need retrieve info have s.section = @sectionid.

my lastly seek was:

and (isnull(@sectionid, s.sectionid) null or isnull(@sectionid, s.sectionid) not null) -- how check @sectionid = -10 or @sectionid > 0

anyone give me hand, please?

thank in advance.

just this... compare section id if not null or if null.

and isnull(@sectionid, s.sectionid) = s.sectionid

to check -10 this:

where (... , isnull(@sectionid, s.sectionid) = s.sectionid) or @sectionid = -10

please run next total query, work:

select s.id soldid, s.quantity soldquantity, solditens s with(nolock) left bring together #sections sc on sc.sectionid = sc.sectionid isnull(s.solddate, @datestart) between @datestart , @dateend , ( isnull(@sectionid, s.sectionid) = s.sectionid) or @sectionid = -10 )

sql sql-server tsql

No comments:

Post a Comment