[英]Mysql stored procedure syntax
I am trying to write simple mysql stored procedure and it seems that I can't get it right, so far I have 我正在尝试编写简单的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
but I get syntax error in line 4: 但我在第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
this message error was returned to me by phpMyAdmin, can anyone tell me why do I get an error? 这个消息错误由phpMyAdmin返回给我,任何人都可以告诉我为什么会出现错误?
edit: modified version, still not good 编辑:修改版,仍然不好
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
now error in line 14 and message: 现在第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
The problem is that you misspelled DECLARE
: 问题是你拼错了DECLARE
:
delcare dbRecord varchar(50);
UPDATE: For your next error, the problem is your illegal use of NOT EXISTS
. 更新:对于您的下一个错误,问题是您非法使用NOT EXISTS
。
Within a stored procedure the proper approach is to count the existing rows, and then conditionally insert a value if the count is 0. 在存储过程中,正确的方法是计算现有行,然后在计数为0时有条件地插入值。
Something like this: 像这样的东西:
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;
PS To avoid poor performance on on your select query, you should consider using a collation that is not case-sensitive so you don't need to apply the LOWER()
function to the artistname column. PS为了避免在您的选择查询上出现性能不佳,您应该考虑使用不区分大小写的排序规则,这样您就不需要将LOWER()
函数应用于artistname列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.