当前位置:文档之家› 实验报告一

实验报告一

一、实验目的 1、实验目的 (1)、掌握利用各种数据类型声明局部变量的方法。 (2)、掌握为局部变量赋值的俩中方法。 (3)、掌握常用系统函数、运算符和表达式的功能和应用。 (4)、掌握Transact-SQL控制流语言的基本功能和分类。 (5)、掌握利用控制流语句实现基本的分支选择和循环处理功能。 (6)、了解其他控制流语句的功能和应用。 (7)、掌握SELECT各个字句的功能和检索数据的方法。 (8)、掌握WHERE字句中LIKE、IN、BETEEN、IS等逻辑运算符的使用。 (9)、掌握COMPUTE语句和聚合函数的使用。

二、实验内容和步骤 1、变量的应用 declare @sno char(8),@name varchar(10),@sex nchar(12),@birthday datetime,@usually int, @final numeric(4,1) set @sno='32145467'; set @name='哈哈'; set @sex='男'; select @birthday ='1989-03-09',@usually=90,@final=80 print @sno+@name+@sex print @birthday print @usually print @final

2、运算符的应用 A、比较运算符 use teaching go select * from student where birthday>'1989-01-01'

select * from teacher where department<>'计算机学院'

B、逻辑运算符 use teaching go select * from score where studentno like '09%' and final between 60 and 90

------------------------ select * from teacher where prof in('教授','副教授')

C、“+”号运算符: declare @a char(5),@b varchar(5),@c int,@d decimal(5,2) select @a='123',@b='456.5',@c=321,@d=564.4 print @a+@b print @a+@d print @c+@d select @a='数据库',@b='程序开发' print @a+@b print @a+@d

D、位运算符 declare @a int,@b int select @a=5,@b=12 select @a&@b,@a|@b,@a^@b,~@a

E、数学函数 select ceiling(16.3),ceiling(-16.8),ceiling(0.0)//向上取整 select floor(16.3),floor(-16.8),floor(0.0)//四舍五入 select round(123.456,2),round(123.456,-1),round(173.456,-2),round(123.456,-4)//第二个数字是四舍五入的位数,当负数时是“.”的左边 select round(175.86,0),round(175.86,0,1) F、时间日期函数 declare @birthday datetime set @birthday ='1989-08-21' select @birthday as '生日',datediff(year,@birthday,getdate()) as '年龄' select getdate() as '当前日期',year(getdate()) as '年份',datepart(month,getdate()) as '月份', datename(day,getdate()) as '日期'

G、转换函数 declare @count int,@date datetime select @count=255,@date=getdate() print '变量count的值为:'+cast(@count as varchar(5)) print cast('2009-7-07' as smalldatetime)+100 print convert(varchar(10),@date,102)

H、字符函数 declare @str as nchar(25) set @str='SQL SERVER 2005 数据库应用与开发' select len(@str),charindex('库应用',@str),substring(@str,5,6),replace(@str,'开发','设计'),lower(@str),ascii(@str)

3、编写程序,根据姓名查询teaching数据库中学生的基本信息和选课信息,学生姓名通过变量输入。对于不存在的学生姓名输入值,打印提示信息。 use teaching go declare @sname nchar(8) set @sname=' 许海冰' if exists(select * from student where sname=@sname) select student.*,courseno,usually,final from student,score where student.studentno=score.studentno and sname=@sname else print '提示:不存在姓名为'+rtrim(ltrim(@sname))+'的学生资料'

4、编写程序,查询所以学生选修课的期末成绩和对应等级,如学生末选修任何课程则输出提示信息。 use teaching go select student.studentno,sname,cname,final, case when final>=90 then '优' when final>=80 then '良' when final>=70 then '中' when final>=60 then '及格' when final<60 then '不及格' when final is null then '未选修任何课程' end as level from student left join score on(student.studentno=score.studentno) left join course on (course.courseno=score.courseno)

5、编写程序,判断字符变量@ch中存放的是字母字符、数字字符还是其他字符,并输出相关的信息。 declare @ch char select @ch='d' if upper(@ch)>='A' and upper(@ch)<='Z' print @ch+'是字母字符' else if @ch>='0' and @ch<='9' print @ch+'是数字字符' else print @ch+'是其他字符' 当@ch='3'时, 当@ch='#'时, 6、编写程序,判断某个年份是否为闰年,年份由变量输入。 declare @year int set @year =year(getdate()) if @year%4=0 begin if @year%100=0 begin if @year%400=0 print cast(@year as char(4))+'年是闰年' else print cast(@year as char(4))+'不年是闰年' end else print cast(@year as char(4))+'年是闰年' end else print cast(@year as char(4))+'不年是闰年'

7、编写程序,输出在1~3000之间能被17整除的最大数值。 declare @s int ,@i int select @s=0,@i=3000 while @i>1 begin if @i%17=0 begin print '1~3000之间能被整除的最大数值为:'+cast(@i as char(4)) break end set @i=@i-1 End

8、查询所有课程的课程编号、课程号和学分 use teaching go select courseno,cname,credit from course 9、查询‘090501’班的所有学生的基本信息。 use teaching go select * from student where classno='090501'

10、查询student表中所有年龄大于20岁的男生的名字和年龄。 use teaching go select sname,datediff(year,birthday,getdate()) as age from student where datediff(year,birthday,getdate())>20 and sex='男'

11、查询计算机学院教师的专业名称。 use teaching go select distinct major from teacher where department='计算机学院'

12、查询选修课程且期末成绩不为空的学生人数。 use teaching go select count(distinct studentno) as '选修课程学生的人数' from score where final is not null

相关主题