How update multiple tables in a Stored Procedure

Carlton Patterson 741 Reputation points
2023-07-04T14:50:35.33+00:00

The following procedure will update a table called 'Process.Queue'. Can someone help modify the procedure such that it updates more than one table. For example, I would the procedure updated both tables,Process.Queue, and Process.ThroughPut

CREATE PROCEDURE Process.FinaliseProcess (@ProcessID int)
AS
BEGIN
    DECLARE @StatusID int = (SELECT ProcessStatusID FROM Process.ProcessStatus WHERE StatusName = 'Succeeded')

    UPDATE Process.Queue
    SET ProcessStatusID = @StatusID,
        ProcessEndDate = getdate()
    WHERE ProcessID = @ProcessID
END
GO
Azure SQL Database
SQL Server | Other
{count} votes

Accepted answer
  1. Anonymous
    2023-07-04T15:12:30.32+00:00

    Hi carlton

    To modify the procedure to update more than one table (Process.Queue and Process.ThroughPut), you can simply add additional UPDATE statements for each table you want to update. Here's the modified procedure:

    
    CREATE PROCEDURE Process.FinaliseProcess (@ProcessID int) AS 
    BEGIN     
       DECLARE @StatusID int = (SELECT ProcessStatusID FROM Process.ProcessStatus WHERE StatusName = 'Succeeded')      
    
       -- Update Process.Queue     
       UPDATE Process.Queue     
       SET   ProcessStatusID = @StatusID,         
             ProcessEndDate = GETDATE()     
       WHERE ProcessID = @ProcessID;      
    
      -- Update Process.ThroughPut (assuming it has the same columns as Process.Queue)     
      UPDATE Process.ThroughPut     
      SET   ProcessStatusID = @StatusID,         
            ProcessEndDate = GETDATE()     
      WHERE ProcessID = @ProcessID; 
    END
    GO 
    
    
    
    

    try this

    In this example, I assumed that the Process.ThroughPut table has the same columns as the Process.Queue table. If the tables have different column structures, you will need to adjust the UPDATE statements accordingly, ensuring that you are updating the correct columns in each table.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-07-05T05:48:34.4933333+00:00

    such that it updates more than one table.

    One UPDATE statement can only update one table. If you want to update more tables, then you have to write a seperate UPDATE command for each table.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.