在多语句 TVF Sql 服务器中使用多个 IF ELSE

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

如何在多语句 TVF 中使用 IF ELSE 语句?我的代码是这样的

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

流量应该如何?

你不能在 WITH

中使用 IF

相反,你需要这样的东西

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