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');
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
Delimiter //
create Procedure Update_Org_role(IN id int(11),IN role varchar(25))
begin
update Organization org set org.Role=role where org.Org_ID=id;
commit;
end;//
Delimiter ;
call Update_Org_role(123,'Company');
I suspect that argument named role is clashing with the column that has the same name in the table that you want to update. You can give different names to your parameters:
Delimiter //
create Procedure Update_Org_role(IN p_org_id int(11),IN p_role varchar(25))
begin
update Organization set role = p_role where Org_ID = p_org_id;
commit;
end;//
Delimiter ;
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');
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.