Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
if u have two table Employee and Department.

tblEmp - EmpID, EmpName,DeptID are the fields
tblDepartment - DeptID,DeptName are the fields

Employee and Department table have the foriegn key relation.




select * from tblEmp e
Inner join tblDepartment d on d.DeptID = e.DeptID
where d.DeptID IN ('1','2')

How to write stored procedure for above statement?
The department id values in where clause IN statement are dynamic (1,2) or (1,2,3) what ever i will pass them dynamically.

Thanks Advance..
Posted
Comments
Herman<T>.Instance 7-Sep-15 7:51am    
I created a Table with one GUID value for the corresponding rows in the request. In your Sp you add that GUID to the call and in the JOIN you could check on the selected rows in that extra table based on the GUID given.

1 solution

IN[^] clause can have a subquery or an expression to check for...
The expression can not be created dynamically, so the only option is to use a subquery...
Create a table variable from your input values and write something like this:
SQL
WHERE d.DeptID IN (SELECT fieldToCheck from @temp_table_var) 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900