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
Related
I created two procedures for insert and delete respectively and would like to add them into just one procedure separated according to user's output. Any suggestions on how to do that?
create table departamento (
dnome varchar(100),
dnumero int primary key,
cpf_gerente bigint,
data_inicio_gerente date
);
Insert procedure:
delimiter //
create procedure inserir_departamento(in pr_dnome varchar(100), in pr_dnumero int, in pr_cpf_gerente bigint, in pr_data_inicio_gerente date)
begin
insert into departamento values(pr_dnome, pr_dnumero, pr_cpf_gerente,pr_data_inicio_gerente);
end //
delimiter ;
#drop procedure inserir_departamento;
call inserir_departamento('Teste', 6, 99988777767,'1978-11-11');
Delete procedure:
delimiter $$
create procedure deletar_departamento(in pr_dnumero int)
begin
delete from departamento
where dnumero = pr_dnumero;
end $$
delimiter ;
#drop procedure deletar_departamento;
call deletar_departamento(6);
I tried doing this to "mix" both procedures, but without success (I just receive a syntax error message):
delimiter $$
create procedure modificar_departamento(in insert enum("i"), in delete enum("d"), in pr_dnome varchar(100), in pr_dnumero int, in pr_cpf_gerente bigint, in pr_data_inicio_gerente date)
begin
if insert == "i" then
insert into departamento values(pr_dnome, pr_dnumero, pr_cpf_gerente,pr_data_inicio_gerente);
else if delete == "d" then
delete from departamento where dnumero = pr_dnumero;
end if;
end $$
delimiter ;
Any idea on how to do that?
= not ==. elseif not else if. Don't use reserved words as variable or parameter names, or if you do put them in backticks.
fiddle
That's my table:
create table if not exists Ditte(nome varchar(30), luogo varchar(30), ZIP int);
That's my procedure:
delimiter //
create procedure deleteDitta(zip int)
begin
DECLARE newZIP int;
SET newZIP = zip;
DELETE from Ditte where ZIP = newZIP;
end;
//
delimiter ;
This is what I added in my table:
insert ignore into Ditte values ("Ditta1", "city1", 6828);
insert ignore into Ditte values ("Ditta2", "city2", 12345);
When I call my procedure deleteDitta and I put "12345" as parameter (like this: call deleteDitta(12345);), the procedure should deletes only the second row in table "Ditte", but it deletes all the contents of the table.
How can I fix it?
This seems to be MySQL getting confused about column names and variable names. Changing your procedure to this fixes the problem:
create procedure deleteDitta(dzip int)
begin
DELETE from Ditte where ZIP = dzip;
end;
Demo on dbfiddle
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
if i use one table in two different Stored procedure name, (one for insert , one for update command) it showing syntax error.
first i created studentrc SP:
delimiter //
create procedure studentrc(in student_name varchar(20),in Reg_no int(6),in mark1 int(3), in mark2 int(3),in total int(10))
begin
insert into studentrecords values(student_name,Reg_no,mark1,mark2,total);
end; //
no errors
next i create studentrcs SP:
delimiter //
create procedure studentrcs(inout Reg_no int(6))
begin
UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=?;
end;//
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 'UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=' at line 3
how can rectify this error...
I'll be the first to admit I'm not a SQL guru by any means, but shouldn't you be taking in more variables and using them in place of the question marks?
You need to put the news values in the args list
create procedure studentrcs(sname varchar(100),m1 varchar(100),m2 varchar(100),total int,inout Reg_noarg int(6))
begin
UPDATE studentrecords
set student_name=sname ,mark1=m1,mark2=m2,total=totalarg
where Reg_no=Reg_noarg ;
for "INSERT"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10),IN field3 INT(10), IN field4 VARCHAR(10))
BEGIN
INSERT INTO mytablename (FIELD_1,FIELD_2,FIELD_3,FIELD_4) VALUES
(field1,field2,field3,field4);
END $$
DELIMITER ;
size which i have given in IN parameter should be equal to field size in table
for "UPDATE"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10) )
BEGIN
UPDATE mytablename SET FIELD_1=filed1 WHERE FIELD_2=field2;
END $$
size which i have given in IN parameter should be equal to field size in table
hope this may help you.