2012-02-10 61 views
0

我想寫簡單的MySQL的存儲過程,似乎我無法得到它的權利,到目前爲止,我MySQL存儲過程的語法

delimiter // 
    create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50)) 
    begin 
    declare dbArtist varchar(50); 
    delcare dbRecord varchar(50); 

    set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist)); 

    set dbRecord=(select recordname from records where lower(recordname)=lower(_record)); 
    if not exists (select * from Artists where lower(artistname)=lower(_artist)) then 
    begin 
     INSERT INTO `Artists`(`ArtistName`) VALUES (_artist); 
     set dbArtist=_artist; 
    end 

    if not exists (select * from Records as R inner join Artists as A on R.ArtistId=A.ArtistId where lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist) then 
    begin 
     INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ((select artistid from artists where artistname=dbArtist),_record); 
     set dbRecord=_record; 
    end 

    end 

但我得到語法錯誤在第4行:

#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 'dbRecord varchar(50); 
set dbArtist = (select artistname from artists where lowe' at line 4 

此消息錯誤是由phpMyAdmin的還給我,誰能告訴我爲什麼我得到一個錯誤?

編輯:修改後的版本,還沒有很好的

delimiter // 
create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50)) 
begin 
declare dbArtist varchar(50); 
declare dbRecord varchar(50); 

set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist)); 
set dbRecord=(select recordname from records where lower(recordname)=lower(_record)); 
if not exists (select * from Artists where lower(artistname)=lower(_artist)) then 
begin 
    INSERT INTO `Artists`(`ArtistName`) VALUES (_artist); 
    set dbArtist=_artist; 
end 

if not exists 
(select * from Records as R inner join Artists as A on R.ArtistId = A.ArtistId where  lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist) 
then 
begin 
    INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ((select artistid from artists where artistname=dbArtist) ,_record); 
    set dbRecord=_record; 
end 

end 

現在錯誤行14消息:

#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 'if not exists (select * from Records as R inner join Artists as A on R.ArtistId' at line 14 
+0

我希望您在使用新版本.. http://stackoverflow.com/questions/3356929/mysql-create-procedure-syntax-issue-line-1 – zod 2012-02-10 17:51:09

+0

我使用的MySQL 5.1 – Andna 2012-02-10 17:51:56

回答

2

的問題是,你拼錯DECLARE

delcare dbRecord varchar(50); 

UPDATE :你的下一個錯誤,問題是你的非法使用NOT EXISTS

在存儲過程中的正確方法是計算現有的行,然後有條件地插入值,如果計數爲0

事情是這樣的:

SELECT COUNT(*) 
INTO @v_row_count 
FROM Artists 
WHERE LOWER(artistname)=LOWER(_artist); 

IF (@v_row_count = 0) 
THEN 
    INSERT INTO `Artists`(`ArtistName`) VALUES (_artist); 
    set dbArtist=_artist; 
END IF; 

附:爲了避免業績不佳對您的選擇查詢,你應該考慮使用排序規則是不區分大小寫,所以你並不需要的功能LOWER()適用於ARTISTNAME列。

+0

我拼corectly現在宣佈,但這個時候,我得到錯誤行14 – Andna 2012-02-10 18:10:46

+0

你的'不使用不支持EXISTS'。看到我更新的答案。 – 2012-02-10 18:24:09

+0

好吧,現在它可以工作。我甚至沒有打這個用法不被支持,早期我用MSSQL,有是合法的使用這樣的方式不存在。謝謝。 – Andna 2012-02-10 18:28:33