简体   繁体   English

如何在SQL的where子句中调用存储过程

[英]how to call a stored procedure in where clause of SQL

I have a T-SQL script, requirment is I need to call a stored procedure in where clause. 我有一个T-SQL脚本,要求是我需要在where子句中调用存储过程。 This stored procedure accept a parameter and returns a bit result. 此存储过程接受参数并返回位结果。 Kindly guide me how to do it. 请指导我如何做到这一点。

Thanks 谢谢

Edit: I cant modify this sp and make it a function. 编辑:我不能修改这个sp并使其成为一个函数。 please

You can not use Stored Procedure in where clause but can use User Defined Function in where clause. 您不能在where子句中使用Stored Procedure ,但可以在where子句中使用User Defined Function

If you cant convert SP to function then you have to first get bit value from executing SP and use that variable in where clause.. 如果你不能将SP转换为函数,那么你必须首先从执行SP获取位值并在where子句中使用该变量。

You can use temporary table to store output of stored procedure and use it in where clause. 您可以使用临时表来存储存储过程的输出,并在where子句中使用它。 The number of columns in your temporary variables must be same as that in resultset from procedure and with exact datatype as of columns in stored procedure resultset. 临时变量中的列数必须与过程的结果集中的列数相同,并且与存储过程结果集中的列的精确数据类型相同。 eg, 例如,

create table #spResult ({columns as result of your sp})

insert into #spResult exec YourSP ({input parameters})

select * from yourtable 
where col in (select col from #spResult) 

drop table #spResult 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM