SQL Server 存储过程入门学习

创建存储过程之前要先用use语句声明要将存储过程存储在哪个数据库中.

e.g

use company;

执行之.之后就可以声明存储过程了.

e.g

create procedure ***

@id varchar(50) , @name int output

as

select @name=name from table where id=@id

然后可以用alter对存储过程进行修改.

declare可以声明一个变量

用set对变量赋值

e.g

alter proc getempp

@ID int
as
declare @Department int
select @Department=a.Department
from company.dbo.employee a
inner join company.dbo.department b
on a.Department=b.ID
where a.ID=@ID
if @@ROWCOUNT>0
begin
select *
from company.dbo.department
where ID=@Department
select * from company.dbo.employee
where Department=@Department
end
else
begin
RAISERROR('No record found',10,1)
end

上例中的@Department是被选择出来的值,然后用来为下面的if语句块中的查询服务的.

posted @ 2010-05-17 16:13  damoyan  阅读(230)  评论(0编辑  收藏  举报