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 2000 Forums
 SQL Server Development (2000)
 Dynamic WHERE clause sans Dynamic SQL (CASE?)

Author  Topic 

Nick
Posting Yak Master

155 Posts

Posted - 2003-09-05 : 12:40:21
Hello-

I'm having a problem. I'm trying to write a pretty simple SQL statement. The problem is I need the WHERE clause to be semi dynamic. Currently it looks like --

WHERE (tblDaysContacts.dayID = @dayID) AND (tblContactTypes.contactTypeID= CASE WHEN @ctID=-1 THEN tblContactTypes.contactTypeID ELSE @ctID END)

The desired results is to limit the records returned based on @dayID and @ctID. With the one problem being if -1 is passed as @ctID then I only want to filter based on @dayID and not @ctID. My current WHERE clause obviously doesn't work because if no contactTypeID exists then the record does not get returned, which is not the desired result.

Normally I would probably just build this as a dynamic SQL statement adding the second part of the WHERE clause based on and IF '@ctID=-1'. The end results of this stored procudure are being used by Crystal Reports however, and it seems to balk when the sproc uses dynamic SQL.

So, my question is. Is there a good way to do what I'm looking to do without using dynamic SQL. At first I thought I might use the CASE to place or leave out the second part of the WHERE clause, but I was running into syntax errors with that and think that it might not work like I want it to.

Any suggestion?
-

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-05 : 13:11:48
I'm missing it...

Isn't


CREATE TABLE myTable99 (col1 int)
GO

DECLARE @x int
SELECT @x = -1
SELECT * FROM myTable99 WHERE col1 = CASE WHEN @x = -1 THEN col1 ELSE @x END
GO

DROP TABLE myTable99
GO



What you want to do?



Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

Nick
Posting Yak Master

155 Posts

Posted - 2003-09-05 : 13:28:34
Well, that would work fine except that not all Contacts will have a contact type.


CREATE TABLE tblContacts (
contactID int IDENTITY (10001, 1) NOT NULL ,

CONSTRAINT PK_tblContacts PRIMARY KEY CLUSTERED
(
contactID
) WITH FILLFACTOR = 90 ON PRIMARY ,

) ON PRIMARY

CREATE TABLE tblContactTypes (
contactTypeID int IDENTITY (10001, 1) NOT NULL ,
contactTypeDesc nvarchar (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT PK_tblContactTypes PRIMARY KEY CLUSTERED
(
contactTypeID
) WITH FILLFACTOR = 90 ON PRIMARY
) ON PRIMARY

CREATE TABLE tblContactsContactTypes (
contactID int NOT NULL ,
contactTypeID int NOT NULL ,
CONSTRAINT PK_tblContactsContactTypes PRIMARY KEY CLUSTERED
(
contactID,
contactTypeID
) WITH FILLFACTOR = 90 ON PRIMARY ,
CONSTRAINT FK_tblContactsContactTypes_tblContacts FOREIGN KEY
(
contactID
) REFERENCES tblContacts (
contactID
) ON DELETE CASCADE ,
CONSTRAINT FK_tblContactsContactTypes_tblContactTypes FOREIGN KEY
(
contactTypeID
) REFERENCES tblContactTypes (
contactTypeID
) ON DELETE CASCADE
) ON PRIMARY

CREATE TABLE tblDaysContacts (
dayID int NOT NULL ,
contactID int NOT NULL ,
primaryContact bit NOT NULL CONSTRAINT DF_tblDaysContacts_primaryContact DEFAULT (0),
CONSTRAINT PK_tblDaysContacts PRIMARY KEY CLUSTERED
(
dayID,
contactID
) WITH FILLFACTOR = 90 ON PRIMARY ,
CONSTRAINT FK_tblDaysContacts_tblContacts FOREIGN KEY
(
contactID
) REFERENCES tblContacts (
contactID
) ON DELETE CASCADE ,
) ON PRIMARY


SELECT tblContactsContactTypes.contactID
FROM tblDaysContacts RIGHT OUTER JOINtblContacts ON tblDaysContacts.contactID = tblContacts.contactID LEFT OUTER JOIN tblContactTypes RIGHT OUTER JOIN tblContactsContactTypes ON tblContactTypes.contactTypeID=tblContactsContactTypes.contactTypeID ON tblContacts.contactID=tblContactsContactTypes.contactID
WHERE (tblDaysContacts.dayID = @dayID) AND tblContactTypes.contactTypeID= CASE WHEN @ctID=-1 THEN tblContactTypes.contactTypeID ELSE @ctID END)


So the problem becomes that I can't set tblContactTypes.contactTypeID=tblContactTypes.contactTypeID if @ctID is -1 because if the Contact is not related to any contactID they will not be returned by the query. Does this make any more sense?
Go to Top of Page

Nick
Posting Yak Master

155 Posts

Posted - 2003-09-05 : 13:37:47
Although it is not really the answer I am looking for, I did solve the problem by adding an IF statement before the SQL statement in question, and running another as an ELSE. This works fine seeing there are only two possibilites (@ctID=1 or NOT), but it would still be nice to find a more elegant solution.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-05 : 14:10:23
First



SELECT cct.contactID
FROM tblDaysContacts dc
RIGHT JOIN tblContacts c
ON dc.contactID = c.contactID
LEFT JOIN tblContactTypes t
RIGHT JOIN tblContactsContactTypes cct
ON t.contactTypeID=cct.contactTypeID
ON c.contactID=cct.contactID
WHERE dc.dayID = @dayId
AND t.contactTypeID= CASE WHEN @ctID=-1 THEN t.contactTypeID ELSE @ctID END


Don't think I've seen to many RIGHT JOINS (never mind sprinkling in some LEFT's..)

quote:

So the problem becomes that

I cant set tblContactTypes.contactTypeID=tblContactTypes.contactTypeID

if @ctID is -1

because

if the Contact is not related to any contactID

they will not be returned by the query.

Does this make any more sense?



Uh, no

You're setting the column to itself..unless it's Null...

Then add


ISNULL(t.contactTypeID,'')= CASE WHEN @ctID=-1 THEN ISNULL(t.contactTypeID,'') ELSE @ctID END




I think(? Well maybe I don't)


Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-09-05 : 15:09:11
WHERE (tblDaysContacts.dayID = @dayID) AND
(@ctID=-1 OR tblContactTypes.contactTypeID=@ctID)
Go to Top of Page
   

- Advertisement -