Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

updating records from one table to another table by ID

Hi experts,

I'm using SQL Server 2019.

I have a table called Employees that looks like this:

User generated image
I have another table called PromotedEmployees that looks like this:
This table holds some employee names and their new title.

User generated image
This is the sql script to create the Employees table:

USE [TestDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employees](
	[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
	[LastName] [nvarchar](20) NOT NULL,
	[FirstName] [nvarchar](10) NOT NULL,
	[Title] [nvarchar](30) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Employees] ON 

INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (1, N'Davolio', N'Nancy', N'Sales Representative')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (2, N'Fuller', N'Andrew', N'Vice President, Sales')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (3, N'Leverling', N'Janet', N'Sales Representative')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (4, N'Peacock', N'Margaret', N'Sales Representative')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (5, N'Buchanan', N'Steven', N'Sales Manager')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (6, N'Suyama', N'Michael', N'Sales Representative')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (7, N'King', N'Robert', N'Sales Representative')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (8, N'Callahan', N'Laura', N'Inside Sales Coordinator')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title]) VALUES (9, N'Dodsworth', N'Anne', N'Sales Representative')
SET IDENTITY_INSERT [dbo].[Employees] OFF

Open in new window




This is the sql script to create the PromotedEmployees table:

USE [TestDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PromotedEmployees](
	[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
	[LastName] [nvarchar](20) NOT NULL,
	[FirstName] [nvarchar](10) NOT NULL,
	[NewTitle] [nvarchar](30) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[PromotedEmployees] ON 

INSERT [dbo].[PromotedEmployees] ([EmployeeID], [LastName], [FirstName], [NewTitle]) VALUES (3, N'Leverling', N'Janet', N'IT Manager')
INSERT [dbo].[PromotedEmployees] ([EmployeeID], [LastName], [FirstName], [NewTitle]) VALUES (6, N'Suyama', N'Michael', N'HR Manager')
INSERT [dbo].[PromotedEmployees] ([EmployeeID], [LastName], [FirstName], [NewTitle]) VALUES (7, N'King', N'Robert', N'Finance Manager')
INSERT [dbo].[PromotedEmployees] ([EmployeeID], [LastName], [FirstName], [NewTitle]) VALUES (9, N'Dodsworth', N'Anne', N'Marketing Manager')
SET IDENTITY_INSERT [dbo].[PromotedEmployees] OFF

Open in new window



Now I want to update the Title in the Employees table with the new titles pertaining to each employee that is found in the PromotedEmployees table.

As shown in this image:

User generated image

So the Employees table should then look like this:

User generated image

What's the best way to do that?

These tables are just a small model of my real tables. In my real tables there are thousands of records in each table.
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maqskywalker
maqskywalker

ASKER

nice. thanks.