August 20, 2004 at 2:34 am
I have a Procedure for search clients. User can give name, lastname, phone and so on but some of those might be Null. The procedure receive the optional @parameters. I have use the following query
Select * FROM dbo.CLIENTS WHERE lastname = isnull(@prmlastname,lastname) and name=isnull(@prmname,name) and ... But of course will be more efficient some like:
IF @prmlastname in null and @prmname is not null select * from dbo.clientes where name=@name but if a have 5 parametres it could be as much as 25 IFs and that is a lot of work.
Other opcion Dinamic SQL but then I lost the precompiled caracteristic.
What is the best solution?
Is there any other solution for optional paramenters?
Thanks for your help
August 22, 2004 at 4:50 pm
The following example is straight out of "Books On Line"... modify it to handle your 5 nullable input parameters...
The parameter default can be the value NULL. In this case, if you do not supply a parameter, SQL Server executes the stored procedure according to its other statements. No error message is displayed.
The procedure definition can also specify that some other action be taken if you do not give a parameter. For example:
CREATE PROC showind3
@table varchar(30) = NULL
AS
IF @table IS NULL PRINT 'Give a table name' ELSE SELECT TABLE_NAME = sysobjects.name, INDEX_NAME = sysindexes.name, INDEX_ID = indid FROM sysindexes INNER JOIN sysobjects ON sysobjects.id = sysindexes.id WHERE sysobjects.name = @table
--Jeff Moden
Change is inevitable... Change for the better is not.
August 23, 2004 at 3:55 am
Federico,
Read the following article by Erland Sommarskog, SQL Server MVP:
http://www.sommarskog.se/dyn-search.html
It's an in-depth article about this problem, with many solutions (using Dynamic SQL or Static SQL).
Razvan
August 24, 2004 at 10:17 am
This SEEMS to work, but no guarantees!
- john
USE Northwind
GO
-- Demonstrate A Query Using An Optional Parameter
CREATE PROCEDURE spGetOrders
@OrderID INT = NULL
AS
BEGIN
SELECT OrderID, CustomerID, EmployeeID, OrderDate
FROM Orders
WHERE OrderID =
CASE
WHEN @OrderID IS NULL THEN OrderID
ELSE @OrderID
END
ORDER BY OrderID
END
GO
-- Test:
EXEC spGetOrders -- Returns All Rows
GO
EXEC spGetOrders 10258 -- Returns 1 Row
GO
August 24, 2004 at 10:43 am
(Update--continued from last post) Actually, I vaguely recall having "gotten burned" by some weird NULL-handling behavior in a query like this. Things didn't always perform as expected.
Instead of testing against NULL, it might be safer if you hard-code a value for the optional parameter that you KNOW will never appear in the data -- e.g.,
-- SAMPLE #2: Demonstrate A Query Using An Optional Parameter
CREATE PROCEDURE spGetOrders2
@OrderID INT = -9999
AS
BEGIN
SELECT OrderID, CustomerID, EmployeeID, OrderDate
FROM Orders
WHERE OrderID =
CASE
WHEN @OrderID = -9999 THEN OrderID
ELSE @OrderID
END
ORDER BY OrderID
END
GO
-- Test:
EXEC spGetOrders2 -- Returns All Rows
GO
EXEC spGetOrders2 10258 -- Returns One Row
GO
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply