all groups > sql server programming > september 2004 >
You're in the

sql server programming

group:

Can a stored procedure have a case statement style structure?


Can a stored procedure have a case statement style structure? CLM
9/24/2004 8:25:15 PM
sql server programming:
I have a stored procedure that I am using and feeding a parameter of 1, 2, or 3
1 = criteriaA is null and criteriaB is null
2 = criteriaA is not null and criteriaB is not null
3 = criteriaA is null or is not null criteriaB is null or is not null

can I
IF @param = 1
Begin
select * from tmpTable where criteraA is null and criteriaB is null
end

IF @param = 2
Begin
select * from tmpTable where criteriaA is not null and criteriaB is not
null
end

IF @param=3
Begin
select * from tmpTable where criteriaA is null or is not null and
criteriaB is null or is not null
end

I know the syntax isn't 100% but will this kind of action work? I can feed
string parameters to the other criteria fields but the other fields are a
linked table that if they are not present (is null) gives one set of results
as opposted to be being present (is not null) or a combined set of results.
There is no way to feed a string parameter to that type of criteria line.

Any info would be greatly appreciated.

Re: Can a stored procedure have a case statement style structure? Roji. P. Thomas
9/25/2004 9:58:13 AM
This might help you

http://www.sommarskog.se/dyn-search.html

--
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com


[quoted text, click to view]

Re: Can a stored procedure have a case statement style structure? Joe Celko
9/25/2004 4:02:35 PM
Here is an example of how to use a CASE expression for complex logic.

SELECT *
FROM Foobar
WHERE CASE
WHEN @param = 1 -- both null
AND COLEASE (criteraA, criteriaB) IS NULL
THEN 1
WHEN @param = 2 -- no nulls
AND criteriaA IS NOT NULL
AND criteriaB IS NOT NULL
THEN 1
WHEN @param = 3 -- one null
AND COALESCE (criteriaA, criteriaB) IS NOT NULL
THEN 1 ELSE 0 END = 1;

--CELKO--
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.


*** Sent via Developersdex http://www.developersdex.com ***
Re: Can a stored procedure have a case statement style structure? Steve Kass
9/25/2004 11:27:30 PM
Unfortunately, this doesn't work, even when the spelling errors are
corrected. The predicates

COALESCE(criteriaA, criteriaB) IS NULL
and
COALESCE(criteriaA,criteriaB IS NOT NULL

are not equivalent to the predicates

criteriaA IS NULL AND criteriaB IS NULL
and
criteriaA IS NULL OR criteriaB IS NULL

as can be seen from the errors raised here:

CREATE TABLE Foobar (
criteriaA int,
criteriaB char
)
INSERT INTO Foobar values (null,'A')
GO

CREATE PROCEDURE p (
@param int
) AS
SELECT *
FROM Foobar
WHERE CASE
WHEN @param = 1 -- both null
AND COALESCE (criteriaA, criteriaB) IS NULL
THEN 1
WHEN @param = 2 -- no nulls
AND criteriaA IS NOT NULL
AND criteriaB IS NOT NULL
THEN 1
WHEN @param = 3 -- one null
AND COALESCE (criteriaA, criteriaB) IS NOT NULL
THEN 1 ELSE 0 END = 1;
GO

EXEC p 1
GO
EXEC p 3
GO

DROP PROC p
DROP TABLE Foobar

The solution Hugo gave works fine.

Steve Kass
Drew University



[quoted text, click to view]
Re: Can a stored procedure have a case statement style structure? Hugo Kornelis
9/25/2004 11:49:04 PM
[quoted text, click to view]

Hi CLM,

Yes, you can. And the syntax is almost completely correct.

IF @param = 1
BEGIN
SELECT Column_List -- Don't use SELECT * in production code!
FROM tmpTable
WHERE criteriaA IS NULL
AND criteriaB IS NULL
END
IF @param = 2
BEGIN
SELECT Column_List -- Don't use SELECT * in production code!
FROM tmpTable
WHERE criteriaA IS NOT NULL
AND criteriaB IS NOT NULL
END
IF @param = 3
BEGIN
SELECT Column_List -- Don't use SELECT * in production code!
FROM tmpTable
-- No WHERE clause: (IS NULL or IS NOT NULL) is always true!!
END


Best, Hugo
--

AddThis Social Bookmark Button