How to Alter a stored procedure in mysql - mysql

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 //

Related

#1305 - PROCEDURE does not exist in mysql

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');

Stored procedure MSSQL to MYSQL

I need convert this stored procedure mssql to mysql, somebody help me please
1)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_IS]
as
insert into request (dateIS) values (GETDATE())
2)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_RN]
as
select COUNT(id) "quantity" from notify
THANKS!
Look at this
CREATE
[DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE
[DEFINER = { user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
func_parameter:
param_name type
type:
Any valid MySQL data type
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
routine_body:
Valid SQL routine statement
Modify a Stored Procedure
MySQL provides an ALTER PROCEDURE statement to modify a routine, but only allows for the ability to change certain characteristics. If you need to alter the body or the parameters, you must drop and recreate the procedure.
DROP PROCEDURE IF EXISTS sp_IS;
delimiter //
create PROCEDURE sp_IS
BEGIN
insert into request (dateIS) values (GETDATE())
END
delimiter;
DROP PROCEDURE IF EXISTS sp_RN;
delimiter //
create PROCEDURE sp_RN
BEGIN
select COUNT(id) AS quantity from notify
END
delimiter;

Edit a Stored Procedure using ALTER?

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 cast in MySQL stored procedures

Trying to create the following routine in MySQL Workbench yields a "This object's DDL statement contains syntax errors. Are you sure you want to apply the DDL statement unchanged?":
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE PROCEDURE `dbName`.`testFunc` ()
BEGIN
SET i = CAST(100 AS DOUBLE);
END
Any ideas?
This does the same thing:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE PROCEDURE `rateGenius`.`testFunc` ()
BEGIN
SET i = CONVERT(100, DOUBLE);
END
You need to declare i in your stored procedure.
DELIMITER $$
CREATE PROCEDURE `rateGenius`.`testFunc` ()
BEGIN
DECLARE i DOUBLE
SET i = 100.0;
END
Not sure what you're actually trying to do with this stored proc though. Your CAST/CONVERT is unecessary in this case but your syntax for those functions is in fact correct.

Stored Procedure with ALTER TABLE

I have a need to sync auto_increment fields between two tables in different databases on the same MySQL server. The hope was to create a stored procedure where the permissions of the admin would let the web user run ALTER TABLE [db1].[table] AUTO_INCREMENT = [num]; without giving it permissions (That just smells of SQL injection).
My problem is I'm receiving errors when creating the store procedure. Is this something that is not allowed by MySQL?
DROP PROCEDURE IF EXISTS sync_auto_increment;
CREATE PROCEDURE set_auto_increment (tableName VARCHAR(64), inc INT)
BEGIN
ALTER TABLE tableName AUTO_INCREMENT = inc;
END;
To extend on the discussion on the comments on Chibu's answer... yes, you can use prepared statements. But you got to use CONCAT to create the sentence instead of using PREPARE ... FROM ....
Here is a working solution:
DROP PROCEDURE IF EXISTS set_auto_increment;
DELIMITER //
CREATE PROCEDURE set_auto_increment (_table VARCHAR(64), _inc INT)
BEGIN
DECLARE _stmt VARCHAR(1024);
SET #SQL := CONCAT('ALTER TABLE ', _table, ' AUTO_INCREMENT = ', _inc);
PREPARE _stmt FROM #SQL;
EXECUTE _stmt;
DEALLOCATE PREPARE _stmt;
END//
DELIMITER;
I've learned this form the article Prepared Statement Failure by Michael McLaughlin.
The problem seems to be that you need to change the delimiter. It thinks that the Alter table line is the end of the function. Try this:
DROP PROCEDURE IF EXISTS sync_auto_increment;
DELIMITER //
CREATE PROCEDURE set_auto_increment (tableName VARCHAR(64), inc INT)
BEGIN
ALTER TABLE tableName AUTO_INCREMENT = inc;
END//
DELIMITER ;
Sometimes mysql is still picky about letting you use stored procedures, so you can do try this if you still can't run it:
DROP PROCEDURE IF EXISTS sync_auto_increment;
DELIMITER //
CREATE PROCEDURE set_auto_increment (tableName VARCHAR(64), inc INT)
DETERMINISTIC
READS SQL DATA
BEGIN
ALTER TABLE tableName AUTO_INCREMENT = inc;
END//
DELIMITER ;
I think you'll find you can't put Data Definition Language statements into a stored procedure. A possible exception would be CREATE TEMPORARY TABLE. I have searched the MySQL manual for more information on that point; it says the stored procedure may contain a statement_list, but nowhere defines what that means. But I think that's the case.