troubleshooting Question

How to have to AND NOT EXIST() in one statement?

Avatar of iscivanomar
iscivanomarFlag for United States of America asked on
Microsoft SQL Server 2005Microsoft SQL Server 2008
8 Comments2 Solutions383 ViewsLast Modified:
This is the statement but I think has an error or

SELECT      TOP 1
                              @FoundStoragePositionID              =      SP.Id,
                              @FoundStorageLocationID              =      SP.Id_StorageLocation,
                              @FoundInternalLevel                    =      ISNULL( MAX( ISFP.InternalLevel ), 0 ) + 1,                                    
                              @minLogicalOrderInStagingLane = SHGL.MinLogicalOrder,
                              @maxLogicalOrderInStagingLane = SHGL.MaxLogicalOrder,
                              @diffBetweenFoundAndExpSeqNum =      ( sp.Position - 1 + SHGL.MinLogicalOrder - @actLogicalOrder ),
                              @tempClearance                              =      ISNULL( sp.clearence, sl.clearance)
                  FROM      [WMS.Storage].StorageLocation SL       
                  JOIN      [WMS.Storage].StoragePosition SP
                  ON            SP.Id_StorageLocation = SL.Id                                                                  
                  JOIN      [WMS.Shipping].ShipmentStagingLane SHGL
                  ON            SL.Id = SHGL.Id_StorageLocation
                  LEFT JOIN      @InSequencePosForProcessedStockunits ISFP
                  ON            SP.Id = ISFP.IdStoragePosition
                  LEFT OUTER JOIN @StorageLocationToExclude SLTE
                  ON            SL.Id = SLTE.Id_Location
                  LEFT OUTER JOIN @OutOfSequencePosForProcessedStockunits OSFP
                  ON            SP.Id = OSFP.IdStoragePosition
                  WHERE      ( SHGL.Id_Shipment = @idShipment)
                  AND            SLTE.Id_Location IS NULL
                  AND            OSFP.IdStoragePosition IS NULL
                  AND            SHGL.MinLogicalOrder <= @actLogicalOrder
                  AND            SHGL.MaxLogicalOrder >= @actLogicalOrder
                  --AND            ( SP.Position = ( @actLogicalOrder - SHGL.MinLogicalOrder + 1 ) )                  
                  AND            ISNULL(SL.StorageDisabled_LGV,0)      =      0
                  AND            ISNULL(SP.StorageBlocked,0)                  =      0
                  AND            ISNULL(SP.StorageDisabled,0)            =      0                                                      
                  AND            (
                              NOT EXISTS (
                                    SELECT      SUPI.ID
                                    FROM      [WMS.Storage].StoragePosition  SPI
                                    LEFT JOIN      [WMS.Storage].StockUnitPosition SUPI
                                    ON            SUPI.Id_StoragePosition = SPI.Id
                                    WHERE      SPI.Id_StorageLocation = SP.Id_StorageLocation
                                    AND            ((SPI.Position = SP.Position      AND      (SPI.StorageBlocked <> 0  OR SPI.StorageDisabled <> 0))
                                                OR
                                                      (
                                                      SPI.Position > SP.Position
                                                            AND
                                                            (
                                                                  SPI.StorageBlocked <> 0
                                                                  OR
                                                                  ( ( NOT SUPI.Id IS NULL ) AND ISNULL( SUPI.BOOKING, 0) = 0 )
                                                            )                                                                                                            
                                                      )
                                                )
                                          )
                              )                                                            
                  ---            The Position must be physically empty or booked for stockunit in stack. ---       

--HERE IS SEYING THAT THERE IS AN INCORRECT SYNTAX NEAR THE KEYWORD 'AND'

                  
                  AND            NOT EXISTS (
                              SELECT      SUPE.ID
                              FROM      [WMS.Storage].StockUnitPosition SUPE
                              LEFT JOIN @StockUnitsInStack SUS
                              ON            SUPE.Id_StockUnit = SUS.IdStockUnit
                              WHERE      SUPE.Id_StoragePosition = sp.Id
                              AND            (
                                                ISNULL( SUPE.Booking, 0)      =      0
                                                OR
                                                (
                                                      (
                                                      SL.Id <> @IdStorageLocationToWhereLGVInFront
                                                      OR
                                                      (
                                                            (
                                                            SL.Id = @IdStorageLocationToWhereLGVInFront
                                                            OR
                                                            @IdStorageLocationToWhereLGVInFront IS NULL
                                                            )
                                                            AND
                                                            ISNULL( @ConsiderOnlyPhysicallyBusyPositions , 0 ) = 0
                                                      )
                                                )                                          
                                                AND
                                                SUPE.Booking <> 0
                                                AND
                                                SUS.IdStockUnit IS NULL
                                                )                                          
                                          )
                                    )
                  AND            (
                              @stockUnitHeight <= ISNULL( sp.clearence, sl.clearance)
                              OR
                              ISNULL( @CheckClearence, 0 ) = 0
                              )            
                  GROUP BY SP.Id, SP.Id_StorageLocation, SHGL.MinLogicalOrder, SHGL.MaxLogicalOrder, SP.Position, SP.Clearence, SL.Clearance
                  ORDER BY SP.Position ASC
ASKER CERTIFIED SOLUTION
TempDBA

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros