SQL Server函数根据参数返回不同的select语句

时间:2012-03-23 14:19:14

标签: sql sql-server-2008 function

我需要一个函数,它根据参数的值返回不同的select语句。我写的如下,但它会抛出像

这样的错误
  

具有返回值的RETURN语句不能在此使用   上下文。

ALTER FUNCTION [dbo].[Sample] (@SampleValue int)
RETURNS TABLE
AS
BEGIN
IF @SampleValue=100
RETURN(
 SELECT ....
)
ELSE
RETURN(
 SELECT ....

)

1 个答案:

答案 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>)
)