準備一些數據,創建2張表,表1為學生表: CREATE TABLE [dbo].[Student] ( [SNO] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(10) NOT NULL, [Performance] DECIMAL(18,2) NULL ) G ...
準備一些數據,創建2張表,表1為學生表:
CREATE TABLE [dbo].[Student] ( [SNO] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(10) NOT NULL, [Performance] DECIMAL(18,2) NULL ) GOSource Code
表2為學生成績表:
接下來,我們為上面2張表添加一些數據,並查詢數據結果:
INSERT INTO [dbo].[Student]([SNO],[Name]) VALUES (10001,N'甲'), (10002,N'乙'), (10003,N'丙'), (10004,N'丁'), (10005,N'戊'), (10006,N'已'), (10007,N'庚') GO INSERT INTO [dbo].[Performance]([SNO],[Score]) VALUES (10002,320), (10004,380), (10001,424), (10007,467), (10005,300) GO SELECT * FROM [dbo].[Student] GO SELECT * FROM [dbo].[Performance] GOSource Code
以上均是為本篇所準備的數據。
把表2的欄位[Score]值更新至表1的[Performance]。
UPDATE s SET s.[Performance] = p.[Score] FROM [dbo].[Student] AS s INNER JOIN [dbo].[Performance] AS p ON(s.[SNO] = p.[SNO]) GO SELECT * FROM [dbo].[Student] GOSource Code