--1.成绩表存学号,姓名,成绩值。根据成绩显示优(90以上),良(80到90),中(70到80),及格(60到70),不及格(60以下)。
use student
create table scoretable(
NUM char(20) not null,
NAME char(10) not null,
SCORE int
)
insert into scoretable values('16050555201','张三',80)
insert into scoretable values('16050555205','姜子牙',92)
insert into scoretable values('16050555210','张飞',85)
insert into scoretable values('16050555219','赵云',79)
insert into scoretable values('16050555221','韩信',65)
insert into scoretable values('16050555204','刘备',87)
insert into scoretable values('16050555205','王昭君',55)
insert into scoretable values('16050555206','芈月',100)
select * from scoretable
select (
case when score<60 then '不及格'
when score>=60 and score<70 then '及格'
when score>=70 and score<80 then '中'
when score>=80 and score<90 then '良'
when score>=90 then '优' end
) as 成绩等次 from scoretable
--2.TSQL编程实现判断变量是字母,数字还是其他字符,并输出其类型。
--通过if语句求一个字符是什么类型
declare @ch char
set @ch = '#'
if UPPER(@ch)>='A' and UPPER(@ch)<='Z'
print @ch+'是字母'
else if @ch>='0' and @ch<='9'
print @ch+'是数字'
else
print @ch+'是其他字符'
--通过case语句求一个字符是什么类型
declare @ch2 char
set @ch2 = ''
print
case
when upper(@ch2)>='A' and upper(@ch2)<='Z' then @ch2+'是字符'
when @ch2>='0' and @ch2<='9' then @ch2+'是数字'
else @ch2+'是其他字符'
end
--3.TSQL编程实现输出所有3000以内能被17整除的数。
declare @Result table (Num int null)
declare @i int
set @i=1
while @i<=3000
begin
if (@i%17)=0
insert into @Result values(@i)
set @i=@i+1
end
select * from @Result
--4.编程实现函数,通过辗转相除法解两个正整数的最大公约数。
CREATE FUNCTION GetGys
(
@num1 BIGINT ,
@num2 BIGINT
)
RETURNS BIGINT
AS
BEGIN
DECLARE @m BIGINT;
DECLARE @i BIGINT;
IF ( @num1 < @num2 )--确保@num1永远是大的 @num2永远是小的
BEGIN
SET @m = @num2;
SET @num2 = @num1;
SET @num1 = @m;
END;
SET @i = @num1 % @num2;
WHILE @i > 0
BEGIN
SELECT @num1 = @num2 ,
@num2 = @i ,
@i = @num1 % @num2;
END;
RETURN @num2;
END;
GO
select dbo.GetGys(18,24) as 最大公约数
--5.TSQL编程实现一个函数,可以将十进制数转换为二进制字符串。
declare @ch3 nchar(10),@n int,@a int
set @n=20
set @ch3=''
print cast(@n as varchar(5))+'的二进制为:'
while @n<>0
begin
set @a=@n%2
set @n=@n/2
set @ch3=char(48+@a)+@ch3
end
print @ch3