Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have this issue in T-Sql - a stored proc - GetEmpoyees, which takes in multiple employee_id's as a string, delimited by "," - a comma. Within the stored-proc an in inbuilt function - fn_split() is called for, splits the string and inserts them in a temp table @tblEmpID.the select statement that follows should take care of both the cases i.) if the empoyee_id string is nullii.)if employee_id string is not nullSELECT *FROM EMPLOYEESWHERE (CASE WHEN ((@empid IS NOT NULL AND LEN(LTRIM(RTRIM(@empid)))>0) THEN empid IN (select iEmpid from @tblEmpid)) ELSE empid = empid END)ORSELECT *FROM EMPLOYEESWHERE empid in (CASE WHEN ((@empid IS NOT NULL AND LEN(LTRIM(RTRIM(@empid)))>0) THEN (select iEmpid from @tblEmpid)) ELSE select empid END)i tried both the select statements above and they are throwing an error.Thanks a lot for the help
malpashaa
Constraint Violating Yak Guru
264 Posts
Posted - 2010-05-02 : 19:17:49
If I understand what you need then you can try one of these:
SELECT * FROM EMPLOYEES WHERE @empid IS NULL OR LEN(@empid) = 0 UNION ALLSELECT * FROM EMPLOYEES WHERE @empid IS NOT NULL AND LEN(@empid) > 0 AND EXISTS(SELECT * FROM @tblEmpid WHERE iEmpid = empid)
Or
IF @empid IS NULL OR LEN(@empid) = 0 SELECT * FROM EMPLOYEESELSE SELECT * FROM EMPLOYEES WHERE EXISTS(SELECT * FROM @tblEmpid WHERE iEmpid = empid)
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2010-05-03 : 01:35:39
[code]SELECT e.* FROM EMPLOYEES eLEFT JOIN @tblEmpid tON t.empid = e.empidWHERE t.empid IS NOT NULLOR LEN(@empid) = 0[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/