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)
 if record does not exist select all

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-01-21 : 08:50:33
Molly writes "Is there anyway to write a select statement that you can specify in the where clause (or somewhere else) that is a if a condition is not met then all records are returned.

SELECT *
FROM dbo.Employees
WHERE EmployeeID = 1

but if there is no EmployeeID equal to 1 then all records are returned. I am using it in a modelling program and cannot use a stored procedure. "

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-01-21 : 08:54:44
This sounds like a pretty horrible way of doing things, but I'll bit.


Declare @EmpID int

SET @EmpID = 1

SELECT *
FROM dbo.Employees
WHERE
EmployeeID = CASE
WHEN (EXISTS(SELECT 1 FROM dbo.Employees WHERE EmployeeID = @EmpID)) THEN @EmpID
ELSE EmployeeID END






Damian
Go to Top of Page
   

- Advertisement -