2011-02-04 92 views
0

我有點小白,當談到一般的存儲過程,但我想寫以下不能創建MySQL存儲過程

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 [email protected]_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 [email protected]_id and [email protected]) 
    BEGIN 
     update permissions set [email protected]_off where [email protected]_id and [email protected] 
    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 

。嘗試創建時出現以下錯誤:

/* SQL錯誤(1064):您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第二行使用'as declare @new_id varchar(30)declare @old_id varchar(30)declare get_perm'使用正確的語法*/

。有誰知道我需要做什麼來完成這項工作?

+0

請在您的問題中包含服務器版本 – 2011-02-04 14:29:05

+0

我正在使用5.1.45社區 – Jonathan 2011-02-04 14:40:09

回答

0

你需要有BEGIN標籤CREATE PROCEDURE clone_perms後不AS

0

不要使用@來啓動本地(聲明)變量名,那是隻爲你與

SET @varname創建用戶變量=值;

聲明。您還需要用分號終止您的語句。這是最新錯誤的原因,沒有;在你的第一個聲明之後。