实验报告一

  • 格式:docx
  • 大小:159.64 KB
  • 文档页数:17

一、实验目的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='';set @name='哈哈';set @sex='男';select @birthday ='1989-03-09',@usually=90,@final=80print @sno+@name+@sexprint @birthdayprint @usuallyprint @final2、运算符的应用A、比较运算符use teachinggoselect * from student where birthday>'1989-01-01'select * from teacher where department<>'计算机学院'B、逻辑运算符use teachinggoselect * 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='',@c=321,@d=print @a+@bprint @a+@dprint @c+@dselect @a='数据库',@b='程序开发'print @a+@bprint @a+@dD、位运算符declare @a int,@b intselect @a=5,@b=12select @a&@b,@a|@b,@a^@b,~@aE、数学函数select ceiling,ceiling,ceiling的左边select round,0),round,0,1)F、时间日期函数declare @birthday datetimeset @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 datetimeselect @count=255,@date=getdate()print '变量count的值为:'+cast(@count as varchar(5))print cast('2009-7-07' as smalldatetime)+100print 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 teachinggodeclare @sname nchar(8)set @sname=' 许海冰'if exists(select * from student where sname=@sname)select student.*,courseno,usually,final from student,score where = and sname=@snameelseprint '提示:不存在姓名为'+rtrim(ltrim(@sname))+'的学生资料'4、编写程序,查询所以学生选修课的期末成绩和对应等级,如学生末选修任何课程则输出提示信息。

use teachinggoselect ,sname,cname,final,casewhen 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 levelfrom student left join score on= left join course on =5、编写程序,判断字符变量@ch中存放的是字母字符、数字字符还是其他字符,并输出相关的信息。

declare @ch charselect @ch='d'if upper(@ch)>='A' and upper(@ch)<='Z'print @ch+'是字母字符'else if @ch>='0' and @ch<='9'print @ch+'是数字字符'elseprint @ch+'是其他字符'当@ch='3'时,当@ch='#'时,6、编写程序,判断某个年份是否为闰年,年份由变量输入。

declare @year intset @year =year(getdate())if @year%4=0beginif @year%100=0beginif @year%400=0print cast(@year as char(4))+'年是闰年'elseprint cast(@year as char(4))+'不年是闰年' endelseprint cast(@year as char(4))+'年是闰年'endelseprint cast(@year as char(4))+'不年是闰年'7、编写程序,输出在1~3000之间能被17整除的最大数值。

declare @s int ,@i intselect @s=0,@i=3000while @i>1beginif @i%17=0beginprint '1~3000之间能被整除的最大数值为:'+cast(@i as char(4))breakendset @i=@i-1End8、查询所有课程的课程编号、课程号和学分use teachinggoselect courseno,cname,creditfrom course9、查询‘090501’班的所有学生的基本信息。

use teachinggoselect * from student where classno='090501'10、查询student表中所有年龄大于20岁的男生的名字和年龄。

use teachinggoselect sname,datediff(year,birthday,getdate()) as age from studentwhere datediff(year,birthday,getdate())>20 and sex='男'11、查询计算机学院教师的专业名称。

use teachinggoselect distinct majorfrom teacherwhere department='计算机学院'12、查询选修课程且期末成绩不为空的学生人数。

use teachinggoselect count(distinct studentno) as '选修课程学生的人数' from score where final is not null13、查询Email使用126邮箱的所有学生的学号、姓名和电子邮箱地址。

use teachinggoselect studentno,sname,email from studentwhere email like '%'14、查询每名学生的学号、选修课程数目、总成绩,并将查询结果存放到生成的“学生选课统计表”.方法一:use teachinggocreate table 学生选课统计表(studentno nchar(10) not null,amount smallint null,sum smallint null,)insert into 学生选课统计表select ,count(courseno) as '选修课程数目',sum(final) as '总成绩' from student left join score on=group by方法二:use teachinggoselect studentno,count(courseno) as '选修课程数目',sum(final) as '总成绩'into 学生选课统计表from scoregroup by studentno-------------------------------------select * from 学生选课统计表select sname,courseno,finalfrom student,scorewhere (courseno='c05109' or courseno='c05103') and =and final between 90 and 10015、查询score表中选修‘c05109’或‘c05103’课程,并且课程期末成绩在90~100分之间的学生姓名和期末成绩。