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 in Stored Procedure

Author  Topic 

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-07 : 15:27:51
I get an error pointing at the @WHERE_CLAUSE variable when I try to execute it. Can I do this?
Thanks

CREATE PROCEDURE dbo.db_GetUsers
-- Gets all ACTIVE users
@ChatRoomID int
AS

DECLARE @WHERE_CLAUSE varchar(255)

If @ChatRoomID > 0
BEGIN
SET @WHERE_CLAUSE = 'dbo.Chatters.isActive = 1 AND dbo.Chatters.ChatId =' + @ChatRoomID
END
ELSE
BEGIN
SET @WHERE_CLAUSE = 'dbo.Chatters.isActive = 1'
END

SELECT dbo.Chatters.cName, dbo.Chatters.id, dbo.Chatters.lastAction, dbo.Roles.Type, dbo.ChatSettings.Name
FROM dbo.Chatters INNER JOIN
dbo.ChatSettings ON dbo.Chatters.ChatId = dbo.ChatSettings.id INNER JOIN
dbo.Roles ON dbo.Chatters.RoleID = dbo.Roles.id
WHERE @WHERE_CLAUSE ORDER BY dbo.ChatSettings.Name ASC



M.E.
Aged Yak Warrior

539 Posts

Posted - 2002-08-07 : 15:54:29
Hmm, about 5th question like this I've seen in the past 2 days. Run a search on this site for dynamic SQL

you'll need to set it up like

set @sql = 'select * from users where' +@where
exec (@sql)

-----------------------
Take my advice, I dare ya
Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-08-07 : 16:13:35
Here's a couple of ways :

 

CREATE PROCEDURE dbo.db_GetUsers (@ChatRoomID int=NULL)
AS
SET NOCOUNT ON

SELECT c.cName, dbo.c.id, c.lastAction, r.Type,cs.Name
FROM dbo.Chatters c
INNER JOIN dbo.ChatSettings cs ON c.ChatId = cs.id
INNER JOIN dbo.Roles r ON c.RoleID = r.id
WHERE c.isActive = 1
AND c.ChatId = ISNULL(@ChatRoomID,c.ChatId)
ORDER BY cs.Name ASC

GO

CREATE PROCEDURE dbo.db_GetUsers (@ChatRoomID int=NULL)
AS
SET NOCOUNT ON

IF @ChatRoomID IS NULL
BEGIN

SELECT c.cName, dbo.c.id, c.lastAction, r.Type,cs.Name
FROM dbo.Chatters c
INNER JOIN dbo.ChatSettings cs ON c.ChatId = cs.id
INNER JOIN dbo.Roles r ON c.RoleID = r.id
WHERE c.isActive = 1
ORDER BY cs.Name ASC
END
ELSE
BEGIN

SELECT c.cName, dbo.c.id, c.lastAction, r.Type,cs.Name
FROM dbo.Chatters c
INNER JOIN dbo.ChatSettings cs ON c.ChatId = cs.id
INNER JOIN dbo.Roles r ON c.RoleID = r.id
WHERE c.isActive = 1 and c.ChatId = @ChatRoomID
ORDER BY cs.Name ASC
END
GO



HTH
Jasper Smith


Edited by - jasper_smith on 08/07/2002 16:29:34
Go to Top of Page

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-07 : 16:31:07
Thanks guys I'll give it a look.

Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-08-07 : 16:45:25
Be aware the top one I suggested won't use an index if one exists on ChatId whereas the second one will. There is a way to get the top one to use an index but it seems to have slipped my mind

HTH
Jasper Smith
Go to Top of Page

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-07 : 16:46:29
thanks

Go to Top of Page
   

- Advertisement -