[英]Conditionally add a column with a default value to an existing table in SQL Server
I want to add a new column in existing SQL table with default values depending upon some cases/conditions.我想根据某些情况/条件在现有 SQL 表中添加一个具有默认值的新列。
I want to know if this is possible.我想知道这是否可能。 If yes how?
如果是如何? if not why?
如果不是为什么?
Alternative :选择 :
One option is to create the column and then update but approach is out of the question here.一种选择是创建列然后更新,但这里的方法是不可能的。
Let's consider I have the following table让我们考虑一下我有下表
╔════╦══════════════╗
║ ID ║ Age ║
╠════╬══════════════║
║ 1 ║ 0.166667 ║
║ 2 ║ 0.125 ║
║ 3 ║ 13 ║
║ 4 ║ 19 ║
║ 5 ║ 45 ║
║ 6 ║ 59 ║
║ 7 ║ 60 ║
║ 8 ║ 64 ║
╚════╩══════════════╝
Desired output is this :所需的输出是这样的:
╔════╦══════════════╦═══════════╗
║ ID ║ Age ║ AgeGroup ║
╠════╬══════════════╬═══════════╣
║ 1 ║ 0.166667 ║ NewBorn ║
║ 2 ║ 0.125 ║ NewBorn ║
║ 3 ║ 13 ║ Teen ║
║ 4 ║ 19 ║ Teen ║
║ 5 ║ 45 ║ Adult ║
║ 6 ║ 59 ║ Adult ║
║ 7 ║ 60 ║ Elder ║
║ 8 ║ 64 ║ Elder ║
╚════╩══════════════╩═══════════╝
I have studied this post but this is only for "Adding column default values"我已经研究过这篇文章,但这仅适用于“添加列默认值”
EDIT : Here is the SQL script with schema and data for the above table for users who wants to test.编辑:这是用于想要测试的用户的上表架构和数据的 SQL脚本。
You may try this.你可以试试这个。 There are basically 2 approach to achieve this.
基本上有两种方法可以实现这一点。
Adding Column in table schema在表架构中添加列
As I understand you want to add a column in table schema as given link shared by you.据我了解,您想在表模式中添加一列作为您共享的给定链接。 You may use
case
statement to add column for calculated values.您可以使用
case
语句为计算值添加列。
EDIT : Correcting Syntax Error编辑:更正语法错误
alter table yourtable
add AgeGroup as (case
when Age < 2 then 'New Born'
when Age < 19 then 'Teen'
when Age < 45 then 'Young'
when Age > 60 then 'Adult'
else 'Invalid'
end);
Or或者
Create view创建视图
You may create a view for same and use it wherever you needed.您可以为其创建一个视图并在任何需要的地方使用它。
Create View TableResult
As
Select Id, Age, case Age
WHEN Age < 2 then 'New Born'
WHEN Age < 19 then 'Teen'
WHEN Age < 45 then 'Young'
WHEN Age > 60 then 'Adult'
else 'Invalid'
end as AgeGroup
End
use case when用例当
select case when age>0 and age<13 then 'new born'
when age>=13 and age<=19 then 'teen'
.................
... end -- put here more condition as your need
You can use a calculated Field.您可以使用计算字段。
Your table design will be like this:您的桌子设计将是这样的:
CREATE TABLE [dbo].[MyTbl](
[ID] [INT] IDENTITY(1,1) NOT NULL,
[Age] DECIMAL(10,3) NOT NULL,
[AgeGroup] AS (CASE WHEN Age < 1 THEN 'Newborn'
WHEN Age BETWEEN 1 AND 13 THEN 'Kid'
WHEN Age BETWEEN 13 AND 60 THEN 'Adult'
WHEN Age > 60 THEN 'Elder' END)
CONSTRAINT [PK_ID]
PRIMARY KEY CLUSTERED ( [ID] ASC ))
Your field will be filled automatically, you just have to add your age.您的字段将自动填充,您只需添加您的年龄。
First, you will have to create the AgeGroup
column to the table:首先,您必须为表创建
AgeGroup
列:
ALTER TABLE yourTable
ADD AgeGroup VARCHAR(10)
As for making default values based on set conditions:至于根据设定条件制作默认值:
UPDATE yourTable
SET AgeGroup = CASE WHEN yourTable.Age >= 0 AND yourTable.Age < 2 THEN 'NewBorn'
WHEN yourTable.Age >= 13 AND yourTable.Age <= 19 THEN 'Teen'
WHEN yourTable.Age >= 20 AND yourTable.Age <= 59 THEN 'Adult'
WHEN yourTable.Age >= 60 THEN 'Elder' END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.