Mysql mariaDB语法错误(带有if语句和参数的存储过程)
你好,你好。。 我需要一些关于使用shell或cmd查询存储过程的帮助` 这是我的书桌 如果我使用一个没有If语句的操作,比如下面的查询,它就可以正常工作 但是如果我使用一个if语句并放入一个参数,比如下面的查询 它将返回这样的错误Mysql mariaDB语法错误(带有if语句和参数的存储过程),mysql,mariadb,Mysql,Mariadb,你好,你好。。 我需要一些关于使用shell或cmd查询存储过程的帮助` 这是我的书桌 如果我使用一个没有If语句的操作,比如下面的查询,它就可以正常工作 但是如果我使用一个if语句并放入一个参数,比如下面的查询 它将返回这样的错误 您的代码有很多问题。首先,IF语句需要在条件之后加上一个THEN,并且必须以END IF终止。其次,过程中的所有语句都必须以;结尾;。请尝试以下方法: create procedure insertUpdateSelectDelete( id int(11)
您的代码有很多问题。首先,IF语句需要在条件之后加上一个THEN,并且必须以END IF终止。其次,过程中的所有语句都必须以;结尾;。请尝试以下方法:
create procedure insertUpdateSelectDelete(
id int(11),
lname varchar(50),
fname varchar(50),
mname varchar(50),
statementtype varchar(30)
)
begin
if statementtype = 'Insert' then
insert into bookAuthor (authorLname,authorFname,authorMname)
values (lname,fname,mname);
elseif statementtype = 'Update' then
update bookAuthor
set authorLname = lname, authorFname = fname, authorMname = mname
where authorID = id;
elseif statementtype = 'Select' then
select * from bookAuthor;
elseif statementtype = 'Delete' then
delete from bookAuthor where authorID = id;
end if;
end
请注意,如果您像我一样以if/ELSEIF块的形式编写过程,则不需要严格使用BEGIN/END包装器,因为过程中只有一条语句
这是一个工作流程图。@JunCain太好了。很高兴我能帮忙。您缺少分隔符语句。
MariaDB [library]> create procedure selectAll()
-> select * from bookAuthor;
Query OK, 0 rows affected (0.60 sec)
MariaDB [library]> create procedure insertUpdateSelectDelete
-> (
-> id int(11),
-> lname varchar(50),
-> fname varchar(50),
-> mname varchar(50),
-> statementtype varchar(30)
-> )
-> if statementtype = 'Insert'
-> insert into bookAuthor
-> (
-> authorLname,
-> authorFname,
-> authorMname
-> )
-> values
-> (
-> lname,
-> fname,
-> mname
-> )
-> if statementtype = 'Update'
-> update bookAuthor set
-> authorLname = lname,
-> authorFname = fname,
-> authorMname = mname
-> where authorID = id
-> if statementtype = 'Select'
-> select * from bookAuthor
-> if statementtype = 'Delete'
-> delete from bookAuthor where authorID = id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into bookAuthor
(
authorLname,
authorFname,
authorMname
)
values
(
lname,' at line 10
create procedure insertUpdateSelectDelete(
id int(11),
lname varchar(50),
fname varchar(50),
mname varchar(50),
statementtype varchar(30)
)
begin
if statementtype = 'Insert' then
insert into bookAuthor (authorLname,authorFname,authorMname)
values (lname,fname,mname);
elseif statementtype = 'Update' then
update bookAuthor
set authorLname = lname, authorFname = fname, authorMname = mname
where authorID = id;
elseif statementtype = 'Select' then
select * from bookAuthor;
elseif statementtype = 'Delete' then
delete from bookAuthor where authorID = id;
end if;
end