过度的错误语法和临时表的分区

时间:2014-09-11 02:26:56

标签: sql sql-server stored-procedures partition-by

我的存储过程中有一个分区依据函数,它从前一个分区依据函数中调用临时表。我用前面的分区按函数创建的表是#tempVehicleManifestRow,而这又是我按功能调用下一个分区的表。它会发生错误

  

"'附近的语法不正确#tempVehicleManifestRow'"

为什么会这样?难道我甚至在实际选择之前就已经生成了一个包含所需数据的临时表吗?

我已经使用我使用的函数附加在分区下面。

这是我按函数划分的第一个分区:

;WITH A AS
    (
        SELECT ROW_NUMBER() OVER(ORDER BY
                CASE
                    when @pOrderby = 'SortByGender' then T.Gender
                    when @pOrderby = 'SortByCost' then T.colCostCenterCodeVarchar
                    when @pOrderby = 'SortByPickupDate' then cast(T.colPickUpDate as varchar(20))
                    when @pOrderby = 'SortByLastName' then T.LastName
                    when @pOrderby = 'SortByFirstName' then T.FirstName
                    when @pOrderby = 'SortByEmployeeID' then cast(T.colSeafarerIdInt as varchar(20))

                    when @pOrderby = 'SortByShip' then T.VesselName
                    when @pOrderby = 'SortByTitle' then T.RankName
                    when @pOrderby = 'SortByRouteFrom' then T.RouteFrom
                    when @pOrderby = 'SortByRouteTo' then T.RouteTo

                    when @pOrderby = 'SortByFromCity' then T.colFromVarchar
                    when @pOrderby = 'SortByToCity' then T.colToVarchar

                    when @pOrderby = 'SortByVehicleTypeName' then T.VehicleTypeName
                    when @pOrderby = 'SortByStatus' then T.VehicleTypeName
                    when @pOrderby = 'SortByCostCenter' then T.colCostCenterCodeVarchar
                    when @pOrderby = 'SortByNationality' then T.Nationality

                    when @pOrderby = 'SortByVehicleVendor' then T.VehicleVendorname

                    when @pOrderby = 'SortByRecordLocator' then T.colRecordLocatorVarchar
                    when @pOrderby = 'SortByOnOffdate' then cast(T.colOnOffDate as varchar(20))
                    when @pOrderby = 'SortByPickupTime' then cast(T.colPickUpTime as varchar(20))
                    when @pOrderby = 'SortByOnOff' then T.colSFStatus
                    when @pOrderby = 'SortByHotel' then T.HotelVendorName
                    ELSE
                           T. VehicleVendorname
            END ,
                CASE WHEN @pOrderby = 'SortByPickupDate' then cast(T.colPickUpTime as varchar(20)) 
                    ELSE T.FirstName
                END
         ) AS xRow, 

         * FROM #tempVehicleManifest T
    ) SELECT * INTO #tempVehicleManifestRow FROM A ORDER BY A.xRow

这是有问题的分区:

;WITH CC AS
(
     SELECT ROW_NUMBER() OVER(PARTITION BY CC.colRecordLocatorVarchar, CC.colSeafarerIdInt,
    CC.colOnOffVarchar , CC.colVehicleVendorIDInt 
    ORDER BY CC.colTagIDInt DESC) xRow, CC.*   
 FROM (
  SELECT distinct
        A.xRow, 
        A.colTransVehicleIDBigint, 
        A.colSeafarerIdInt, A.LastName, A.FirstName, 
        A.colIdBigint, A.colTravelReqIDInt, 
        A.colRecordLocatorVarchar, A.colOnOffDate, 
        A.colRequestIDInt, A.colVehicleVendorIDInt,
        A.VehicleVendorname, A.colVehiclePlateNoVarchar, 
        A.colPickUpDate, A.colPickUpTime, 
        A.colDropOffDate, A.colDropOffTime, 
        A.colConfirmationNoVarchar, A.colVehicleStatusVarchar, 
        A.colVehicleTypeIdInt, A.VehicleTypeName, A.colSFStatus, 
        A.colRouteIDFromInt, A.RouteFrom, A.colRouteIDToInt, A.RouteTo, 
        A.colFromVarchar, A.colToVarchar, A.colRemarksForAuditVarchar, 
        A.colHotelIDInt, A.HotelVendorName, A.colRankIDInt, A.RankName, 
        A.colCostCenterIDInt, A.colCostCenterCodeVarchar, 
        Nationality = RTRIM(LTRIM(N.colNationalityCodeVarchar)) + '-' + RTRIM(LTRIM(N.colNationalityDescriptionVarchar)),
        A.colIsVisibleBit, 
        A.colContractIdInt, A.Gender, A.colVesselIdInt, 
        A.VesselName, UserID = @pUserID, A.colSeqNoInt
        , A.colDriverIDInt
        --, A.colIsNoVehicleNeeded  
        , A.colVehicleDispatchTime
        , FlightNo =A.colFlightNoVarchar
        , Carrier = A.colMarketingAirlineCodeVarchar    

        , Departure = A.colDepartureAirportLocationCodeVarchar
        , Arrival = A.colArrivalAirportLocationCodeVarchar
        , DeptDate = A.colDepartureDateTime
        , ArrDate = A.colArrivalDateTime
        , PA.PassportNo
        , PA.PassportExp
        , PA.PassportIssued
        , BR.Birthday
        , ISNULL(CC.colIsActiveBit,0) as colIsActiveBitTagged
        , CC.colCreatedByVarchar as createdUserTag
        , CC.colModifiedByVarchar as modifiedUserTag
        , CC.colVehicleVendorIDInt as taggedVehicleVendorId
        #tempVehicleManifestRow A 
        LEFT JOIN TblVehicleManifestConfirmed B ON
                A.colSeafarerIdInt = B.colSeafarerIdInt AND
                A.colVehicleVendorIDInt = B.colVehicleVendorIDInt AND
                A.colPickUpDate = B.colPickUpDate AND
                ISNULL(A.colRecordLocatorVarchar,'') = ISNULL(B.colRecordLocatorVarchar,'')
                AND A.colRouteIDFromInt = B.colRouteIDFromInt
                AND A.colRouteIDToInt = B.colRouteIDToInt
        --not visible to vendor but not realy cancelled
        LEFT JOIN TblVehicleManifestConfirmed Hide ON
                A.colSeafarerIdInt = Hide.colSeafarerIdInt AND
                A.colVehicleVendorIDInt = Hide.colVehicleVendorIDInt AND
                A.colPickUpDate = Hide.colPickUpDate AND
                ISNULL(A.colRecordLocatorVarchar,'') = ISNULL(Hide.colRecordLocatorVarchar,'') AND          
                ISNULL(Hide.colIsVisibleBit,1) = 0
                AND A.colRouteIDFromInt = B.colRouteIDFromInt
                AND A.colRouteIDToInt = B.colRouteIDToInt
                --added new table
            LEFT JOIN TblTag_Vehicle CC ON B.colIdBigint = CC.colIdBigint AND
                B.colTravelReqIDInt = CC.colTravelReqIDInt AND B.colSeafarerIdInt = CC.colSeafarerIdInt             
                --end new added table
        LEFT JOIN dbo.TblVehiclePlates VP ON VP.colPlateID = B.colVehiclePlateNoVarchar
        LEFT JOIN dbo.TblSeafarer S ON S.colSeafarerIdInt = A.colSeafarerIdInt      
        LEFT JOIN TblNationality N ON N.colNatioalityIdInt = S.colNationalityIDInt
        LEFT JOIN #TempPassport PA ON A.colSeafarerIdInt = PA.SeafarerId
        LEFT JOIN tmRemarks_Birthday BR ON BR.FK_ItineraryRefID = A.colRecordLocatorVarchar
        JOIN #tempVehicleVendor VE ON VE.colVehicleVendorIDInt = A.colVehicleVendorIDInt
    WHERE 
        (   B.colConfirmedManifestIDBigint IS NULL  
            OR 
            Hide.colTransVehicleIDBigint IS NOT NULL
        )
    ORDER BY A.xRow
 )
) SELECT * INTO #tempManifestNew

1 个答案:

答案 0 :(得分:-1)

您在#tempVehicleManifestRow A附近的此查询中错过了 FROM CLAUSE 。这是正确的查询。

;WITH CC
AS (
    SELECT ROW_NUMBER() OVER (
            PARTITION BY CC.colRecordLocatorVarchar
            ,CC.colSeafarerIdInt
            ,CC.colOnOffVarchar
            ,CC.colVehicleVendorIDInt ORDER BY CC.colTagIDInt DESC
            ) xRow
        ,CC.*
    FROM (
        SELECT DISTINCT A.xRow
            ,A.colTransVehicleIDBigint
            ,A.colSeafarerIdInt
            ,A.LastName
            ,A.FirstName
            ,A.colIdBigint
            ,A.colTravelReqIDInt
            ,A.colRecordLocatorVarchar
            ,A.colOnOffDate
            ,A.colRequestIDInt
            ,A.colVehicleVendorIDInt
            ,A.VehicleVendorname
            ,A.colVehiclePlateNoVarchar
            ,A.colPickUpDate
            ,A.colPickUpTime
            ,A.colDropOffDate
            ,A.colDropOffTime
            ,A.colConfirmationNoVarchar
            ,A.colVehicleStatusVarchar
            ,A.colVehicleTypeIdInt
            ,A.VehicleTypeName
            ,A.colSFStatus
            ,A.colRouteIDFromInt
            ,A.RouteFrom
            ,A.colRouteIDToInt
            ,A.RouteTo
            ,A.colFromVarchar
            ,A.colToVarchar
            ,A.colRemarksForAuditVarchar
            ,A.colHotelIDInt
            ,A.HotelVendorName
            ,A.colRankIDInt
            ,A.RankName
            ,A.colCostCenterIDInt
            ,A.colCostCenterCodeVarchar
            ,Nationality = RTRIM(LTRIM(N.colNationalityCodeVarchar)) + '-' + RTRIM(LTRIM(N.colNationalityDescriptionVarchar))
            ,A.colIsVisibleBit
            ,A.colContractIdInt
            ,A.Gender
            ,A.colVesselIdInt
            ,A.VesselName
            ,UserID = @pUserID
            ,A.colSeqNoInt
            ,A.colDriverIDInt
            --, A.colIsNoVehicleNeeded  
            ,A.colVehicleDispatchTime
            ,FlightNo = A.colFlightNoVarchar
            ,Carrier = A.colMarketingAirlineCodeVarchar
            ,Departure = A.colDepartureAirportLocationCodeVarchar
            ,Arrival = A.colArrivalAirportLocationCodeVarchar
            ,DeptDate = A.colDepartureDateTime
            ,ArrDate = A.colArrivalDateTime
            ,PA.PassportNo
            ,PA.PassportExp
            ,PA.PassportIssued
            ,BR.Birthday
            ,ISNULL(CC.colIsActiveBit, 0) AS colIsActiveBitTagged
            ,CC.colCreatedByVarchar AS createdUserTag
            ,CC.colModifiedByVarchar AS modifiedUserTag
            ,CC.colVehicleVendorIDInt AS taggedVehicleVendorId
        -- HERE YOU HAVE MISSED FROM CLAUSE 
        FROM #tempVehicleManifestRow A
        LEFT JOIN TblVehicleManifestConfirmed B ON A.colSeafarerIdInt = B.colSeafarerIdInt
            AND A.colVehicleVendorIDInt = B.colVehicleVendorIDInt
            AND A.colPickUpDate = B.colPickUpDate
            AND ISNULL(A.colRecordLocatorVarchar, '') = ISNULL(B.colRecordLocatorVarchar, '')
            AND A.colRouteIDFromInt = B.colRouteIDFromInt
            AND A.colRouteIDToInt = B.colRouteIDToInt
        --not visible to vendor but not realy cancelled
        LEFT JOIN TblVehicleManifestConfirmed Hide ON A.colSeafarerIdInt = Hide.colSeafarerIdInt
            AND A.colVehicleVendorIDInt = Hide.colVehicleVendorIDInt
            AND A.colPickUpDate = Hide.colPickUpDate
            AND ISNULL(A.colRecordLocatorVarchar, '') = ISNULL(Hide.colRecordLocatorVarchar, '')
            AND ISNULL(Hide.colIsVisibleBit, 1) = 0
            AND A.colRouteIDFromInt = B.colRouteIDFromInt
            AND A.colRouteIDToInt = B.colRouteIDToInt
        --added new table
        LEFT JOIN TblTag_Vehicle CC ON B.colIdBigint = CC.colIdBigint
            AND B.colTravelReqIDInt = CC.colTravelReqIDInt
            AND B.colSeafarerIdInt = CC.colSeafarerIdInt
        --end new added table
        LEFT JOIN dbo.TblVehiclePlates VP ON VP.colPlateID = B.colVehiclePlateNoVarchar
        LEFT JOIN dbo.TblSeafarer S ON S.colSeafarerIdInt = A.colSeafarerIdInt
        LEFT JOIN TblNationality N ON N.colNatioalityIdInt = S.colNationalityIDInt
        LEFT JOIN #TempPassport PA ON A.colSeafarerIdInt = PA.SeafarerId
        LEFT JOIN tmRemarks_Birthday BR ON BR.FK_ItineraryRefID = A.colRecordLocatorVarchar
        JOIN #tempVehicleVendor VE ON VE.colVehicleVendorIDInt = A.colVehicleVendorIDInt
        WHERE (
                B.colConfirmedManifestIDBigint IS NULL
                OR Hide.colTransVehicleIDBigint IS NOT NULL
                )
        ) AS x -- missing alias declaration, required in TSQL
    )
SELECT *
INTO #tempManifestNew