我需要一个函数,它根据参数的值返回不同的select语句。我写的如下,但它会抛出像
这样的错误具有返回值的RETURN语句不能在此使用 上下文。
ALTER FUNCTION [dbo].[Sample] (@SampleValue int)
RETURNS TABLE
AS
BEGIN
IF @SampleValue=100
RETURN(
SELECT ....
)
ELSE
RETURN(
SELECT ....
)
答案 0 :(得分:3)
根据您的评论 - 只需将其写为单个查询 - 无需将其写出两次。
ALTER FUNCTION [dbo].[Sample] (@SampleValue int)
RETURNS TABLE
AS
BEGIN
RETURN(
SELECT .... WHERE @SampleValue = 100 OR (<rest of where clause from other branch>)
)