I am trying to create a stored procedure using PHP. My reading indicates the best way to do this by running the.sql file using the 'exec' command in PHP.
In testing i created a file named amtv3_create_routines.sql with this contents:
DELIMITER //
DROP PROCEDURE IF EXISTS createTc //
CREATE PROCEDURE createTc()
BEGIN
drop table if exists v3_tc;
CREATE TABLE v3_tc (
source BIGINT UNSIGNED NOT NULL ,
dest BIGINT UNSIGNED NOT NULL ,
PRIMARY KEY (source, dest) )
ENGINE = InnoDB;
insert into v3_tc (source, dest)
select distinct rel.sourceid, rel.destinationid
from rf2_ss_relationships rel inner join rf2_ss_concepts con
on rel.sourceid = con.id and con.active = 1
where rel.typeid = (select distinct conceptid from rf2_ss_descriptions where term = 'is a')
and rel.active = 1;
REPEAT
insert into v3_tc (source, dest)
select distinct b.source, a.dest
from v3_tc a
join v3_tc b on a.source = b.dest
left join v3_tc c on c.source = b.source and c.dest = a.dest
where c.source is null;
set @x = row_count();
select concat('Inserted ', @x);
UNTIL @x = 0 END REPEAT;
create index idx_v3_tc_source on v3_tc (source);
create index idx_v3_tc_dest on v3_tc (dest);
END //
DELIMITER;
This code works fine when I manually enter it into mysql 5.6.22 However if I save the file and from the prompt enter the command.
mysql -uroot -p -hlocalhost amtv3 < [full path]/amtv3_create_routines.sql
I have tried saving the file using utf8 encoding and windows 1252 encoding.
From the command prompt, there is no feedback, and the procedure is not created. In PHP I am using the codeigniter framework. If I use the db->query
method I can create the stored procedure, however the database loses connection. issuing $db->reconnect()
works, but not reliably.
Any suggestions on how to create the stored procedure?
Omitting the space in the last line, DELIMITER;
should result in a syntax error (there may be some other reason why this error is not being printed).
你可以试试这个
mysql -h localhost -U <username> -d <database_name> -f /home/user/<procedure_name>.sql
DELIMITER
is only a feature of certain MySQL clients, and not a feature of the server. Therefore, when executing a.sql file directly, the server will interpret the first semi-colon as the end of the first statement and DELIMITER //
will be seen as a syntax violation:
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 'DELIMITER // CREATE PROCEDURE. . .
Discovered this here: https://github.com/go-sql-driver/mysql/issues/351#issuecomment-120081608
The Solution? Simply don't change the delimiter. The BEGIN
and END
already delimit a compound statement.
Source: https://www.tutorialspoint.com/mysql/mysql_begin_end_compound.htm
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.