我需要編寫一個SQL查詢來更新DateOfBirth
列中的Age
列。下面是一個包含一些現成數據的示例表。如何在SQL Server查詢中基於DateOfBirth列更新Age列
CREATE TABLE Students
(
ID INT IDENTITY,
Name VARCHAR (100),
DateOfBirth date,
Age INT,
ClassID INT,
SectionID INT,
PRIMARY KEY (ID)
)
INSERT INTO Students (Name, DateOfBirth, Age, ClassID, SectionID)
VALUES ('Adam','2003-06-16', 0, 6, 2)
INSERT INTO Students (Name, DateOfBirth, Age, ClassID, SectionID)
VALUES ('Botham','2001-03-22', 0, 8, 1)
INSERT INTO Students (Name, DateOfBirth, Age, ClassID, SectionID)
VALUES ('Hillton',Null, 0, 7, 2)
INSERT INTO Students (Name, DateOfBirth, Age, ClassID, SectionID)
VALUES ('Rasty','2004-12-02', 0, 5, 1)
INSERT INTO Students (Name, DateOfBirth, Age, ClassID, SectionID)
VALUES ('Holistar',Null, 0, 8, 1)
如果您已經有DateOfBirth列,爲什麼還需要保留Age列? –
如果你真的需要它作爲一個單獨的列,你可以把它作爲一個計算列 –
同意Zohar - 一般來說,不要*存儲你可以計算的數據(除非計算是昂貴的,甚至在那裏,內置到數據庫系統中的工具,以便您不必手動執行計算) –