我正在为一个班级做作业,我应该确定这个人的年龄是否介于以下某个类别之间并插入适当的值:
0 – 12 – Child
13 – 19 – Teenager
20 + Adult.
我知道使用case选项要容易得多但是我需要使用if语句。我已经有一个名为营销列表的表,我在其中更改并添加了年龄和年龄类别列。有人可以帮帮我吗?
alter table marketing_list
add age integer,
age_category varchar(20)
Create Procedure Age_Range
(
@Id integer,
@Name varchar(20),
@DOB date,
@Parish varchar(20),
@Mobile integer,
@Mobile_Provider varchar(7)
)
As
Begin
update marketing_list
Set age = DATEDIFF(year, Date_of_Birth, GETDATE())
If (DATEDIFF(year, @DOB,GETDATE()) > 0 and (DATEDIFF(year, @DOB, GETDATE()) <= 12))
Begin
update marketing_list
Set age_category = 'Child'
End
End
答案 0 :(得分:0)
你喜欢这样的工作吗? (伪代码,但应该没问题)
Create Procedure Age_Range
(
@Id integer,
@Name varchar(20),
@DOB date,
@Parish varchar(20),
@Mobile integer,
@Mobile_Provider varchar(7)
)
As
Begin
declare @age int
set @age = datediff(YEAR, @dob, getutcdate())
if(@age <= 12)
update marketing_list Set age_category = 'Child', age = @age where id = @id
if(@age > 12 AND @age <= 19)
update marketing_list Set age_category = 'Teenager', age = @age where id = @id
else
update marketing_list Set age_category = 'Adult', age = @age where id = @id
end