Using multiple IF ELSE in A multi-statement TVF Sql Server

时间:2016-04-15 15:02:31

标签: sql-server sql-server-2008 user-defined-functions sql-function

How can I use IF ELSE statements inside A multi-statement TVF ? My Cide goes like this

CREATE FUNCTION Production.ms_tvf_ProductCostDifference
(
@ID INT ,

)
     RETURNS @retCostDifference TABLE
     (
      ProductId INT ,
      CostDifference MONEY
    )
    AS
    BEGIN

    With ABC as 
     ( Select ------
     )
    if @ID ='1'
    //some code using ABC defined
    ELSe IF @ID=2
    //Somecode
    Return;
    END

How should be the flow?

1 个答案:

答案 0 :(得分:0)

You cant use IF inside a WITH

instead you need something like this

  if @ID ='1'
    With ABC as 
         ( Select ------
         )
        //some code using ABC defined
  ELSE IF @ID=2
      With ABC as 
         ( Select ------
         )
      //Somecode
  Return;