-1

I have two tables:

Table 1

Code Value
AAA  100
BBB  200
CCC  300
DDD  400

Table 2

Code NewCode
AAA  ALPHA
BBB  BETA
DDD  DELTA

How do you create a stored procedure that will update all the Code in Table 1 so to the NewCode they should have based on Table 2? So that the end result will be something like this:

Code   Value
ALPHA  100
BETA   200
CCC    300
DELTA  400
Dale K
  • 21,987
  • 13
  • 41
  • 69
bloodfire1004
  • 483
  • 1
  • 7
  • 23

2 Answers2

3
UPDATE Table1
SET Table1.Code=Table2.Newcode
FROM Table2
WHERE Table1.Code=Table2.Code
Saman Gholami
  • 3,241
  • 6
  • 29
  • 67
1

Here is procedure creation:

USE [myDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[MyProcedure] 
(
  -- Add the parameters for the stored procedure here
)
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE TABLE1-----------THAT IS THE QUERY.
        SET TABLE1.Code = TABLE2.NewCode
    WHERE TABLE1.Code = TABLE2.Code
END
Dale K
  • 21,987
  • 13
  • 41
  • 69