1

我的存储过程中有一个 partition-by 函数,它从以前的 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
4

1 回答 1

-1

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

;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
于 2014-09-11T03:54:40.503 回答