Question : SQL Query - Simple Syntax Issue


I need to write 2 different query by using IF Else Condition

If (Select * from Employee Where EmpId = 1) Returns Value or Not Equal to EMPTY

Proceed :- Select * from Employee Where EmpId = 1

Else if (Select * from Employee Where EmpId = 1) = EMPTY or NOT returning any value

Then Return

Select * from TemporaryEmployee Where EmpId = 1

OBJECTIVE :- We need to return atleast a  ROW  - Based on Row Return

There can be a EMPTY ROW - If there is a Empty Row - Then we need to write a different SQL Select Statement.

How to perform this syntax ?

Answer : SQL Query - Simple Syntax Issue

I usally "fine-tune" that like this, presuming you need the "same" columns :

DECLARE @res TABLE ( empID int, empname varchar(100) ) --- more columns as needed
SET NOCOUNT ON
INSERT INTO @res  (empID, empname)
SELECT empID, empName
  FROM Employee
 WHERE empid = 1
IF @@ROWCOUNT = 0
BEGIN
 INSERT INTO @res  (empID, empname)
 SELECT empID, empName
   FROM TemporaryEmployee
  WHERE empid = 1
END
-- return the final results
SELECT *
  FROM @res

 
Random Solutions  
 
programming4us programming4us