SQL Server 基础练习题1

1、任意给定两个整数,求这两个数之和

在这里插入图片描述
源码:

declare @x int,@y int, @z int
set @x=1
set @y=2
set @z=@x+@y
print 'x+y='+ltrim(str(@z))

2、使用单分支的条件语句编程,计算并输出两个整数的最大值

在这里插入图片描述
源码:

declare @a as int,@b as int,@max as int
set @a=3
set @b=5
if @a>@b
   set  @max=@a
if @a<=@b
   set @max=@b
print 'max='+cast(@max as char(3))

3、使用双分支的条件语句编程,计算交输出两个整数的最大值

在这里插入图片描述
源码:

declare @a as int,@b as int,@max as int
set @a=3
set @b=5
if @a>@b
   set @max=@a
else
   set @max=@b
print 'max='+convert(char(3),@max)

4、有一函数

①当x<0时,y=-1;

②当x=0时,y=0;

③当x>0时,y=1。编写程序,输入一个x值,输出y值

算法一:
在这里插入图片描述
源码:

declare @x as int,@y as int
set @x=3
if @x<0
   set @y=-1
else
   if @x=0
      set @y=0
   else
      set @y=1
print @y

算法二:
在这里插入图片描述
源码:

declare @x as int,@y as int
set @x=3
set @y=case  
         when @x<0 then  -1
         when @x=0 then  0
         else 1
end
print @y

5、将学生百分制成绩输出等级制成绩,百分制分数段[90,100]为优;[80,90)为良;[70,80)为中;[60,70)为及格;[0,60)为不及格

在这里插入图片描述
源码:

declare @bcj real,@dcj varchar(18)
set @bcj=86.8
set @dcj=case  
          when @bcj >=90 and @bcj<=100 then '优'
          when @bcj >=80 and @bcj<90 then '良'
          when @bcj >=70 and @bcj<80 then '中'
          when @bcj >=60 and @bcj<70 then '及格'
          when @bcj >=0 and @bcj<60 then '不及格'
          else '这不是百分制成绩!'
       end
print @dcj

6、根据自然数字,输出相应的星期的英文单词

declare @a  int,@y  varchar(20)
set @a=6
set @y=case  
          when @a =1 then 'Monday'
          when @a =2 then 'Tuesday'
          when @a =3 then 'Wednesday'
          when @a =4 then 'Thursday'
          when @a =5 then 'Fridday'
          when @a =6 then 'Saturday'
          when @a =7 then 'Sunday'
          else 'No such day'
       end
print @y
此程序还可以这样写
declare @a  int,@y  varchar(20)
set @a=6
set @y=case  @a
          when 1 then 'Monday'
          when 2 then 'Tuesday'
          when 3 then 'Wednesday'
          when 4 then 'Thursday'
          when 5 then 'Fridday'
          when 6 then 'Saturday'
          when 7 then 'Sunday'
          else 'No such day'
       end
print @y

7、计算1+2+……+100

在这里插入图片描述
源码:

declare @sum int,@i int
set @sum=0
set @i=1
while @i<=100
      begin
         set @sum=@sum+@i
         set @i=@i+1
      end
print @sum

8、找出100至999之间的所有“水仙花数”。所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身(如153=13+53+33)。请编程计算并输出所有的水仙花数

源码:

declare @n as int,@b as int,@s as int,@g as int
set @n=100
while @n<1000
     begin
         set @b=cast(substring(cast(@n as char(3)),1, 1) as int) 
         set @s=cast(substring(cast(@n as char(3)),2,1) as int)
         set @g=cast(substring(cast(@n as char(3)),3,1) as int)
         if power(@b,3)+power(@s,3)+power(@g,3)=@n
            begin
               print @n
            end

9、编程输出如下九九乘法表

在这里插入图片描述
源码:

/*i表示被乘数,又表示打印列数;j表示乘数,又表示打印行数*/
declare @i tinyint,@j tinyint,@s varchar(100)
set @j=1
while @j<=9
   begin
      set @i=1
      set @s=space(0)
      while @i<=@j
         begin
            set @s=@s+cast(@i as char(1))+'×'+cast(@j as char(1))+'='+cast((@i*@j) as char(2))+space(1)
            set @i=@i+1
         end
      print @s
      set @j=@j+1
   end
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大菜彩

家人们鼓励鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值