Home > Database > Mysql Tutorial > 封装游标存储过程(Pr_execsql2)_MySQL

封装游标存储过程(Pr_execsql2)_MySQL

WBOY
Release: 2016-06-01 13:56:30
Original
1218 people have browsed it

使用脚本自己可以执行自己的特性,封装游标操作存储过程如下:
CREATE PROCEDURE Pr_ExecSQL2
@Asqlstring VARCHAR(8000) --输入的SQL语句
AS
EXEC ('
--启动事务


BEGIN TRAN

DECLARE @ASql VARCHAR(8000)
DECLARE tnames_cursor CURSOR LOCAL FAST_FORWARD FOR '+ @asqlstring +'
OPEN tnames_cursor
FETCH NEXT FROM tnames_cursor INTO @ASql
WHILE (@@FETCH_STATUS=0)
BEGIN
print @ASql
EXEC (@ASql)
IF @@ERROR 0 GOTO FINALEX99v
FETCH NEXT FROM tnames_cursor INTO @ASql
END
CLOSE tnames_cursor
DEALLOCATE tnames_cursor

COMM99v TRAN
RETURN

FINALEX99v:
ROLLBACK TRAN
CLOSE tnames_cursor
DEALLOCATE tnames_cursor

')
使用方法:
declare @sql varchar(8000)
set @sql='select ''update a set a.a='' + b.a from b '
exec Pr_execsql2 @sql

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template