SQL的存储过程 语法格式是什么?

如题所述

---判断是否存在存储过程
if exists( select name from sysobjects where name='proc_name' )
drop proc proc_name
go

---创建存储过程
create proc proc_name
@parameter varchar(20),
@parameter2 int
as
主体语句
go

if exists:判断是否存在
drop proc : 删除存储过程,后面接存储过程名称,名称不用引起来
create proc: 创建存储过程,后面接存储过程名称,名称不用引起来
@parameter varchar(20): 参数,在执行存储过程的时候需要传入的参数,这里是字符类型,如果有多个参数的话,除了最后一个参数,其他的参数后面需要加逗号(英文状态),如果不需要传入,直接去掉就行了。
create proc proc_name
as
主体语句
go

as : 关键字
主体语句:就是你要做的操作的语句,和平时的完全一样,存储过程就相当把平时的sql语句在外面加了个壳的感觉。比如这里你需要查询一个表 select * from tableName1 那么这里就这样写
create proc proc_name
as
select * from tableName1
go
传参数:
create proc proc_name
@parameter varchar(20)
as
select * from tableName where name=@parameter
go
执行的时候这样
exec proc_name '小明'
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-14
--[例7.1]不带参的
use pxscj
go
create procedure student_info
as
select *
from cjb
where xh='081101'
go

--[例7.2]带参的
use pxscj
go
create procedure student_info1 @name char(8),@cname char(16)
as
select a.xh,xm,kcm,cj,t.xf
from xsb a inner join cjb b
on a.xh=b.xh inner join kcb t
on b.kch=t.kch
where a.xm=@name and t.kcm=@cname
go
execute student_info1 '王林','计算机基础'