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
 Transact-SQL (2000)
 Dynamic WHERE

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-02-27 : 12:09:39
Arnold writes "Hi

I have a Stored Procedure like this

CREATE PROCEDURE ShowUsers
@Department int,@OnlyActive bit
AS
IF(@OnlyActive=1)
BEGIN
SELECT * FORM USERS WHERE IDDepartment=@Department
AND Active=1
END
ELSE
BEGIN
SELECT * FROM USERS WHERE IDDepartment=@Department
END

Is it possible to do this in one line - something like:

SELECT * FORM USERS WHERE IDDepartment=@Department
IF(@OnlyActive=1)
AND Active=1

I tried to put the Statement into an variable and then EXECUTE(@varible) but i get "@department must be declared"

Is it possible or do i have to create two SQL-Statements?

Hope you can help me

Thanks
Arnold"

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-27 : 13:08:50
You can achieve a dynamic WHERE clause if you use COALESCE. Check out this article:
http://www.sqlteam.com/item.asp?ItemID=2077

Tara Kizer
aka tduggan
Go to Top of Page
   

- Advertisement -