sql存储过程begin
ALTER procere [dbo].[wvsp_updateTaskStatus]
@ttype int,
@id int,
@status int // 这一段主要是定义存储过程需要的参数
as
begin 开始存储过程
if @ttype=0 // 开始判断 如果传进来的@ttype=0
begin //开始执行
if (@status=3) //如果(@status=3)
begin //开始执行函数
insert into tb_queuelog([type],mid,code,otherparams,created) //往表名为tb_queuelog的表插入数据,以上为要插入的字段
select @ttype,mid,code,otherparams,created
from tb_mergequeue
where id=@id //要插入字段的数据从表tb_mergequeue 搜索出来,与上表要插入的字段一一对应,搜索条件为:id=@id
delete from tb_mergequeue
where id=@id //删除tb_mergequeue表中where id=@id的字段
end //结束@status=3条件循环
else//如果(@status不等于3,进入另一个方法
update tb_mergequeue
set status=@status
where id=@id //更新tb_mergequeue的数据
end //结束(@status不等于3的条件循环
else if(@ttype=1) //如果 (@ttype=1 -- 页面同步任务
begin开始
if (@status=3)
begin
insert into tb_queuelog([type],mid,code,otherparams,created)
select @ttype,mid,code,otherparams,created
from tb_syncqueue
where id=@id
delete from tb_syncqueue
where id=@id
end
else
update tb_syncqueue
set status=@status
where id=@id
end
select @@rowcount //取得记录总数
end 结束存储过程
B. SQL的存储过程 语法格式是什么
这里以创建名为 GetStuCou 的无参数存储过程为例:
create procere 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
(2)sql存储过程begin扩展阅读:
创建存储过程的注意事项:
1、保持事务简短,事务越短,越不可能造成阻塞。
2、在事务中尽量避免使用循环while和游标,以及避免采用访问大量行的语句。
3、在启动事务前完成所有的计算和查询等操作,避免同一事务中交错读取和更新。可以使用表变量预先存储数据。即存储过程中查询与更新使用两个事务实现。
4、超时会让事务不执行回滚,超时后如果客户端关闭连接sqlserver自动回滚事务。如果不关闭,将造成数据丢失,而其他事务将在这个未关闭的连接上执行,造成资源锁定,甚至服务器停止响应。
5、避免超时后还可打开事务 SET XACT_ABORT ON统计信息可以优化查询速度,统计信息准确可以避免查询扫描,直接进行索引查找。
C. SQL的存储过程中的事物处理是begin tran 还是begin transaction
俩个都行,tran是transaction的简写形式,就想存储过程:proc是process的简写一样的.
D. SQL 中存储过程怎么使用
一、简单的储存过程:
1、创建一个存储过程
create procere GetUsers()
begin
select * from user;
end;12345
2、调用存储过程
call GetUsers();12
3、删除存储过程
drop procere if exists GetUsers;
二、带参数的存储过程
1、MySql 支持 IN (传递给存储过程) , OUT (从存储过程传出) 和 INOUT (对存储过程传入和传出) 类型的参数 , 存储过程的代码位于 BEGIN 和 END 语句内 , 它们是一系列 SQL 语句 , 用来检索值 , 然后保存到相应的变量 (通过指定INTO关键字) ;
2、下面的存储过程接受三个参数 , 分别用于获取用户表的最小 , 平均 , 最大分数 , 每个参数必须具有指定的类型 , 这里使用十进制值(decimal(8,2)) , 关键字 OUT 指出相应的参数用来从存储过程传出
create procere GetScores(
out minScore decimal(8,2),
out avgScore decimal(8,2),
out maxScore decimal(8,2)
)
begin
select min(score) into minScore from user;
select avg(score) into avgScore from user;
select max(score) into maxScore from user;
end;1234567891011
3、调用此存储过程 , 必须指定3个变量名(所有 MySql 变量都必须以@开始) , 如下所示 :
call GetScores(@minScore, @avgScore, @maxScore);12
4、该调用并没有任何输出 , 只是把调用的结果赋给了调用时传入的变量@minScore, @avgScore, @maxScore, 然后即可调用显示该变量的值 :
select @minScore, @avgScore, @maxScore;
5、使用 IN 参数 , 输入一个用户 id , 返回该用户的名字 :
create procere GetNameByID(
in userID int,
out userName varchar(200)
)
begin
select name from user
where id = userID
into userName;
end;12345678910
6、调用存储过程 :
call GetNameByID(1, @userName);
select @userName;123
E. 在SQL里面写存储过程的时候 加“Begin End”和不加有什么区别么
这个其实就是相当于你程序里的大括号,用来识别你的代码开始和结束的位置,方便分析器执行,如果你的代码不多,不用begin
和end没有什么事情,如果是一个语句很多的存储过程了,你要加上,不然系统会分辨不出来,就报错了
F. 什么是SQL的存储过程
sql存储过程说简单点就是一个在t-sql下用户可以自行定义的函数,
但是与一般的函数也有不同的地方,比如它的返回值只能return(int类型),如果你要输出什么信息的话只能用output.这也是存储过程的一个特色吧,设定的参数可以有输出。讲起来有点抽象,给你个例子看看吧!
首先创建一个存储过程
create
procere
cunchuguocheng
@a
int,
@b
int,
@c
int
output
as
begin
select
@c
=
@a+@b
return(0)
end
然后调用这个存储过程
declare
@value
int,
--返回值
@c
int
--结果值
exec
@value
=
cunchuguocheng
2,2,@c
output
select
@value
as
返回值
select
@c
as
结果值
程序写的很简单,你运行一下我想你就会对存储过程有所了解了。
G. 在SQL中存储过程的一般语法是什么
1、 创建语法
createproc|procerepro_name
[{@参数数据类型}[=默认值][output],
{@参数数据类型}[=默认值][output],
....
]
as
SQL_statements
2、 创建不带参数存储过程
--创建存储过程
if(exists(select*fromsys.objectswherename='proc_get_student'))
dropprocproc_get_student
go
createprocproc_get_student
as
select*fromstudent;
--调用、执行存储过程
execproc_get_student;
3、 修改存储过程
--修改存储过程
alterprocproc_get_student
as
select*fromstudent;
4、 带参存储过程
--带参存储过程
if(object_id('proc_find_stu','P')isnotnull)
dropprocproc_find_stu
go
createprocproc_find_stu(@startIdint,@endIdint)
as
select*fromstudentwhereidbetween@startIdand@endId
go
execproc_find_stu2,4;
5、 带通配符参数存储过程
--带通配符参数存储过程
if(object_id('proc_findStudentByName','P')isnotnull)
dropprocproc_findStudentByName
go
createprocproc_findStudentByName(@namevarchar(20)='%j%',@nextNamevarchar(20)='%')
as
select*fromstudentwherenamelike@nameandnamelike@nextName;
go
execproc_findStudentByName;execproc_findStudentByName'%o%','t%';
(7)sql存储过程begin扩展阅读:
SQL存储过程优点:
1、重复使用。存储过程可以重复使用,从而可以减少数据库开发人员的工作量。
2、减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。
3、安全性。参数化的存储过程可以防止SQL注入式攻击,而且可以将Grant、Deny以及Revoke权限应用于存储过程。
H. sql存储过程
if(@@error<>0)
begin
ROLLBACK TRAN
set @errMsg = '数据库执行出错!';
end
ELSE
BEGIN
COMMIT TRAN
END
I. sql存储过程
一、简单的储存过程:
1、创建一个存储过程
create procere GetUsers()
begin
select * from user;
end;12345
2、调用存储过程
call GetUsers();12
3、删除存储过程
drop procere if exists GetUsers;
二、带参数的存储过程
1、MySql 支持 IN (传递给存储过程) , OUT (从存储过程传出) 和 INOUT (对存储过程传入和传出) 类型的参数 , 存储过程的代码位于 BEGIN 和 END 语句内 , 它们是一系列 SQL 语句 , 用来检索值 , 然后保存到相应的变量 (通过指定INTO关键字) ;
2、下面的存储过程接受三个参数 , 分别用于获取用户表的最小 , 平均 , 最大分数 , 每个参数必须具有指定的类型 , 这里使用十进制值(decimal(8,2)) , 关键字 OUT 指出相应的参数用来从存储过程传出
create procere GetScores(
out minScore decimal(8,2),
out avgScore decimal(8,2),
out maxScore decimal(8,2)
)
begin
select min(score) into minScore from user;
select avg(score) into avgScore from user;
select max(score) into maxScore from user;
end;1234567891011
3、调用此存储过程 , 必须指定3个变量名(所有 MySql 变量都必须以 @ 开始) , 如下所示 :
call GetScores(@minScore, @avgScore, @maxScore);12
4、该调用并没有任何输出 , 只是把调用的结果赋给了调用时传入的变量 @minScore, @avgScore, @maxScore , 然后即可调用显示该变量的值 :
select @minScore, @avgScore, @maxScore;
5、使用 IN 参数 , 输入一个用户 id , 返回该用户的名字 :
create procere GetNameByID(
in userID int,
out userName varchar(200)
)
begin
select name from user
where id = userID
into userName;
end;12345678910
6、调用存储过程 :
call GetNameByID(1, @userName);
select @userName;123
参考资料
SQL存储过程使用介绍.csdn博客[引用时间2017-12-31]
J. SQL的存储过程中的事物处理是begin tran 还是begin transaction
标记一个显式本地事务的起始点,事务的处理的基本语法为:
BEGIN { TRAN | TRANSACTION }
[ { transaction_name | @tran_name_variable }
[ WITH MARK [ 'description' ] ]
]
[ ; ]
因此对于begin tran 或begin transaction都是支持的。tran 是transaction缩写。
同理对于end tran 也是一样的原理。