存储过程语法二
1、 创建语法
create proc | procedure pro_name
[{@参数数据类型} [=默认值] [output],
{@参数数据类型} [=默认值] [output],
....
]
as
SQL_statements
2、 创建不带参数存储过程
--创建存储过程
if (exists (select * from sys.objects where name = 'proc_get_student'))
drop proc proc_get_student
go
create proc proc_get_student
as
select * from student;
--调用、执行存储过程
exec proc_get_student;
3、 修改存储过程
--修改存储过程 alter proc proc_get_student as select * from student;
4、 带参存储过程
1 --带参存储过程 2 if (object_id('proc_find_stu', 'P') is not null) 3 drop proc proc_find_stu 4 go 5 create proc proc_find_stu(@startId int, @endId int) 6 as 7 select * from student where id between @startId and @endId 8 go 9 10 exec proc_find_stu 2, 4;
5、 带通配符参数存储过程
1 --带通配符参数存储过程 2 if (object_id('proc_findStudentByName', 'P') is not null) 3 drop proc proc_findStudentByName 4 go 5 create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%') 6 as 7 select * from student where name like @name and name like @nextName; 8 go 9 10 exec proc_findStudentByName; 11 exec proc_findStudentByName '%o%', 't%';