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、 带参存储过程
--带参存储过程
if (object_id('proc_find_stu', 'P') is not null)
drop proc proc_find_stu
go
create proc proc_find_stu(@startId int, @endId int)
as
select * from student where id between @startId and @endId
go
exec proc_find_stu 2, 4;
5、 带通配符参数存储过程
--带通配符参数存储过程
if (object_id('proc_findStudentByName', 'P') is not null)
drop proc proc_findStudentByName
go
create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
select * from student where name like @name and name like @nextName;
go
exec proc_findStudentByName;exec proc_findStudentByName '%o%', 't%';
扩展资料:
SQL存储过程优点:
1、重复使用。存储过程可以重复使用,从而可以减少数据库开发人员的工作量。
2、减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。
3、安全性。参数化的存储过程可以防止SQL注入式攻击,而且可以将Grant、Deny以及Revoke权限应用于存储过程。
参考资料来源:百度百科—存储过程
这里以创建名为 GetStuCou 的无参数存储过程为例:
create procedure GetStuCou
as
begin //开始存储过程
select * from Students left join Course c on s.C_S_Id=c.C_Id
end //结束存储过程
下面是存储过程的其他用法:
--创建存储过程
CREATE PROCEDURE PROC(后面接类型)
--定义变量--简单赋值
declare @a intset @a=5 print @a
--使用select语句赋值
declare @user1 nvarchar(50)
select @user1='张三'
print @user1
declare @user2 nvarchar(50)
--创建临时表1 create table #DU_User1
(
[ID] [int] NOT NULL,
[Oid] [int] NOT NULL,
);
--定义一个游标
declare user_cur cursor for select ID,Oid,[Login] from ST_User
--打开游标
open user_cur
while @@fetch_status=0 begin
--读取游标
fetch next from user_cur into @ID,@Oid,@Login
print @ID
--print @Login
end
close user_cur
扩展资料:
创建存储过程的注意事项:
1、保持事务简短,事务越短,越不可能造成阻塞。
2、在事务中尽量避免使用循环while和游标,以及避免采用访问大量行的语句。
3、在启动事务前完成所有的计算和查询等操作,避免同一事务中交错读取和更新。可以使用表变量预先存储数据。即存储过程中查询与更新使用两个事务实现。
4、超时会让事务不执行回滚,超时后如果客户端关闭连接sqlserver自动回滚事务。如果不关闭,将造成数据丢失,而其他事务将在这个未关闭的连接上执行,造成资源锁定,甚至服务器停止响应。
5、避免超时后还可打开事务 SET XACT_ABORT ON统计信息可以优化查询速度,统计信息准确可以避免查询扫描,直接进行索引查找。
本回答被网友采纳参考资料:开发者在线http://www.builder.com.cn/
本回答被提问者采纳一般分为十种情况,每种语法各不相同:
1、 创建语法
create proc | procedure pro_name2、 创建不带参数存储过程
--创建存储过程3、 修改存储过程
--修改存储过程4、 带参存储过程
--带参存储过程5、 带通配符参数存储过程
--带通配符参数存储过程6、 带输出参数存储过程
if (object_id('proc_getStudentRecord', 'P') is not null)7、 不缓存存储过程
--WITH RECOMPILE 不缓存8、 加密存储过程
--加密WITH ENCRYPTION9、 带游标参数存储过程
if (object_id('proc_cursor', 'P') is not null)10、 分页存储过程
---存储过程、row_number完成分页存储过程(stored procedure)有时也称为sproc。存储过程存储于数据库中而不是在单独的文件中,有输入参数、输出参数以及返回值等。
在数据库中,创建存储过程和创建其他对象的过程一样,除了它使用的AS关键字外。存储过程的基本语法如下:
CREATE PROCDUER|PROC <sproc name>
[<parameter name>[schema.]<data type>[VARYING][=<default value>][OUT [PUT]][,
[<parameter name>[schema.]<data type>[VARYING][=<default value>][OUT [PUT]][,
...]]
[WITH
RECOMPILE|ENCRYPTION|[EXECUTE AS {CALLER|SELF|OWNER|<'user name'>}]
[FOR REPLICATION]
AS
<code>|EXTERNAL NAME <assembly name>.<assembly class>
存储过程示例一:
执行存储过程方法一:
本回答被网友采纳