Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Update two table using single procedure

$
0
0

Hi Guys,

Please can you help me to reframe this procedure. Currently it is taking more time for execution.
i have four tables temp_table, table_account, table_recpt, table_dusr. Of which i will be updating tables table_account and table_recpt.
I will be updating more than 200K rows on table_account and approx 1 million on table_recpt.

temp_table
col1, col2, col3, col4, col5

table_account
col1, col2, col3, col4, col6 ,col7

table_dus
col5,col6

table_recpt
col7, col3, col4, col6

alter table table_recpt add column mynewcolumn int null;
alter table table_account add column mynewcolumn1 int null;

================================================================

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[temp_updateprocedure]    
  
AS
SET NOCOUNT ON
  
  DECLARE  @COUNT_VAR INT;
  SET      @COUNT_VAR=0;
  DECLARE  @COUNT_VAR1 INT;
  SET      @COUNT_VAR1=0;
  DECLARE  @CNT INT;
  SET      @CNT = 1;

  BEGIN TRY    -- Wrap in a transaction.  
   
     
         BEGIN TRAN -- 1ST TRANSACTION TO Update the REC POINT MANAGEMENT TABLE   

          WHILE @CNT > 0
          BEGIN   

   UPDATE  RECPT
   SET
   RECPT.COL3 = temp_table.COL3,
   RECPT.COL4 = temp_table COL4,
   RECPT.COL5 = table_dus.COL6
         RECPT.MYNEWCOLUMN = 1
                 FROM   table_recpt RECPT
   inner join CONDITIONS
   WHERE RECPT.MYNEWCOLUMN IS NULL

   SET @COUNT_VAR=@COUNT_VAR + @@ROWCOUNT;

   SELECT @COUNT_VAR;

      END --END WHILE     

     
  COMMIT TRANSACTION   -- Commit the transaction.

                BEGIN TRAN -- 1ST TRANSACTION TO Update the REC POINT MANAGEMENT TABLE   

          WHILE @CNT > 0
          BEGIN   

   UPDATE  ACNT
   SET
   ACNT.COL3 = temp_table.COL3,
   ACNT.COL4 = temp_table.COL4,
   ACNT.COL5 = table_dus.COL6
         RECPT.MYNEWCOLUMN1 = 1
                 FROM   table_account ACNT
   inner join CONDITIONS
   WHERE ACNT.MYNEWCOLUMN1 IS NULL

   SET @COUNT_VAR1=@COUNT_VAR + @@ROWCOUNT;

   SELECT @COUNT_VAR1;

      END --END WHILE     

     
  COMMIT TRANSACTION   -- Commit the transaction.

  END TRY   

 

   
 BEGIN CATCH
   
  IF @@trancount > 0   
  BEGIN   
   -- Undo.   
  ROLLBACK   
  END   
   
  -- Let the app know that it failed.   
  SELECT 0 Result   
   
 END CATCH   
   
SET NOCOUNT OFF
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>