sql server 2008 - Determine what param to use in Select statement in a Stored Procedure -


i have stored procedure returns common query, need call in several functions functions may call through period id or others through header id, far know how can determine param use in order retrive data properly, have implemented.

create procedure dbo.gettfdrecordinfo     @periodid int = null,     @headerid int  = null begin      select         -- have lot more fields , joins here, that's why need statement in single call through either period id or header id          *            nt_csrtnvperiodinfo t          -- how can make possible shown above, can use "case when"?         (                              /*                 if @periodid null                      t.headerid = @headerid                  if @headerid null                     t.periodid = @periodid                 */             )     end go  -- swtich between params exec nt_csrtnvperiodinfo null, 2654 exec nt_csrtnvperiodinfo 196, null 

this answer:

create procedure dbo.gettfdrecordinfo     @periodid int = null,     @headerid int  = null begin      select         -- have lot more fields , joins here, that's why need statement in single call through either period id or header id          *            nt_csrtnvperiodinfo t          -- how can make possible shown above, can use "case when"?         ((@periodid null) or (t.periodid = @periodid))         ,   ((@headerid null) or (t.headerid = @headerid)) end go 

you have use conditional or check nulls, if param set, second condition checked, if not, procedure consider true statement , go next.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -