2017-03-16 100 views
2

我嘗試從數函數收集我的數據,並把它的變量表調用內部進行@GetAllRep,然後添加一個條件,如必須聲明標量變量在SQLSERVER

WHERE @GetAllRep.UserId IN (SELECT @tableSubset.userId FROM @tableSubset) 

用於過濾我的數據用戶ID場,但給我這個錯誤:

必須聲明標量變量「@GetAllRep」。

必須聲明標量變量「@tableSubset」。

ALTER PROCEDURE [dbo].[ProceCompleteReportB2B] 
(
    @startdate  DATETIME, 
    @enddate  DATETIME, 
    @top   INT, 
    @state   INT, 
    @type   INT, 
    @subset  NVARCHAR(15), 
    @oneSubset  NVARCHAR(128), 
    @userId  NVARCHAR(128) 
) 
AS 
BEGIN 
    DECLARE @GetAllRep TABLE 
      (
       [Id] [int], 
       [factorno][int], 
       [PayType][bit], 
       [ReserveNumber][int], 
       [ReserveState][int], 
       [state][nvarchar](50), 
       [Price][nvarchar](50), 
       [ReserveType][nvarchar](50), 
       [ObjectIdDepartue][int], 
       [IssueDate][nvarchar](50), 
       [BankId][int], 
       [Confirmed][bit], 
       [TrackingCode][nvarchar](50), 
       [Transactionsuccess][nvarchar](50), 
       [Name][nvarchar](128), 
       [TiketUrl][nvarchar](128), 
       [ObjectIdReturn] [int] NULL, 
       [TelNumber][nvarchar](50) NULL, 
       [UserId][nvarchar](128) 
      ) 

    IF (@type = 0) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncFlightReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncTrainReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncCharterReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncBusReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncInsuranceReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncCarReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncHotelReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncIFlightReportB2B](@startdate, @enddate, @top, @state) 
     UNION 
     SELECT * 
     FROM dbo.[FuncMassageReportB2B](@startdate, @enddate, @top, @state) 
    ------------------------------------------------------------------- 
    IF (@type = 1) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncFlightReportB2B](@startdate, @enddate, @top, @state) 
       ----------------------------------------------------------------------- 
    ELSE 
    IF (@type = 2) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncTrainReportB2B](@startdate, @enddate, @top, @state) 
    ----------------------------------------------------------------------- 
    IF (@type = 3) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncCharterReportB2B](@startdate, @enddate, @top, @state) 
       --------------------------------------------------------------------- 
    ELSE 
    IF (@type = 4) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncBusReportB2B](@startdate, @enddate, @top, @state) 
    --------------------------------------------------------------------- 
    IF (@type = 5) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncInsuranceReportB2B](@startdate, @enddate, @top, @state) 
       --------------------------------------------------------------------- 
    ELSE 
    IF (@type = 6) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncCarReportB2B](@startdate, @enddate, @top, @state) 
    --------------------------------------------------------------------- 
    IF (@type = 7) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncHotelReportB2B](@startdate, @enddate, @top, @state) 
       --------------------------------------------------------------------- 
    ELSE 
    IF (@type = 8) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncIFlightReportB2B](@startdate, @enddate, @top, @state) 
    ELSE 
    IF (@type = 9) 
     INSERT INTO @GetAllRep 
     SELECT * 
     FROM dbo.[FuncMassageReportB2B](@startdate, @enddate, @top, @state) 
    --------------------------------------------------------------------- 
    DECLARE @tableSubset TABLE ([userId] [nvarchar](128)) 
    INSERT INTO @tableSubset 
    SELECT * 
    FROM dbo.FuncGetSubsetUserIds(@subset, @oneSubset, @userId) AS SubsetUserIds 

    SELECT * 
    FROM @GetAllRep 
    WHERE @GetAllRep.UserId IN (SELECT @tableSubset.userId FROM @tableSubset) 
    ORDER BY 
      IssueDate DESC 
END 

如何解決這一問題?

回答

2

只需創建別名表變量,並在WHERE子句

SELECT * 
FROM @GetAllRep GetAllRep 
       ---^^^ 
WHERE GetAllRep.UserId IN (SELECT userId FROM @tableSubset t) 
ORDER BY 
     IssueDate DESC 
+0

確定這是正確的使用它。現在得到這個錯誤:必須聲明標量變量「@tableSubset」。 – programmer138200

+1

好吧,它解決了。非常感謝 – programmer138200