fac*_*cot 3 sql function sql-server-2008
我需要一个根据参数值返回不同选择语句的函数。我像下面这样写,但它抛出了一个错误
在此上下文中不能使用带有返回值的 RETURN 语句。
ALTER FUNCTION [dbo].[Sample] (@SampleValue int)
RETURNS TABLE
AS
BEGIN
IF @SampleValue=100
RETURN(
SELECT ....
)
ELSE
RETURN(
SELECT ....
)
Run Code Online (Sandbox Code Playgroud)
根据您的评论 - 只需将其写为单个查询 - 无需将其写出两次。
ALTER FUNCTION [dbo].[Sample] (@SampleValue int)
RETURNS TABLE
AS
BEGIN
RETURN(
SELECT .... WHERE @SampleValue = 100 OR (<rest of where clause from other branch>)
)
Run Code Online (Sandbox Code Playgroud)