SQL Server存储过程与触发器课堂练习及答案_第1页
SQL Server存储过程与触发器课堂练习及答案_第2页
SQL Server存储过程与触发器课堂练习及答案_第3页
SQL Server存储过程与触发器课堂练习及答案_第4页
SQL Server存储过程与触发器课堂练习及答案_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

1、存储过程与触发器课堂练习及答案1.创建一个存储过程,显示所有价格在15美元以下的书的书名,类型,价格。CREATE PROCEDURE show_titleASSELECT title,type,priceFROM titlesWHERE price < 15GOEXEC show_title2.把价格作为参数,创建一个能显示在某两个指定价格之间的书的书名,类型,价格。CREATE PROCEDURE show_title2price1 money,price2 moneyASSELECT title,type,priceFROM titlesWHERE price between pr

2、ice1 and price2GOshow_title2 12,203.使用OUTPUT参数,创建一个计算圆柱体体积的存储过程。并执行它。CREATE PROCEDURE comp_arear smallint,h smallint,result decimal(10,2 OUTPUTASSET result = PI(*SQUARE(r* hGODECLARE answer decimal(10,2EXECUTE comp_area 2,3, answer OUTPUTSELECT 'The result is: ', answer4.A 建立price_change表,准备

3、用来存放书的价格变化信息,有以下几列:title_id, type,old_price, new_price, change_date, operator。B 建立一个更新触发器,一旦titles表发生更新,立即把相关信息存放到price_change表中。create table price_change(title_id varchar(20,type varchar(20,old_price money,new_price money,change_date datetime,operator varchar(20gocreate trigger tri_priceon titlesfo

4、r updateasinsert into price_changeselect o.title_id,o.type,o.price,n.price,getdate(,user_name(from deleted o JOIN inserted nON o.title_id = n.title_idgoupdate titles set price = price*1.15.修改练习4,使得只有当price列被更新时,才会触发触发器。create trigger tri_priceon titlesfor updateasif update(pricebegininsert into pric

5、e_changeselect o.title_id,o.type,o.price,n.price,getdate(,user_name(from deleted o JOIN inserted nON o.title_id = n.title_idendgo6.创建一个存放书的编号、书名、类型、价格、对应作者的编号、姓名、电话、住址的视图。use pubsgocreate view v_titledetailasselect t.title_id, title, type, price, a.au_id, au_lname, au_fname, phone, addressfrom title

6、s t,titleauthor ta,authors awhere t.title_id = ta.title_id and ta.au_id = a.au_id7.为这个视图创建一个Instead of更新触发器,把对视图的更新放到触发器里面来做。(假设,我们只允许更新这个视图的某几个列:price, phone, addresscreate trigger tri_titledetailon v_titledetailinstead of updateasdeclare price money,phone varchar(20,address varchar(40,title_id var

7、char(20,au_id varchar(20select title_id = title_id, price = price, au_id = au_id, phone = phone ,address = address from insertedupdate titles set price = price where title_id = title_idupdate authors set phone = phone, address = address where au_id = au_idgo8.添加测试数据。更新v_titledetail,把书编号为LI1234的书的价格改为2

温馨提示

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

评论

0/150

提交评论