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
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
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//
How Do create insert query using procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_emp `()
INSERT INTO employee(id,name,salary,address) VALUES (id,name,salary,address)
$name= $_POST['name'];
$salary= $_POST['salary'];
$address= $_POST['address'];
$sql=mysql_query("CALL insert_emp ($name,$salary,$address)");
Make sure the field "id" is set to auto-increment inorder to have an easy inserting into table.
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `insert_emp`(IN NAME VARCHAR(15),IN salary INT,IN address VARCHAR(15))
BEGIN
INSERT INTO `employee`(employee.`id`,employee.`name`,employee.`salary`,employee.`address`) VALUES (id,NAME,salary,address);
END$$
DELIMITER ;
Example query,
CALL insert_emp('anish',18000,'mlre');
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
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.