實驗5.1 儲存過程的建立與使用
一、實驗目的
理解儲存過程的概念、作用、建立和呼叫方法。
二、實驗原理
使用CREATE PROCEDURE語句建立儲存過程,ALTER PROCEDURE語句修改儲存過程,DROP PROCEDURE語句刪除儲存過程,儲存過程有不帶引數的、有帶輸入引數的、有帶輸出引數(output)的,還可以有帶返回值的。建立好的儲存過程可以使用EXECprocedure_name語句執行。
三、實驗裝置
安裝有SQL SERVER 2008的計算機。
四、實驗示例
1、模糊查詢
create procedure proc_empname @E_name varchar(10)
as
selecta.emp_name,a.dept,b.tot_amt
fromemployee a inner join sales b
ona.emp_no=b.sale_id
wherea.emp_name like @E_name
go
exec sp_empname'陳%'
2、利用儲存過程計算出’E0014’業務員的銷售總金額。
create procedure proc_saletot @E_no char(5),@p_tot int output
as
select @p_tot=sum(tot_amt)
from sales
where [email protected]_no
go
declare @tot_amt int
exec sp_saletot E0014, @tot_amt output
select @tot_amt
3、建立一帶返回值的儲存過程,返回某一部門的平均工資
create proc proc_avg_salary @Dept char(4)
as
[email protected]_salary int
[email protected]_salary=avg(salary)
from employee
where [email protected]
[email protected]_salary
print'返回值='+cast(@avg as char(10))
五、實驗內容
1、利用儲存過程,給employee表新增一條業務部門員工的資訊。
Create proc sp_Insert_employee
@emp_no char(5), @emp_name varchar(10), @Sex char(2), @dept varchar(10),
@title varchar(10) , @date_hired datetime ,@birthday datetime ,
@salary int,@telephone varchar(20),@addr varchar(50),@rtn int output
as
declare
@emp_name2 varchar(10), @Sex2 char(2), @dept2 varchar(10), @title2 varchar(10),
@date_hired2 datetime ,@birthday2 datetime,
@salary2 int,@telephone2 varchar(20)
if exists(select * from employee where [email protected]_no)
begin
select @emp_name2=emp_name, @Sex2=sex, @dept2=dept, @title2=title,
@date_hired2=date_hired,@birthday2=birthday,
@salary2=salary,@telephone2=telephone
from Student where [email protected]_no
if ((@[email protected]_name)and (@[email protected])and( @[email protected])and ( @[email protected])
and (@[email protected]_hired)and (@[email protected]) and (@[email protected]) and (@[email protected]))
/*有完全相同的資料*/
begin
set @rtn=0
end
else
begin
/*更新資料*/
update employee set [email protected]_name, [email protected], [email protected], [email protected],
[email protected]_hired,[email protected],[email protected],[email protected]
where [email protected]_no
set @rtn=2
end
end
else
begin
insert into employee values(@emp_no, @emp_name, @Sex, @dept , @title, @date_hired ,@birthday ,
@salary ,@telephone ,@addr )
set @rtn=1
end
insert employee values('E000145','王大華','e','業務','經理','1976-10-13','1951-08-01',8000,'0218120440','上海市');
2、利用儲存過程從employee、sales、customer表的連線中返回所有業務員的姓名、客戶姓名、銷售金額。
create proc infor
as
begin
select emp_name,cust_name,tot_amt
from sales,customer,employee
where sale_id = emp_no and customer.cust_id = sales.cust_id
end
go
exec infor
3、建立帶一個輸入引數的儲存過程,實現按員工姓名進行模糊查詢,查詢員工編號、訂單編號、銷售金額。
create procedure query @name varchar(10)
as
select sale_id,order_no,sum(tot_amt)銷售金額
from sales where sale_id in(select emp_no from employee
where emp_name like @name)
group by sale_id,order_no
exec query '劉%'
4、建立帶兩個輸入引數的儲存過程,查詢姓“李”並且職稱為“職員”的員工的員工編號、訂單編號、銷售金額。
create proc infer3
as
begin
select emp_name,emp_no,order_no,tot_amt
from employee,sales
where title='職員'and emp_name like '李%'and
emp_no=sale_id
end
go
exec infer3
5、利用儲存過程計算出訂單編號為10003的訂單的銷售金額。(帶一輸入引數和一輸出引數)(提示:sales表中的tot_amt應該等於sale_item表中的同一張訂單的不同銷售產品的qty*unit_price之和)
create procedure inout(
@order_no char(5),
@p_tot int output
)
as
select @p_tot=sum(sale_item.qty*sale_item.unit_price)
from sales,sale_item
where [email protected]_no
declare @tot_amt int
exec inout 10003,@tot_amt output
select @tot_amt
6、建立一儲存過程,根據給出的職稱,返回該職稱的所有員工的平均工資。(帶一輸入引數和返回值)
create procedure avg_salary @title char(10)
as
declare @avg_salary int
select @avg_salary=avg(salary)
from employee
where [email protected]
return @avg_salary
declare @avg int
exec @avg=avg_salary '職員'
print '職員的平均工資是:=' + cast(@avg as char(10))