tags:

views:

58

answers:

3

Is there any alternate way to create stored procedure without putting all query in one long string if criteria of WWHERE clause can be different.

Suppose I have Orders table I want to create stored procedure on this table and there are three column on which I wnat to filter records.

1- CustomerId, 2- SupplierId, 3- ProductId.

If user only give CustomerId in search criteria then query should be like following

SELECT * FROM Orders WHERE Orders.CustomerId = @customerId

And if user only give ProductId in search criteria then query should be like following

SELECT * FROM Orders WHERE Orders.ProductId = @productId

And if user only all three CustomerId, ProductId, and SupplierId is given then all three Ids will be used in WHERE to filter.

There is also chance that user don't want to filter record then query should be like following

SELCT * FROM Orders

Whenever I have to create this kind of procedure I put all this in string and use IF conditions to check if arguments (@customeId or @supplierId etc) has values.

I use following method to create procedure

DECLARE @query      VARCHAR(MAX)
DECLARE @queryWhere VARCHAR(MAX)

SET @query = @query + 'SELECT * FROM Orders '

IF (@originationNumber IS NOT NULL)
BEGIN
    BEGIN
        SET @queryWhere =@queryWhere + ' Orders.CustomerId = ' + CONVERT(VARCHAR(100),@customerId)
    END
END

IF(@queryWhere <> '')
BEGIN
   SET @query = @query+' WHERE ' + @queryWhere
END

EXEC (@query)

Thanks.

+1  A: 

You could pass NULL for fields that you don't want to include in your WHERE-clause and check for NULL in the query:

Select customerId, productId, supplierId
From Orders
Where ( customerId = @customerId Or @customerId IS NULL )
  And ( productId = @productId Or @productId IS NULL )
  And ( supplierId= @supplierId Or @supplierId IS NULL )

Don't use SELECT *, always list the columns you actually need.

Peter Lang
+1  A: 

HI,

Select CustomerId, ProductId, SupplierId
From Orders
Where CustomerId = ISNULL( @customerId, CustomerId   )
  And ProductId = ISNULL( @productId, ProductId  )
  And SupplierId= ISNULL( @supplierId, SupplierId )
IordanTanev
I'm not sure if index can be used if you use `ISNULL`?
Peter Lang
i have used it on several occasions in stored procedures working on tables with thousand of rows and had no problems of indexes but i have never run Execution plan to see if index is working. This is a good remark i will run few Execution plans to see what is happening
IordanTanev
I tried with Oracle (`NVL` and `COALESCE`), and there the index was not used. Please run your tests and let us know if TSQL has some sort of special optimization for `ISNULL`.
Peter Lang
@PeterLang you are completely right.I create table containing int ID an two nvarchar columns. On one of the nvarchar columns i created a index and the index did not fire.I also found that why i head no problem using ISNULL. When you create PK in a table in MSSQL 2005 it creates clustered index and when you use ISNULL in where this clustered index is used.
IordanTanev
@IordanTanev: Thanks for trying. I think it's better to use `OR` instead of `ISNULL` to allow the use of indexes: http://stackoverflow.com/questions/2522886/creating-stored-procedure-having-different-where-clause-on-different-search-crite/2522911#2522911
Peter Lang
A: 

Erland Sommarskog has a great pair of articles, describing various search techniques and trade offs (one for SQL 2008, one for earlier versions)

Damien_The_Unbeliever