?
Solved

How can I use exist in store procedure?

Posted on 2011-04-26
4
Medium Priority
?
254 Views
Last Modified: 2012-05-11
This is part of the store procedure which is giving me an error because I am using more than one SELECT statements after EXISTS. How can I fix this error.  


DECLARE @ActualStorageLocation BIGINT
      
      SELECT top(1) @ActualStorageLocation = SL.Id
      FROM [WMS.Storage].[StorageLocation] SL
            JOIN
            [WMS.Storage].StoragePosition SP
            ON SL.Id = SP.Id_StorageLocation
      WHERE SL.Name = @FromLocation
      
      IF (@ActualStorageLocation IS NULL)
      BEGIN
            --SEND ERROR CODE " " LOCATION or NAME not found in [WMS.Storage.StorageLocation]
            SET @ResultErrorCode = 'CA'            
      END
      ELSE
      BEGIN
            --Check if the Ticket Number exist in E80 Inventory
            DECLARE @ActualStackUnit NVARCHAR(255) = NULL
            
            SELECT top(1) @ActualStackUnit = S.ID
            FROM WMS_Kraft.StockUnit SU
                  JOIN
                  [WMS.Inventory].StockUnit S
                  ON SU.ID = S.Id
            WHERE SU.TicketNumber = @TicketNumber   --OR  S.LPN = @TicketNumber
            AND S.Deleted = 0
            AND S.Destroyed = 0
            AND EXISTS (SELECT * FROM [WMS.Storage].StockUnitPosition SUP
                              WHERE SU.ID = SUP.Id_StockUnit
                              AND SUP.Id_StoragePosition
                              IN (SELECT * FROM [WMS.Storage].StoragePosition SP
                                    JOIN
                                     [WMS.Storage].StorageLocation SL
                                    ON SL.Id = SP.Id_StorageLocation
                              WHERE Id_StorageLocation = SL.Id))
            IF (@ActualStackUnit IS NULL)
            BEGIN
                  --SEND ERROR CODE " " TICKET NUMBER or LPN not found in [WMS.Inventory.StockUnit]
                  SET @ResultErrorCode = 'Not_LPN'
            END
            ELSE
            BEGIN
                  --DELETE THE INFORMATION FROM TABLES: [WMS.Inventory.StockUnit] & [WMS_Kraft.StockUnit]
                  --Where the ticket number is the same and also the ID
                        
                  SELECT @counter = @@ROWCOUNT, @error = @@ERROR
                  SET @ResultCode =      @counter;
                  SET @ResultMsg      =      'Location has being Cleared';
            END
      END
      
      
      
0
Comment
Question by:iscivanomar
4 Comments
 
LVL 9

Expert Comment

by:radcaesar
ID: 35468512
exists (select col1 from table1);
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 800 total points
ID: 35468523

When using IN, specify a field name, dont use *


      DECLARE @ActualStorageLocation BIGINT
     
      SELECT top(1) @ActualStorageLocation = SL.Id
      FROM [WMS.Storage].[StorageLocation] SL
            JOIN
            [WMS.Storage].StoragePosition SP
            ON SL.Id = SP.Id_StorageLocation
      WHERE SL.Name = @FromLocation
     
      IF (@ActualStorageLocation IS NULL)
      BEGIN
            --SEND ERROR CODE " " LOCATION or NAME not found in [WMS.Storage.StorageLocation]
            SET @ResultErrorCode = 'CA'            
      END
      ELSE
      BEGIN
            --Check if the Ticket Number exist in E80 Inventory
            DECLARE @ActualStackUnit NVARCHAR(255) = NULL
           
            SELECT top(1) @ActualStackUnit = S.ID
            FROM WMS_Kraft.StockUnit SU
                  JOIN
                  [WMS.Inventory].StockUnit S
                  ON SU.ID = S.Id
            WHERE SU.TicketNumber = @TicketNumber   --OR  S.LPN = @TicketNumber
            AND S.Deleted = 0
            AND S.Destroyed = 0
            AND EXISTS (SELECT 1 FROM [WMS.Storage].StockUnitPosition SUP
                              WHERE SU.ID = SUP.Id_StockUnit
                              AND SUP.Id_StoragePosition
                              IN (SELECT Id_StoragePosition FROM [WMS.Storage].StoragePosition SP
                                    JOIN
                                     [WMS.Storage].StorageLocation SL
                                    ON SL.Id = SP.Id_StorageLocation
                              WHERE Id_StorageLocation = SL.Id))
            IF (@ActualStackUnit IS NULL)
            BEGIN
                  --SEND ERROR CODE " " TICKET NUMBER or LPN not found in [WMS.Inventory.StockUnit]
                  SET @ResultErrorCode = 'Not_LPN'
            END
            ELSE
            BEGIN
                  --DELETE THE INFORMATION FROM TABLES: [WMS.Inventory.StockUnit] & [WMS_Kraft.StockUnit]
                  --Where the ticket number is the same and also the ID
                       
                  SELECT @counter = @@ROWCOUNT, @error = @@ERROR
                  SET @ResultCode =      @counter;
                  SET @ResultMsg      =      'Location has being Cleared';
            END
      END
0
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 200 total points
ID: 35473793
There is no problem due to multiple Select after Exist. It may be the other issue.
0
 

Author Closing Comment

by:iscivanomar
ID: 35474472
Thank you for your help.
Have a good day.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question