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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help needed with SP

Author  Topic 

sql_server_dba
Posting Yak Master

167 Posts

Posted - 2010-06-10 : 12:20:21
Hello,

I need some guidance here to modify one of my SP...
1.) I have table AAAA with 3 columns A CHAR,B CHAR,C DATETIME and all are NOT NULLS.

Now coming to the main topic....
I have an SP like this and working fine since we never gave null values for the parameters.

CREATE PROCEDURE XYZ
@A CHAR(10) = NULL
@B CHAR(10) = NULL
@C DATETIME = NULL
AS
BEGIN
IF ((@A IS NULL) OR (@B IS NULL) OR (@C IS NULL))
BEGIN
SELECT top 1 A,B,C FROM AAAA
WHERE A = @A AND B = @B and C = @C
END
ELSE
BEGIN
SELECT A,B,C FROM AAAA
WHERE A = @A AND B = @B and C = @C
END
END

The whole SP works fine...but the SP needs to be changed since for the first condition in SP where @A or @B or @C is null, the SP does not return the record even if it exists since we are comparing them in WHERE condition (to NULL Values). Now i need to modify the 1st part of the SP. How can i do this? Any help appreciated.

Thanks in Advance.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-06-10 : 12:53:42
Sounds like you are implementing a catch all query. Check out this link:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page

sql_server_dba
Posting Yak Master

167 Posts

Posted - 2010-06-10 : 14:54:47
quote:
Originally posted by Lamprey

Sounds like you are implementing a catch all query. Check out this link:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/




The Link really helped me lot. But i got a problem here. As you can see the select statement in the first condition...its SELECT TOP1.... So if we follow catch all query, then we might be missing the TOP 1 part. How can we implement that TOP 1 part with this catch all query? Is it possible in it?
Go to Top of Page
   

- Advertisement -