使用带有多个值的IN子句的SQL存储过程 [英] SQL Stored Procedure for using IN clause with multiple values

查看:221
本文介绍了使用带有多个值的IN子句的SQL存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有两个表Employee和Department。



tblEmp - EmpID,EmpName,DeptID是字段

tblDepartment - DeptID, DeptName是字段



员工和部门表格具有foriegn密钥关系。









select * from tblEmp e

内部联接tblDepartment d on d.DeptID = e.DeptID

其中d.DeptID IN('1','2')



如何为上述声明编写存储过程?

where子句IN语句中的部门id值是动态的(1,2)或(1,2,3)我将动态传递它们。



谢谢提前..

解决方案

IN [ ^ ]子句可以有子查询或要检查的表达式......

表达式不能动态创建,因此唯一的选择是使用子查询...

从输入值创建表变量并写下这样的内容:

  WHERE  d.DeptID  IN  SELECT  fieldToCheck 来自  @ temp_table_var 


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..

解决方案

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:

WHERE d.DeptID IN (SELECT fieldToCheck from @temp_table_var) 


这篇关于使用带有多个值的IN子句的SQL存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆