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
Related
I m passing parameters in stored procedures but unable to fetch records when filtering with inputs.But when I store that Input into variables and pass it to fetch record I get the records.why?
Here is stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = ip_Id;
END$$
DELIMITER;
I tried to take input in variable and pass it to where clause ti fetch records .
Here is the method that I have tried. And it works why?
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
Select ip_Id into #ip_Id;
SELECT * FROM table_namel IAS WHERE Id = #ip_Id;
END$$
DELIMITER;
We didn't know the structure of table_namel, but it seems the table have a field with same name as input parameter.
To prevent that use the parameters with some prefix/surfix:
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN p_ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = p_ip_Id;
END$$
DELIMITER;
I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.
I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//
Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//
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 am trying to make a query to create several procedures at the same time; however when I try to drop them at the beginning is throws a 1064 error and only considered the first procedure query.
It creates the procedures perfectly when I try to run it separately but it stops when I do it together it stops in after the first drop
DROP PROCEDURE IF EXISTS add;
DROP PROCEDURE IF EXISTS remove;
DROP PROCEDURE IF EXISTS edit;
#PROCEDURE TO ADD
DELIMITER //
CREATE PROCEDURE add
(p_Name VARCHAR(30), p_Quantity DECIMAL(6,2), p_QuantityType VARCHAR(5), p_Notes VARCHAR(50))
BEGIN
INSERT INTO table
(name, quantity, quantity_type, notes)
VALUES
(p_Name, p_Quantity, p_QuantityType, p_Notes);
COMMIT;
END //
DELIMITER;
#PROCEDURE TO REMOVE BASED ON THE INVENTORY ITEM ID
DELIMITER //
CREATE PROCEDURE remove
( p_Id SMALLINT(4))
BEGIN
DELETE FROM table
WHERE id=p_Id;
COMMIT;
END//
DELIMITER ;
#PROCEDURE TO EDIT BASED ON THE INVENTORY ITEM ID
DELIMITER //
CREATE PROCEDURE edit
(p_Name VARCHAR(30), p_Id SMALLINT(4))
BEGIN
UPDATE table
SET name=p_Name
WHERE id=p_Id;
COMMIT;
END //
DELIMITER ;
The recommendation is to avoid using reserved words (add, table) or give special treatment. See 9.3. Reserved Words.
SQL Fiddle demo
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 //