开发者

can't create mysql stored procedure

开发者 https://www.devze.com 2023-02-09 02:40 出处:网络
I\'m a bit of a noob when it comes to stored procedures in general, but I\'m trying to write the following

I'm a bit of a noob when it comes to stored procedures in general, but I'm trying to write the following

Create procedure clone_perms 
as
declare @new_id varchar(30), @old_id varchar(30)

declare get_perms cursor for select userspermsUserid, userspermsPermission from users_permissions where userspermsUserid=@old_id

declare @perms varchar(30), @on_off boolean

FETCH get_perms into @perms, @on_off
while(@@sqlstatus=0)
BEGIN
    if exists ( select 1 from permissions where userspermsUserid=@new_id and userspermsPermID=@perm )
    BEGIN 开发者_高级运维  
        update permissions set userspermsPermission=@on_off where userspermsUserid=@new_id and userspermsPermID=@perm
    END
    else
    BEGIN
        insert permissions (userspermsUserID, userspermsPermID, userspermsPermission) values (@new_id, @perms, @on_off)
    END

    FETCH get_perms into @perms, @on_off
END
CLOSE get_perms  

DEALLOCATE CURSOR get_perms
end   

. I get the following error when trying to create it:

/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as declare @new_id varchar(30) declare @old_id varchar(30) declare get_perm' at line 2 */

. Does anyone know what I need to do to make this work?


You need to have BEGIN tag after CREATE PROCEDURE clone_perms and not AS


don't use an @ to start local (declared) variable names, that's only for user variables you create with the

SET @varname=value;

statement. you'll also need to terminate your statements with a semicolon. that's the cause of the latest error, there's no ; after your first declare statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消