我想从带有WHERE IN
子句的表中获取记录,如下所示:
Select *
From Table_A
Where table_id IN (Query X that returns multiple or single id)
现在我想将X查询移动到SQL Server函数,因为它将使用多个位置并希望像这样调用查询:
Select *
From Table_A
Where table_id IN dbo.function(parameters)
有可能这样做吗?如果是,则哪个是函数类型(标量或表函数)?
答案 0 :(得分:3)
我认为你需要传递
CREATE FUNCTION function_name(@inputparams ...)
RETURNS TABLE
AS
RETURN SELECT id FROM table WHERE @inputparams...
go
select *
FROM table
WHERE table_id IN ( SELECT column_name FROM function_name(inputparametes) )
答案 1 :(得分:0)
试试这个:
CREATE FUNCTION fncName (@params ...)
RETURNS TABLE
AS
RETURN SELECT id FROM tableName WHERE @params...
go
SELECT * FROM query WHERE id IN (SELECT id FROM fncName(@params...))