SQL存储过程实例详解(二)

2014-11-24 13:18:18 · 作者: · 浏览: 4
_time)VALUES('T010','1002','B002','2007-9-15','2008-1-5') INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T011','1004','B003','2007-12-28',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T012','1002','B003','2007-12-30',null) 标准答案: -- 1)查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的学生编号、学生名称、图书编号、图书名称、借出日期—
select 学生编号=stuID,学生名称=(select stuName from student where stuID=borrow.stuID),图书编号=BID,图书名称=(select title from book where BID=borrow.BID),借出日期=T_time from borrow where stuID in (select stuID from student where major='计算机') and T_time>'2007-12-15' and T_time<'2008-1-8'
-- 2)查询所有借过图书的学生编号、学生名称、专业--
select 学生编号=stuID,学生名称=stuName,专业=major from student where stuID in (select stuID from borrow)
-- 3)查询借过作者为“安意如”的图书的学生姓名、图书名称、借出日期--
select 学生名称=(select stuName from student where stuID=borrow.stuID),图书名称=(select title from book where BID=borrow.BID),借出日期=T_time,归还日期=B_time from borrow where BID in (select BID from book where author='安意如')
-- 4)查询目前借书但未归还图书的学生名称及未还图书数量--
select 学生名称=(select stuName from student where stuID=borrow.stuID),借书数量=count(*) from borrow where B_time is null group by stuID

题目2

程序员工资表:ProWage

字段名称

数据类型

说明

ID

int

自动编号,主键

PName

Char(10)

程序员姓名

Wage

int

工资

创建一个存储过程,对程序员的工资进行分析,月薪1500到10000不等,如果有百分之五十的人薪水不到2000元,给所有人加薪,每次加100,再进行分析,直到有一半以上的人大于2000元为止,存储过程执行完后,最终加了多少钱?
例如:如果有百分之五十的人薪水不到2000,给所有人加薪,每次加100元,直到有一半以上的人工资大于2000元,调用存储过程后的结果如图:
\
请编写T-SQL来实现如下功能:
1) 创建存储过程,查询是否有一半程序员的工资在2200、3000、3500、4000、5000或6000元之上,如果不到分别每次给每个程序员加薪100元,至之一半程序员的工资达到2200,3000,3500,4000,5000或6000元。
2) 创建存储过程,查询程序员平均工资在4500元,如果不到则每个程序员每次加200元,至到所有程序员平均工资达到4500元。
建表语句:
USE master
GO
/*$$$$$$$$$$$$$建库$$$$$$$$$$$$$$$$$$$$$$$$*/
--检验数据库是否存在,如果为真,删除此数据库--
IF exists(SELECT * FROM sysdatabases WHERE name='Wage')
  DROP DATABASE Wage
GO
CREATE DATABASE Wage
GO

--建数据表--
USE Wage
GO
CREATE TABLE ProWage  --程序员工资表
(
  ID int identity(1,1) primary key,  --工资编号
  PName  CHAR(10) NOT NULL ,     --程序员姓名
  Wage  int NOT NULL    --工资
)
GO
--插入数据--
INSERT INTO ProWage(PName,Wage)VALUES('青鸟',1900)
INSERT INTO ProWage(PName,Wage)VALUES('张三',1200)
INSERT INTO ProWage(PName,Wage)VALUES('李四',1800)
INSERT INTO ProWage(PName,Wage)VALUES('二月',3500)
INSERT INTO ProWage(PName,Wage)VALUES('蓝天',2780)
标准答案:
--1、创建存储过程--
if exists (select * from sysobjects where name='Sum_wage')
drop procedure Sum_wage
GO
create procedure Sum_wage 
@PWage int,
@AWage int,
@total int
as 
while (1=1)
begin
if (select count(*) from ProWage)>2*(select count(*) from ProWage where Wage>=@PWage)
update ProWage set @total=@total+@AWage,Wage=Wage+@AWage
else
break
end
print'一共加薪:'+convert(varchar,@total)+'元'
print'加薪后的程序员工资列表:'
select * from ProWage
--调用存储过程1--
exec Sum_wage @PWage=2000,@AWage=100,@total=0
exec Sum_wage @PWage=2200,@AWage=100,@total=0
exec Sum_wage @PWage=3000,@AWage=100,@total=0
exec Sum_wage @PWage=4000,@AWage=100,@total=0
exec Sum_wage @PWage=5000,@AWage=100,@total=0
exec Sum_wage @PWage=6000,@AWage=100,@total=0

--2、创建存储过程2--
if exists (select * from sysobjects where name='Avg_wage')
drop procedure Avg_wage
GO
create procedure Avg_wage 
@PWage int,
@AWage int,
@total int
as 
while (1=1)
begin