简体   繁体   中英

can't create mysql stored procedure

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?

您需要在CREATE PROCEDURE clone_perms之后使用BEGIN标记,而不要使用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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM