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
Post a Comment