How to update data in one table from corresponding data in another table in SQL Server

Format:

update t2
set t2.deptid = t1.deptid
from test1 t1, test2 t2
where t2.employeeid = t1.employeeid

Example:

USE [UG_AdmissionDb]
GO
-- exec sp_BackupData

ALTER PROCEDURE [dbo].[sp_Modified_Data]
AS
BEGIN          
    -- Begin Trnsaction
    BEGIN TRY
        BEGIN TRANSACTION        
            
            UPDATE t2
            SET t2.[BANGLA] = t1.BANGLA,
            t2.[ENGLISH] = t1.[ENGLISH],
            t2.[PHYSICS] = t1.[PHYSICS],
            t2.[CHEMISTRY] = t1.[CHEMISTRY],
            t2.[MATHEMATICS] = t1.[MATHEMATICS],
            t2.[BIOLOGY] = t1.[BIOLOGY],
            t2.[SUB_4TH] = t1.[SUB_4TH],
            t2.[GPA] = t1.[GPA],
            t2.[CGPA] = t1.[CGPA],
            t2.[LTGRADE] = t1.[LTGRADE]

            FROM [ReScrutiny].[dbo].[ug_data_full_board_hsc] as t1, [UG_AdmissionDb].[dbo].[ug_data_full_board_hsc]  as t2
            WHERE
              t2.[ROLL] = t1.[ROLL]
              AND t2.[REG] = t1.[REG]
              AND t2.[PASSYEAR] = t1.[PASSYEAR]
              AND t2.[BOARDNAME] = t1.[BOARDNAME]
    
        COMMIT
        select 'Process Successful!' IsOk
    END TRY
    BEGIN CATCH
        IF(@@TRANCOUNT > 0)
            ROLLBACK        
        select ERROR_MESSAGE() IsOk    
    END CATCH
END
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s