SQL 文本:
use Company
go
create PROC Select_PNAME
as
select PNAME from Project
exec Select_PNAME
结果截图:
SQL 文本:
use Company
go
create PROC add_Salary
as
update Employee
set SALARY=SALARY*1.1
go
exec add_Salary
go
select SSN,FNAME,LNAME,SALARY from Employee
结果截图:
在执行存储过程前进行一次查询,结果如下。
在执行存储过程后进行一次查询,结果如下。
SQL 文本:
use Company
go
create PROC emp_info
as
select * from EMPLOYEE where SALARY>30000
go
exec emp_info
go
结果截图:
SQL 文本:
use Company
go
create PROC employ_info(@number int=30000)
as
select * from Employee where SALARY>@number
go
exec employ_info @number=40000
go
结果截图:
当没有给定参数
当给定参数为40000
SQL 文本:
use Company
go
create proc pro_info(@likestring varchar(30)='p%')
as
if(@likestring='p%') begin
select * from Project where PNAME like @likestring
end else begin
select * from Project where PNAME like '%'+@likestring+'%'
end
go
exec pro_info 'new'
go
结果截图:
提供参数
不提供参数
SQL 文本:
use Company
go
create PROC out_project(@pro varchar(50),@avgSalary int OUTPUT,@maxSalary int OUTPUT,@minSalary int OUTPUT)
as
select @avgSalary=AVG(e.SALARY),@maxSalary=MAX(e.SALARY),@minSalary=MIN(e.SALARY)
from Employee e,WORKS_ON w,Project p
where p.PNAME=@pro
AND w.PNO=p.PNUMBER
AND e.SSN=w.ESSN
return
go
declare @pro varchar(50),@avgSalary int,@maxSalary int,@minSalary int
set @pro='ProductX'
exec out_project @pro,@avgSalary OUTPUT,@maxSalary OUTPUT,@minSalary OUTPUT
select @avgSalary,@maxSalary,@minSalary
go
结果截图:
SQL 文本:
use Company
go
create proc Select_like(@slike varchar(20))
as
declare @i int
select @i=COUNT(*) from Employee where FNAME like '%'+@slike+'%'
if(@i=0) return -1
if(@i=1) return 0
if(@i>1) return 1
go
declare @status int
exec @status=Select_like 'z'
print @status
go
结果截图:
当输入’z’,时,结果应该返回-1
当输入’ohn’,结果应该返回0
当输入’n’,结果应该返回1
SQL 文本:
use Company
go
create PROC out_depart(@dnumber varchar(50),@avgSalary varchar(20) OUTPUT)
as
declare @count int
select @count=COUNT(*) from Department where DNUMBER=@dnumber
if(@count=0)
begin
set @avgSalary='该部门不存在'
end
else begin
select @avgSalary= AVG(e.SALARY)
from Employee e,Department d
where d.DNUMBER=@dnumber
and e.DNO=d.DNUMBER
end
return
go
declare @dnumber varchar(50),@avgSalary varchar(20)
set @dnumber='5'
exec out_depart @dnumber,@avgSalary OUTPUT
select @avgSalary as 平均
go
结果截图:
当输入一个不存在的部门号“10”,输出该部门不存在。
当输入一个存在的部门号“5”,正确输出结果。