Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sequence of events inside a stored procedure

Sequence of events inside a stored procedure

Scheduled Pinned Locked Moved Database
databasesharepointsysadminhostingtools
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    Hello everyone. Everything I have read says that operations within a stored procedure are executed in the order in which they are encountered, and the semi-colons and BEGIN-END blocks can be used to ensure that statements are executed before subsequent statements are performed. However I am stumped by what is happening in my script. The intent of the script is to copy an order record and all associated table entries for the order from one database to another. I have stripped down the script code below to just the order table itself (and removed some passwords, server names etc). Here is the code:

    IF @@SERVERNAME <> 'Server1'
    BEGIN
    EXEC SP_ADDLINKEDSERVER
    'Server1'

      EXEC SP\_ADDLINKEDSRVLOGIN
        'Server1',
        'False',
        NULL,
        'userX',
        'passwordX'
    

    END

    go

    DECLARE @mode VARCHAR(1);
    DECLARE @orderNum INT;

    SET @orderNum = 1338464;
    SET @mode = 'R' -- set to 'R' for Report or 'U' for update
    BEGIN TRANSACTION

    DECLARE @sourceName VARCHAR(8);
    DECLARE @okay CHAR(1);
    DECLARE @user VARCHAR(20);

    SET @sourceName = 'liveDB';
    SET @user = SYSTEM_USER;

    CREATE synonym sourcesummary FOR [Server1].[liveDB].[dbo].[order_summary];
    SET @okay = 'Y'

    IF @okay = 'Y'
    AND @@SERVERNAME = 'Server1'
    BEGIN
    SELECT 'Cannot execute on live server - retry on the sever hosting the db that is to be copied to' [error]

      SET @okay = 'N'
    

    END

    IF @okay = 'Y'
    AND NOT EXISTS (SELECT order_num
    FROM sourcesummary
    WHERE order_num = @orderNum)
    BEGIN
    SELECT 'Unable to find order' [error],@sourceName [source db],@orderNum [order number];

      SET @okay = 'N';
    

    END

    IF @okay = 'Y'
    BEGIN
    IF EXISTS (SELECT order_num
    FROM order_summary
    WHERE ordr_num = @orderNum)
    BEGIN
    PRINT 'deleting order_summary:';

            DELETE order\_summary
            WHERE  order\_num = @orderNum;
        END
    
      BEGIN
    
      PRINT 'copying order\_summary:';
          INSERT INTO order\_summary
          SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
          FROM   sourcesummary
          WHERE  sourcesummary.order\_num = @orderNum;
      END
    
      PRINT 'retreiving copied order\_summary:';
      SELECT 'copied' \[order\_summary\],\*
      FROM   order\_summary
      WHERE  order\_num = @orderNum;
    

    END

    DROP synonym sourcesummary;

    IF @mode = 'U'
    BEGIN
    COMMIT TRANSACTION

      SELECT 'Changes hav
    
    A P 2 Replies Last reply
    0
    • C Clive D Pottinger

      Hello everyone. Everything I have read says that operations within a stored procedure are executed in the order in which they are encountered, and the semi-colons and BEGIN-END blocks can be used to ensure that statements are executed before subsequent statements are performed. However I am stumped by what is happening in my script. The intent of the script is to copy an order record and all associated table entries for the order from one database to another. I have stripped down the script code below to just the order table itself (and removed some passwords, server names etc). Here is the code:

      IF @@SERVERNAME <> 'Server1'
      BEGIN
      EXEC SP_ADDLINKEDSERVER
      'Server1'

        EXEC SP\_ADDLINKEDSRVLOGIN
          'Server1',
          'False',
          NULL,
          'userX',
          'passwordX'
      

      END

      go

      DECLARE @mode VARCHAR(1);
      DECLARE @orderNum INT;

      SET @orderNum = 1338464;
      SET @mode = 'R' -- set to 'R' for Report or 'U' for update
      BEGIN TRANSACTION

      DECLARE @sourceName VARCHAR(8);
      DECLARE @okay CHAR(1);
      DECLARE @user VARCHAR(20);

      SET @sourceName = 'liveDB';
      SET @user = SYSTEM_USER;

      CREATE synonym sourcesummary FOR [Server1].[liveDB].[dbo].[order_summary];
      SET @okay = 'Y'

      IF @okay = 'Y'
      AND @@SERVERNAME = 'Server1'
      BEGIN
      SELECT 'Cannot execute on live server - retry on the sever hosting the db that is to be copied to' [error]

        SET @okay = 'N'
      

      END

      IF @okay = 'Y'
      AND NOT EXISTS (SELECT order_num
      FROM sourcesummary
      WHERE order_num = @orderNum)
      BEGIN
      SELECT 'Unable to find order' [error],@sourceName [source db],@orderNum [order number];

        SET @okay = 'N';
      

      END

      IF @okay = 'Y'
      BEGIN
      IF EXISTS (SELECT order_num
      FROM order_summary
      WHERE ordr_num = @orderNum)
      BEGIN
      PRINT 'deleting order_summary:';

              DELETE order\_summary
              WHERE  order\_num = @orderNum;
          END
      
        BEGIN
      
        PRINT 'copying order\_summary:';
            INSERT INTO order\_summary
            SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
            FROM   sourcesummary
            WHERE  sourcesummary.order\_num = @orderNum;
        END
      
        PRINT 'retreiving copied order\_summary:';
        SELECT 'copied' \[order\_summary\],\*
        FROM   order\_summary
        WHERE  order\_num = @orderNum;
      

      END

      DROP synonym sourcesummary;

      IF @mode = 'U'
      BEGIN
      COMMIT TRANSACTION

        SELECT 'Changes hav
      
      A Offline
      A Offline
      Andrei Straut
      wrote on last edited by
      #2

      Try committing the transaction after deleting and inserting the rows into the database, but before retrieving them, I'm assuming that's what causing the problem

      Full-fledged Java/.NET lover, full-fledged PHP hater. Full-fledged Google/Microsoft lover, full-fledged Apple hater. Full-fledged Skype lover, full-fledged YM hater.

      1 Reply Last reply
      0
      • C Clive D Pottinger

        Hello everyone. Everything I have read says that operations within a stored procedure are executed in the order in which they are encountered, and the semi-colons and BEGIN-END blocks can be used to ensure that statements are executed before subsequent statements are performed. However I am stumped by what is happening in my script. The intent of the script is to copy an order record and all associated table entries for the order from one database to another. I have stripped down the script code below to just the order table itself (and removed some passwords, server names etc). Here is the code:

        IF @@SERVERNAME <> 'Server1'
        BEGIN
        EXEC SP_ADDLINKEDSERVER
        'Server1'

          EXEC SP\_ADDLINKEDSRVLOGIN
            'Server1',
            'False',
            NULL,
            'userX',
            'passwordX'
        

        END

        go

        DECLARE @mode VARCHAR(1);
        DECLARE @orderNum INT;

        SET @orderNum = 1338464;
        SET @mode = 'R' -- set to 'R' for Report or 'U' for update
        BEGIN TRANSACTION

        DECLARE @sourceName VARCHAR(8);
        DECLARE @okay CHAR(1);
        DECLARE @user VARCHAR(20);

        SET @sourceName = 'liveDB';
        SET @user = SYSTEM_USER;

        CREATE synonym sourcesummary FOR [Server1].[liveDB].[dbo].[order_summary];
        SET @okay = 'Y'

        IF @okay = 'Y'
        AND @@SERVERNAME = 'Server1'
        BEGIN
        SELECT 'Cannot execute on live server - retry on the sever hosting the db that is to be copied to' [error]

          SET @okay = 'N'
        

        END

        IF @okay = 'Y'
        AND NOT EXISTS (SELECT order_num
        FROM sourcesummary
        WHERE order_num = @orderNum)
        BEGIN
        SELECT 'Unable to find order' [error],@sourceName [source db],@orderNum [order number];

          SET @okay = 'N';
        

        END

        IF @okay = 'Y'
        BEGIN
        IF EXISTS (SELECT order_num
        FROM order_summary
        WHERE ordr_num = @orderNum)
        BEGIN
        PRINT 'deleting order_summary:';

                DELETE order\_summary
                WHERE  order\_num = @orderNum;
            END
        
          BEGIN
        
          PRINT 'copying order\_summary:';
              INSERT INTO order\_summary
              SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
              FROM   sourcesummary
              WHERE  sourcesummary.order\_num = @orderNum;
          END
        
          PRINT 'retreiving copied order\_summary:';
          SELECT 'copied' \[order\_summary\],\*
          FROM   order\_summary
          WHERE  order\_num = @orderNum;
        

        END

        DROP synonym sourcesummary;

        IF @mode = 'U'
        BEGIN
        COMMIT TRANSACTION

          SELECT 'Changes hav
        
        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Or add the WITH(NOLOCK) hint. I also recall others having similar trouble when using GO, but that doesn't appear to be the problem here.

        C 1 Reply Last reply
        0
        • P PIEBALDconsult

          Or add the WITH(NOLOCK) hint. I also recall others having similar trouble when using GO, but that doesn't appear to be the problem here.

          C Offline
          C Offline
          Clive D Pottinger
          wrote on last edited by
          #4

          Thank you gentlemen. I found the cause of the issue. The section that reads

            PRINT 'copying order\_summary:';
                INSERT INTO order\_summary
                SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
                FROM   sourcesummary
                WHERE  sourcesummary.order\_num = @orderNum;
          

          should have been

            PRINT 'copying order\_summary:';
                SET IDENTITY\_INSERT order\_summary ON;
                INSERT INTO order\_summary (\[order\_num\],\[order\_date\], etc, etc, etc...)
                SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
                FROM   sourcesummary
                WHERE  sourcesummary.order\_num = @orderNum;
                SET IDENTITY\_INSERT order\_summary OFF;
          

          "... to become an SQL expert? Oh, I figure 3 or 4 days reading. A week tops!"

          Clive Pottinger Victoria, BC

          C 1 Reply Last reply
          0
          • C Clive D Pottinger

            Thank you gentlemen. I found the cause of the issue. The section that reads

              PRINT 'copying order\_summary:';
                  INSERT INTO order\_summary
                  SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
                  FROM   sourcesummary
                  WHERE  sourcesummary.order\_num = @orderNum;
            

            should have been

              PRINT 'copying order\_summary:';
                  SET IDENTITY\_INSERT order\_summary ON;
                  INSERT INTO order\_summary (\[order\_num\],\[order\_date\], etc, etc, etc...)
                  SELECT \[order\_num\],\[order\_date\], etc, etc, etc...
                  FROM   sourcesummary
                  WHERE  sourcesummary.order\_num = @orderNum;
                  SET IDENTITY\_INSERT order\_summary OFF;
            

            "... to become an SQL expert? Oh, I figure 3 or 4 days reading. A week tops!"

            Clive Pottinger Victoria, BC

            C Offline
            C Offline
            cbeglobal
            wrote on last edited by
            #5

            :thumbsup:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups