简体   繁体   中英

how to create a stored procedure in mysql the right way

PHP mysqli quickguide for stored procedures, http://php.net/manual/en/mysqli.quickstart.stored-procedures.php :

if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
    !$mysqli->query("CREATE PROCEDURE p(IN id_val INT) BEGIN INSERT INTO test(id) VALUES(id_val); END;")) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

does this not delete the procedure then replace it? is this the correct way to do it? what is the point in deleting it? does deleting it not negate the whole point of having a procedure which you can call for the duration of a MySQL connection?

You have to drop the 'old' procedure first, otherwise the CREATE will fail with an "already exists" errors. It's the same for pretty much EVERY object in a database, eg

CREATE TABLE foo ...   // creates the table
CREATE TABLE foo ...   // will **NOT** replace the one just created

you cannot 'overwrite' a table/proc/db just by redefining it. you have to drop the original one first.

Consider the chaos that'd occur if some poor DBA at a major bank accidentally ran

CREATE DATABASE clients;

and trashed their entire client db because the DB engine replaced the original DB with this new empty one.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM