Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2008-02-28 : 06:39:25
|
I sometimes need to pass in blank arguments to a stored procedure.For example, in a drop down on a webpage, the user select all.I need it to then ignore part of the where caluse.I could use various IF statements in the SQL but I understand this is bad practice:CREATE PROCEDURE web_ViewQueries (@costcent varchar(4),@creditcontroller varchar(50),@QueryType int)ASSELECT invoice_no, cust_name, credit_contname, project_no, costcent, queryID, --SY numbers, inv_date, amount, totamnt, Query.ProjMan, Query.ProfitCentreMan, Query.ClientContactName, Query.ClientContactTel, Query.Comments, QueryType.Description, Structure.[Company Description], Structure.[Div Level Description], Structure.[Div Level], Structure.[Bus Unit Level], Structure.[Regional Level]FROM InvoicesLEFT JOIN Query ON Query.ID = Invoices.QueryIDLEFT JOIN QueryType on QueryType.ID = Query.QueryTypeLEFT JOIN Structure ON Structure.[Cost Centre] = Invoices.costcentWHERE InvoiceStatus = 12AND Invoices.costcent = @costcentAND Invoices.credit_contname = @creditcontrollerAND QueryType.ID = @QueryTypeORDER BY Invoices.cust_name |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-28 : 06:42:26
|
quote: Originally posted by qwertyjjj I sometimes need to pass in blank arguments to a stored procedure.For example, in a drop down on a webpage, the user select all.I need it to then ignore part of the where caluse.I could use various IF statements in the SQL but I understand this is bad practice:CREATE PROCEDURE web_ViewQueries (@costcent varchar(4),@creditcontroller varchar(50),@QueryType int)ASSELECT invoice_no, cust_name, credit_contname, project_no, costcent, queryID, --SY numbers, inv_date, amount, totamnt, Query.ProjMan, Query.ProfitCentreMan, Query.ClientContactName, Query.ClientContactTel, Query.Comments, QueryType.Description, Structure.[Company Description], Structure.[Div Level Description], Structure.[Div Level], Structure.[Bus Unit Level], Structure.[Regional Level]FROM InvoicesLEFT JOIN Query ON Query.ID = Invoices.QueryIDLEFT JOIN QueryType on QueryType.ID = Query.QueryTypeLEFT JOIN Structure ON Structure.[Cost Centre] = Invoices.costcentWHERE InvoiceStatus = 12AND (Invoices.costcent = @costcent OR ISNULL(@costcent,'')='')AND (Invoices.credit_contname = @creditcontroller OR ISNULL(@creditcontroller ,'')='')AND (QueryType.ID = @QueryType OR ISNULL(@QueryType ,'')='')ORDER BY Invoices.cust_name
|
 |
|
|
|
|
|
|