SQLServer2005数据库原理与应用基础第9章例题_第1页
SQLServer2005数据库原理与应用基础第9章例题_第2页
SQLServer2005数据库原理与应用基础第9章例题_第3页
SQLServer2005数据库原理与应用基础第9章例题_第4页
SQLServer2005数据库原理与应用基础第9章例题_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、【例 9.1】声明一个整数类型的局部变量。declare mynumber int 【例 9.2】声明多个局部变量。declare name varchar(8),salary money,jointime datetime 【例 9.3】给【例 9.2】中的变量赋值。set name= 李智 set salary=$3700 set jointime= 2002-4-6【例 9.4】查询职工工号为s0415 的职工姓名及月工资并保存到局部变量name和salary 中。select name=姓名 ,salary= 月工资from 职工 where 职工工号 =s0415【例 9.5】查询职

2、工总人数,并存储到变量row 中。declare row int select row=(select count(*) from 职工 ) go 【例 9.6】 下面是一些正确的批处理:在影像租借数据库中如果已经存在表t, 就删除它,然后再重新建立表t,并查询是否存在表t. use 影像租借if object_id(dbo.t) is not null drop table dbo.t go create table t ( member_no char(7), birth_data datetime ) go /* 显示结果 */ select table_name from inform

3、ation_schema.tables where table_name=t go 【例 9.7】使用注释语句。- 对影像租借数据库进行操作,先打开数据库use 影像租借go /* 以下语句表示查询职工表中所有职工信息 */ select * from 职工go /* 下面第一语句查询分公司信息。第二条语句go表示提交一个批处理。 */ select * from 分公司go 【例 9.8】显示工号为s0003 的职工的姓名。declare name varchar(10) select name=姓名 from 职工 where 职工工号 =s0003 print 姓名: +name 【例

4、9.9】在职工表中查询姓名叫李智的职工,如果有记录,显示其工号和所在分公司编号,如果没有这个人,则显示“查无此人”。if exists(select * from 职工 where 姓名 = 李智 ) begin declare number char(5) declare br_num char(4) select number=职工工号 ,br_num=分公司编号from 职工where 姓名 = 李智 print 工号 +number print 分公司编号 +br_num end else print查无此人 【例 9.10】查询职工平均月工资是否高于3000,并显示相应信息。decl

5、are avg_sal money select avg_sal=avg( 月工资 ) from 职工if avg_sal3000 print平均月工资高于3000 else print平均月工资低于3000 【例 9.11】在 case 表达式中查询职工姓名及所在分公司,并修改分公司编号内容。select 姓名 , 分公司编号 = case when 分公司编号 =b001 then 子公司 1 when 分公司编号 =b002 then 子公司 2 when 分公司编号 =b003 then 子公司 3 end from 职工或者:select 姓名 , case 分公司编号when b0

6、01 then 子公司 1 when b002 then 子公司 2 when b003 then 子公司 3 end as 分公司编号 from 职工【例 9.12】求 1 到 100 之间的奇数和。declare n smallint,sum smallint set n=1 set sum=0 while n=0 begin set n=n+1 if n100 begin select 1到 100 之间的奇数和=sum break end if (n%2)=0 continue else set sum=sum+n end 【例 9.13 利用 goto 语句求 10 的阶乘。decl

7、are jc int,times int select jc=1,times=1 label1: select jc=jc*times select times=times+1 if times=10 goto label1 print10的阶乘为 +str(jc) 【例 9.14】设置在9:00 执行一次查询操作,查看职工信息。begin waitfor time 9:00:00select * from 职工end 【例 9.15】设置在1 小时后执行一次查询操作,查看职工信息。begin waitfor delay 1:00:00select * from 职工end 【例 9.16】职

8、工表中月工资是数值型,可以转为字符型,从而在其上进行字符串的匹配查找。select 姓名 , 月工资from 职工where cast( 月工资 as varchar(7) like 3% 【例 9.17】简单事务编程示例。begin tran mytran use 影像租借go update 职工set 月工资 =月工资 *1.1 where 月工资 3000 go delete 分公司where 分公司编号 =b003 if error0 begin rollback tran mytran print 删除记录出错 return end commit tran mytran go 【例

9、9.18】建立一个游标并用来查询分公司的经理的姓名及所属分公司编号。use 影像租借go /* 定义两个变量分别用来存放姓名和分公司编号 */ declare name varchar(8),branch char(5) /* 声明游标 */ declare managers cursor for select 姓名 , 分公司编号from 职工where 职务 = 经理 order by 分公司编号/* 打开游标 */ open managers /* 提取游标 */ fetch next from managers into name,branch /* 与 while 语句一起取到游标结

10、果集中的每行数据 */ while fetch_status=0 begin select name,branch fetch next from managers into name,branch end /* 关闭游标 */ close managers /* 释放游标 */ deallocate managers 【例 9.19】建立包含分公司b0001 的所有职工信息的游标,给 b0001 的职工增加工资,经理增加5%,普通职员增加6 %。use 影像租借go /* 定义变量存放职务 */ declare job varchar(6) /* 声明游标 */ declare branch

11、1 cursor for select 职务from 职工where 分公司编号 =b001 for update of 月工资/* 打开游标 */ open branch1 /* 提取游标 */ fetch next from branch1 into job /* 与 while 语句一起取到游标结果集中的每行数据 */ while fetch_status=0 begin if job=经理 begin update 职工set 月工资 =月工资 *1.05 where current of branch1 end else begin update 职工set 月工资 =月工资 *1.

12、06 where current of branch1 end fetch next from branch1 into job end /* 关闭游标 */ close branch1 /* 释放游标 */ deallocate branch1 /* 读取数据进行验证 */ select * from 职工 where 分公司编号 =b001【例 9.20】通过游标删除会员登记表中2002 年登记的会员记录。use 影像租借go declare branch char(4),member char(7),stuff char(5),time datetime /* 声明游标 */ declare members cursor for select 分公司编号 , 会员号 , 职工工号 , 加入时间from 会员登记for update of 分公司编号 , 会员号 , 职工工号 , 加入时间open members fetch next from members into branch,member,stuf

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论