目录
一、编写T-SQL程序,计算 1 + 2 − 3 + 4 − 5 … − 99 + 100 =?
二、写一个求三个数最大值的函数,输入为三个整数,输出为其中的最大值。
三、写一个T-SQL函数 ,利用标准体重计算公式,根据身高判断体重是否合乎标准
四、写一个T-SQL多语句表值函数 ,根据父母的血型列出孩子的所有可能血型
declare @i int,@j int,@sum int,@str varchar(500),@temp int
select @i=2,@j=-1,@sum=1,@str='1'
while(@i<=100)
begin
set @temp=power(@j,@i)*@i
set @sum = @sum+@temp
if(@temp>0)
set @str+='+'+cast(@temp as varchar(5))
else
set @str+=cast(@temp as varchar(5))
set @i+=1
end
print @str+'='+cast(@sum as varchar(3))
create function MyMax(
@num1 int,
@num2 int,
@num3 int
)
returns int
as
begin
declare @Maxnum int
if(@num1>=@num2)
set @Maxnum = @num1
else
set @Maxnum = @num2
if(@Maxnum>=@num3)
set @Maxnum = @Maxnum
else
set @Maxnum = @num3
return @Maxnum
end;select dbo.MyMax( 7, 9, 13)
执行结果:
T-SQL程序代码
create function fat(
@height int,
@weight int,
@sex varchar(3)
)
returns varchar(50)
as
begin
declare @tempWeight int,@str varchar(50)
set @str='';
if(@sex='男')
set @tempWeight = (@height-105)
else if(@sex='女')
set @tempWeight = (@height-100)
else
begin
set @str='你输入的性别有误';
return @str;
end
if(@tempWeight-@weight>0)
set @str='你的体重比标准体重轻了'+cast(@tempWeight-@weight
as varchar(4))+'公斤'
else if(@tempWeight-@weight<0)
set @str='你的体重比标准体重重了'+cast(@weight-@tempWeight
as varchar(4))+'公斤'
else
set @str='你的身材非常标准'
return @str;
end
执行结果:
T-SQL程序代码
create function blood(
@faBl varchar(4),
@maBl varchar(4)
)
returns @blood table(
possibleblood varchar(4)
)
as
begin
if(@faBl='A'and @maBl='A')or(@faBl='O' and @maBl='A')
or(@faBl='A'and @maBl='O')
begin
insert into @blood values('A');
insert into @blood values('O');
end
else if(@faBl='A' and @maBl='B')or(@faBl='B' and @maBl='A')
begin
insert into @blood values('A');
insert into @blood values('B');
insert into @blood values('AB');
insert into @blood values('O');
end
else if(@faBl='A' and @maBl='AB')or(@faBl='B' and @maBl='AB')
or(@faBl='AB' and @maBl='A')or(@faBl='AB' and @maBl='B')
or(@faBl='AB' and @maBl='AB')
begin
insert into @blood values('A');
insert into @blood values('B');
insert into @blood values('AB');
end
else if(@faBl='B' and @maBl='B')or(@faBl='B' and @maBl='O')
or(@faBl='O' and @maBl='B')
begin
insert into @blood values('B');
insert into @blood values('O');
end
else if(@faBl='AB' and @maBl='O')or(@maBl='AB' and @faBl='O')
begin
insert into @blood values('A');
insert into @blood values('B');
end
else if(@faBl='O' and @maBl='O')
begin
insert into @blood values('O');
end
return
end
执行结果: