I am trying to create a procedure in a MySQL database, but I want to check if it exists first.
I know how to do it for a table but when I use the same syntax for a stored procedure it doesn't compile.
Does anybody know?
Just drop the procedure if it does exist and then re-add it:
DROP PROCEDURE IF EXISTS my_procedure;
CREATE PROCEDURE my_procedure()
SELECT EXISTS(SELECT 1 FROM mysql.proc p WHERE db = 'db_name' AND name = 'stored_proc_name');
So you could do:
IF NOT EXISTS(SELECT 1 FROM mysql.proc p WHERE db = 'db_name' AND name = 'stored_proc_name') THEN
....
END IF;
It doesn't exist. We had to write a stored procedure that mimics the same functionality. Basically we create stored procedures by calling a stored procedure that does the "if exists" check.
mySQL 8 allows to check for existence within the create statement using IF NOT EXISTS.
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
CREATE
[DEFINER = user]
PROCEDURE [IF NOT EXISTS] sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE
[DEFINER = user]
FUNCTION [IF NOT EXISTS] sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
Just call the procedure
CALL my_procedure();
if you get the specific error
PROCEDURE yourDB.my_procedure does not exist
you can now react to "Error Code : 1305" and create the missing procedure:
CREATE PROCEDURE my_procedure()
BEGIN
...
END
Related
I have stored procedure as below:
DROP PROCEDURE IF EXISTS TEST;
CREATE PROCEDURE TEST()
BEGIN
IF (SELECT count(*) FROM (SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE 'User') A) > 0 THEN
ALTER TABLE User DROP FOREIGN KEY FK_forkey
END IF;
END
SELECT ....
I have the syntax error at END IF. I have been trying many ways to fix this but no success so far. If I add Delimiter // after the DROP PROCEDURE and after the END, I get error at the end Delimiter (delimiter is not valid input at this position). What did I miss/How did I do it wrong? Thank you in advance for your response.
UPDATED
DROP PROCEDURE IF EXISTS TEST;
DELIMITER //
CREATE PROCEDURE TEST()
BEGIN
IF EXISTS(SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE 'User')
THEN
ALTER TABLE User DROP FOREIGN KEY FK_AccountID;
END IF;
END //
DELIMITER;
The updated above is my new change and it reports error at the end Delimiter: Delimiter is not valid input at this position
This answer is from #Solarflare, since he/she didn't post the answer, I'll do it instead so I could mark this question as answered and it may come in handy for others newbies that might run into the same problem as me.
You have to leave a space between delimiter and ;
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_or_update`$$
CREATE PROCEDURE insert_or_update(
IN username VARCHAR(70),
IN score INT,
IN titlein VARCHAR(70)
)
begin
IF EXISTS (SELECT * FROM two_player WHERE title=titlein and user1!=username and user2='') THEN
UPDATE two_player SET score12=score , user2=username WHERE title=titlein and user1!=username and user2='' limit 1;
ELSE
INSERT INTO two_player (user1,score11,title) values (username, score, titlein);
END if;
END$$
DELIMITER ;
call insert_or_update('sara',20,'math');
I create a procedure. But when I try to call it I get this error message:
#1305 - PROCEDURE u941310304_menu.insert_or_update does not exist
What's wrong?
I tested you code and the call to procedure works.
Your default database is u941310304_menu, it seems you are creating the procedure in another db. You can create the procedure specifying the destination database.
DELIMITER $$
DROP PROCEDURE IF EXISTS `u941310304_menu`.`insert_or_update`$$
CREATE PROCEDURE `u941310304_menu`.insert_or_update(
[...]
If the procedure is in another database you must specify the db name as prefix:
call `another_database`.insert_or_update('sara',20,'math');
I have created a Stored Procedure
CREATE PROCEDURE GetAllRecords()
BEGIN
SELECT * FROM my_table;
END //
Now I want to add a parameter to this Stored Procedure like this :
CREATE PROCEDURE GetAllRecords(id1 INT(4))
BEGIN
SELECT * FROM my_table WHERE `id` = id1;
END //
How can I edit my Stored Procedure?
Delete the procedure
drop procecdure GetAllRecords//
And recreate it
CREATE PROCEDURE GetAllRecords(id1 INT(4)) ...
You have drop procedure first and then re-create it
DROP PROCEDURE IF EXISTS GetAllRecords;
CREATE PROCEDURE GetAllRecords(IN _id INT)
SELECT *
FROM my_table WHEREid = _id;
And since you're using the only statement in your procedure you can ditch BEGIN ... END block and use a usual delimiter.
In case you're wondering no you can not use ALTER PROCEDURE in this case
This statement can be used to change the characteristics of a stored
procedure. However, you cannot change the parameters or body of a
stored procedure using this statement; to make such changes, you
must drop and re-create the procedure using DROP PROCEDURE and CREATE PROCEDURE.
First of all ,This is not how you can pass the parameter in stored procedure.
You can not alter stored procedure, the way to do this is drop existing and create new one.
DELIMITER $$
DROP PROCEDURE IF EXISTS GetAllRecords$$
CREATE PROCEDURE GetAllRecords(IN param1 INT, OUT pram2 Varchar(100), INOUT param3 Date)
BEGIN
select * from <tbl>;
< Your queries>
END$$
DELIMITER ;
For Full detail of stored procedure
please follow the link
http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/
http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.pdf
To Alter a stored Procedure check this link
http://technet.microsoft.com/en-us/library/ms345356.aspx
The best way is to drop the stored proceure and create it again
DROP PROCEDURE IF EXISTS GetAllRecords
CREATE PROCEDURE GetAllRecords(IN _id INT)
AS
BEGIN
SELECT *
FROM my_table WHEREid = _id;
END
How to Alter a stored procedure in Mysql.
DROP PROCEDURE IF EXISTS sp_Country_UPDATE;
CREATE PROCEDURE sp_Country_UPDATE
( IN p_CountryId int,
IN p_CountryName nvarchar(25),
IN p_CountryDescription nvarchar(25),
IN p_IsActive bit,
IN p_IsDeleted bit )
UPDATE
Country
SET
CountryName = p_CountryName ,
CountryDescription=p_CountryDescription,
IsActive= p_IsActive,
IsDeleted=p_IsDeleted
WHERE
CountryId = p_CountryId ;
How to alter this Stored Procedure?
If you mean you want to edit the Procedure, then you can't according to the MySQL docs:
This statement can be used to change the characteristics of a stored procedure. More than one change may be specified in an ALTER PROCEDURE statement. However, you cannot change the parameters or body of a stored procedure using this statement; to make such changes, you must drop and re-create the procedure using DROP PROCEDURE and CREATE PROCEDURE.
The Alter syntax lets you change the "characteristics" but not the actual procedure itself
http://dev.mysql.com/doc/refman/5.0/en/alter-procedure.html
Here's an example of creating, Altering (the comment) then dropping and recreating:
DROP PROCEDURE myFunc;
DELIMITER //
CREATE PROCEDURE myFunc ()
COMMENT 'test'
BEGIN
SELECT 5;
END //
DELIMITER ;
ALTER PROCEDURE myFunc
COMMENT 'new comment';
CALL myFunc();
DROP PROCEDURE myFunc;
DELIMITER //
CREATE PROCEDURE myFunc ()
COMMENT 'last time'
BEGIN
SELECT 6;
END //
DELIMITER ;
CALL myFunc();
The above CALL myFunc() statments would return 5 and then 6.
Viewing the stored procedure would show a comment of "test", "new comment" or "last time" depending on when you viewed the Procedure body (I'm not sure how to view the comments via the CLI but I can see them in the functions tab in Navicat)
ALTER PROCEDURE proc_name [characteristic ...]
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
This is how you Create
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
This is how you Alter
Alter PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
I am trying to make a stored procedure using mySQL. This procedure will validate a username and a password. I'm currently running mySQL 5.0.32 so it should be possible to create procedures.
Heres the code I've used. All I get is an SQL syntax error.
GO
CREATE PROCEDURE checkUser
(IN #brugernavn varchar(64)),IN #password varchar(64))
BEGIN
SELECT COUNT(*) FROM bruger WHERE bruger.brugernavn=#brugernavn AND bruger.pass=#Password;
END;
Thank you in advance
I figured it out now. Here's the correct answer
CREATE PROCEDURE checkUser
(
brugernavn1 varchar(64),
password varchar(64)
)
BEGIN
SELECT COUNT(*) FROM bruger
WHERE bruger.brugernavn=brugernavn1
AND bruger.pass=password;
END;
# points to a global var in mysql. The above syntax is correct.
(IN #brugernavn varchar(64)**)**,IN #password varchar(64))
The problem is the )
Its very easy to create procedure in Mysql. Here, in my example I am going to create a procedure which is responsible to fetch all data from student table according to supplied name.
DELIMITER //
CREATE PROCEDURE getStudentInfo(IN s_name VARCHAR(64))
BEGIN
SELECT * FROM student_database.student s where s.sname = s_name;
END//
DELIMITER;
In the above example ,database and table names are student_database and student respectively.
Note: Instead of s_name, you can also pass #s_name as global variable.
How to call procedure?
Well! its very easy, simply you can call procedure by hitting this command
$mysql> CAll getStudentInfo('pass_required_name');