procedures in mySql - mysql

I'm actually trying to insert value into table using a procedure.This is my table code
create table employeeTable(
employeeId varchar(20),
employeeName varchar(20),
qualification varchar(20),
cadre varchar(15),
age numeric(3),
sex char(1),
occupation varchar(10),
dateOfBirth date,
address varchar(50),
city varchar(15),
employeePhoto blob,
constraint pk_employeeId_employeeTable primary key(employeeId)
);
My insert procedure is
delimiter //
create procedure insertEmployee(in employeeId varchar(20),in employeeName varchar(20),
in qualification varchar(20),in cadre varchar(15),in age numeric(3),in sex char(1),
in occupation varchar(10),in dateOfBirth date,in address varchar(50),in city varchar(15),
in employeePhoto blob)
begin
insert into employeetable values(upper(employeeId),employeeName,qualification,
cadre,age,sex,occupation,
str_to_date(dateOfBirth,'%d-%m-%Y'),address,city,employeePhoto);
end;//
delimiter ;
And I'm trying to call this procedure with following parameters
call insertEmployee('A001','Murugan','Phd','Manager',12,'M','service','30-07-1994',
'30,PNG nagar,pollachi','Coimbatore',null);
But values are not getting inserted and it's showing an error #1048 - Column 'dateOfBirth' cannot be null..
What is the problem here??

delimiter //
create procedure insertEmployee(in employeeId varchar(20),in employeeName varchar(20),
in qualification varchar(20),in cadre varchar(15),in age numeric(3),in sex char(1),
in occupation varchar(10),in dateOfBirth varchar(15),in address varchar(50),in city varchar(15),
in employeePhoto blob)
begin
insert into employeetable values(upper(employeeId),employeeName,qualification,
cadre,age,sex,occupation,
str_to_date(dateOfBirth,'%d-%m-%Y'),address,city,employeePhoto);
end;//
delimiter ;
Try this procedure. You were accpeting the dateOfBirth as a date object in the parameter list. It should have been a VARCHAR as that is how it is passed to the procedure.

Related

Stored procedure not working for inserting data

I have this table:
User
user_id int PK
username varchar(20)
secret_code varchar(20)
name varchar(20)
age int
gender varchar(20)
city varchar(20)
latest_signin_time timestamp
latest_signout_time timestamp
loc_list json
buddy_list json
I created a stored procedure:
create procedure insert_users(IN user_id int , in username varchar(20),in secret_code varchar(20),
in name varchar(20), in age int, in gender varchar(20), in city varchar(20),
in latest_signin_time timestamp, in latest_signout_time timestamp,
in loc_list json , in buddy_list json)
begin
insert into user values(user_id, username, secret_code, name, age, gender, city,
latest_signin_time, latest_signout_time, loc_list,buddy_list)
end ;
call insert_user(​'1', 'avs431','pwd1','Ameya','22','Male','Mumbai',null,null,'[]','[]'​​);
However, my code isn't running and I keep getting "Error Code: 1064. 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 'end' at line 9"
I'm not sure what is going wrong here. Can anyone help?
Thank you!
create procedure insert_users(IN user_id int , in username varchar(20),in secret_code varchar(20),
in name varchar(20), in age int, in gender varchar(20), in city varchar(20),
in latest_signin_time timestamp, in latest_signout_time timestamp,
in loc_list json , in buddy_list json)
/* begin */
insert into user values(user_id, username, secret_code, name, age, gender, city,
latest_signin_time, latest_signout_time, loc_list,buddy_list)
/* end */ ;
The commands in procedure terminate with semicolon (;). Use prefixes in the procedure parameters so the parameters won't be mixed up with column names. Also, list the column names in INSERT so your procedure will work even if a column is added to the table.
delimiter //
create procedure insert_users(
in_user_id int,
in_username varchar(20),
in_secret_code varchar(20),
in_name varchar(20),
in_age int,
in_gender varchar(20),
in_city varchar(20),
in_latest_signin_time timestamp,
in_latest_signout_time timestamp,
in_loc_list json,
in_buddy_list json
)
begin
insert into user (user_id, username, secret_code, name, age, gender,
city, latest_signtime, latest_signout_time, loc_list, buddy_list)
values(in_user_id, in_username, in_secret_code, in_name, in_age, in_gender,
in_city, in_latest_signin_time, in_latest_signout_time, in_loc_list, in_buddy_list);
end
//
delimiter ;
call insert_user(​'1', 'avs431','pwd1','Ameya','22','Male','Mumbai',null,null,'[]','[]'​​);

MySQL Procedure/trigger/constraint

I need help in creating a procedure with a constraint, when inserting staff member a manager to be specific he/she needs to be assigned to one clinic and one clinic only, thus when trying to insert a another manager to the same clinic it should create a trigger, that a manager has been assigned already to that clinic. Here's my code for inserting my variables :
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_staff`(in staff_num char(4),in First_name varchar(40),in surname varchar(30),in street varchar(20), in city varchar(20), in state varchar(2),in zipcode varchar(20),in Phone_no varchar(20),in Date_of_birth date,in gender_ char(1), in ID_no int(13),in Positon varchar(10),in salary_ double,in ClinicNo char(5))
BEGIN
set sql_safe_updates=0;
set foreign_key_checks=0;
insert into staff(staffNo,sFName,sLname,sStreet,sCity,sState,sZipCode,sTelNo,DOB,gender,ID,position,salary,clinicNo)
values(staff_num,First_name,surname,street,city,state,zipcode,Phone_no,Date_of_birth,gender_,ID_no,Positon,salary_,ClinicNo);
END
Following would check before inserting by selecting. If does not exist, record is inserted.
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_staff`(in staff_num char(4),in First_name varchar(40),in surname varchar(30),in street varchar(20), in city varchar(20), in state varchar(2),in zipcode varchar(20),in Phone_no varchar(20),in Date_of_birth date,in gender_ char(1), in ID_no int(13),in Positon varchar(10),in salary_ double,in ClinicNo char(5))
BEGIN
set sql_safe_updates=0;
set foreign_key_checks=0;
IF ( Position = 'Manager' AND EXISTS (select * from staff where clinicNo = ClinicNo and Positon='Manager')) THEN
//Handle your case
ELSE
insert into staff(staffNo,sFName,sLname,sStreet,sCity,sState,sZipCode,sTelNo,DOB,gender,ID,position,salary,clinicNo)values(staff_num,First_name,surname,street,city,state,zipcode,Phone_no,Date_of_birth,gender_,ID_no,Positon,salary_,ClinicNo);
END IF;
END

Incorrect number of arguments expected 8, got 0

CREATE PROCEDURE novi_korisnik (korisnik_id INT, new_ime varchar(45), new_prezime varchar(60), new_datum_rodj DATE, new_drzava_rodj varchar(45), new_email varchar(45), new_korisnicko_ime varchar(45), new_biografija TEXT)
BEGIN
insert into korisnik ( ime, prezime, datum_rodj, drzava_rodj, email, korisnicko_ime, biografija) values ( new_ime, new_prezime, new_datum_rodj, new_drzava_rodj, new_email, new_korisnicko_ime, new_biografija);
END
Please help me with these procedure
Result is INCORRECT NUMBER OF ARGUMENTS

mysql syntax error 1064 on create procedure

Hi I have created a table and now creating a stored procedure.
CREATE PROCEDURE p_order_insert (in in_agent_id Varchar(10),in in_rate_city varchar(15),in in_sendername varchar(50),in in_senderaddress1 varchar(50),in in_senderaddress2 varchar(50),in in_senderaddress3 varchar(50),in in_senderaddress4 varchar(50),in in_sendercity varchar(50),in in_senderstate varchar(50),in in_senderpostcode varchar(10),in in_sendercountry varchar(15),in in_receivername varchar(50),in in_receiveraddress1 varchar(50),in in_receiveraddress2 varchar(50),in in_receiveraddress3 varchar(50),in in_receiveraddress4 varchar(50),in in_receivercity varchar(50),in in_receiverstate varchar(50),in in_receiverpostcode varchar(10),in in_receivercountry varchar(15),in in_item1_description varchar(80),in in_item2_description varchar(80),in in_item3_description varchar(80),in in_item4_description varchar(80),in in_item5_description varchar(80),in in_item6_description varchar(80),in in_item1_quantity int,in in_item2_quantity int,in in_item3_quantity int,in in_item4_quantity int,in in_item5_quantity int,in in_item6_quantity int,in in_item1_weight double,in in_item2_weight double,in in_item3_weight double,in in_item4_weight double,in in_item5_weight double,in in_item6_weight double,in in_item1_value double,in in_item2_value double,in in_item3_value double,in in_item4_value double,in in_item5_value double,in in_item6_value double,in in_order_status Varchar(10),in in_total_price double,in in_agent_commission double,in in_payment_received Varchar(10), OUT out_awb_ref_id Varchar (10)) BEGIN
INSERT INTO hvp_order_master (agent_id, rate_city, sendername, senderaddress1, senderaddress2, senderaddress3, senderaddress4, sendercity, senderstate, senderpostcode, sendercountry, receivername, receiveraddress1, receiveraddress2, receiveraddress3, receiveraddress4, receivercity, receiverstate, receiverpostcode, receivercountry, item1_description, item2_description, item3_description, item4_description, item5_description, item6_description, item1_quantity, item2_quantity, item3_quantity, item4_quantity, item5_quantity, item6_quantity, item1_weight, item2_weight, item3_weight, item4_weight, item5_weight, item6_weight, item1_value, item2_value, item3_value, item4_value, item5_value, item6_value, order_status, total_price, agent_commission, payment_received)
VALUES
(in_agent_id, in_rate_city, in_sendername, in_senderaddress1, in_senderaddress2, in_senderaddress3, in_senderaddress4, in_sendercity, in_senderstate, in_senderpostcode, in_sendercountry, in_receivername, in_receiveraddress1, in_receiveraddress2, in_receiveraddress3, in_receiveraddress4, in_receivercity, in_receiverstate, in_receiverpostcode, in_receivercountry, in_item1_description, in_item2_description, in_item3_description, in_item4_description, in_item5_description, in_item6_description, in_item1_quantity, in_item2_quantity, in_item3_quantity, in_item4_quantity, in_item5_quantity, in_item6_quantity, in_item1_weight, in_item2_weight, in_item3_weight, in_item4_weight, in_item5_weight, in_item6_weight, in_item1_value, in_item2_value, in_item3_value, in_item4_value, in_item5_value, in_item6_value, in_order_status, in_total_price, in_agent_commission, in_payment_received); END;;
However, I am getting
1064 - Erreur de syntaxe près de '' à la ligne 6 there is no such issue which I can see.
Could someone help?
When you included the DELIMITER wrapper it saves, like below.
In addition I am sure you have a poor schema design.
DROP PROCEDURE IF EXISTS p_order_insert;
DELIMITER $
CREATE PROCEDURE p_order_insert
(in in_agent_id Varchar(10),
in in_rate_city varchar(15),
in in_sendername varchar(50),
in in_senderaddress1 varchar(50),
in in_senderaddress2 varchar(50),
in in_senderaddress3 varchar(50),
in in_senderaddress4 varchar(50),
in in_sendercity varchar(50),
in in_senderstate varchar(50),
in in_senderpostcode varchar(10),
in in_sendercountry varchar(15),
in in_receivername varchar(50),
in in_receiveraddress1 varchar(50),
in in_receiveraddress2 varchar(50),
in in_receiveraddress3 varchar(50),
in in_receiveraddress4 varchar(50),
in in_receivercity varchar(50),
in in_receiverstate varchar(50),
in in_receiverpostcode varchar(10),
in in_receivercountry varchar(15),
in in_item1_description varchar(80),
in in_item2_description varchar(80),
in in_item3_description varchar(80),
in in_item4_description varchar(80),
in in_item5_description varchar(80),
in in_item6_description varchar(80),
in in_item1_quantity int,
in in_item2_quantity int,
in in_item3_quantity int,
in in_item4_quantity int,
in in_item5_quantity int,
in in_item6_quantity int,
in in_item1_weight double,
in in_item2_weight double,
in in_item3_weight double,
in in_item4_weight double,
in in_item5_weight double,
in in_item6_weight double,
in in_item1_value double,
in in_item2_value double,
in in_item3_value double,
in in_item4_value double,
in in_item5_value double,
in in_item6_value double,
in in_order_status Varchar(10),
in in_total_price double,
in in_agent_commission double,
in in_payment_received Varchar(10),
OUT out_awb_ref_id Varchar (10))
BEGIN
INSERT INTO hvp_order_master (agent_id, rate_city, sendername, senderaddress1, senderaddress2,
senderaddress3, senderaddress4, sendercity, senderstate, senderpostcode, sendercountry,
receivername, receiveraddress1, receiveraddress2, receiveraddress3, receiveraddress4,
receivercity, receiverstate, receiverpostcode, receivercountry, item1_description,
item2_description, item3_description, item4_description, item5_description,
item6_description, item1_quantity, item2_quantity, item3_quantity, item4_quantity,
item5_quantity, item6_quantity, item1_weight, item2_weight, item3_weight, item4_weight,
item5_weight, item6_weight, item1_value, item2_value, item3_value, item4_value, item5_value,
item6_value, order_status, total_price, agent_commission, payment_received)
SELECT
in_agent_id, in_rate_city, in_sendername, in_senderaddress1, in_senderaddress2,
in_senderaddress3, in_senderaddress4, in_sendercity, in_senderstate, in_senderpostcode,
in_sendercountry, in_receivername, in_receiveraddress1, in_receiveraddress2, in_receiveraddress3,
in_receiveraddress4, in_receivercity, in_receiverstate, in_receiverpostcode, in_receivercountry,
in_item1_description, in_item2_description, in_item3_description, in_item4_description,
in_item5_description, in_item6_description, in_item1_quantity, in_item2_quantity, in_item3_quantity,
in_item4_quantity, in_item5_quantity, in_item6_quantity, in_item1_weight, in_item2_weight,
in_item3_weight, in_item4_weight, in_item5_weight, in_item6_weight, in_item1_value, in_item2_value,
in_item3_value, in_item4_value, in_item5_value, in_item6_value, in_order_status, in_total_price,
in_agent_commission, in_payment_received;
END$$
DELIMITER ;
The need for a DELIMITER is dependent on which client-side tool you use. For PHPMyAdmin, it is not needed typically if ever.
With MySQL Workbench it is.
Add DELIMITER ## to the start. Also, at the end of your code you have the ; twice. END;; 1 is enough.
DELIMITER ##
CREATE PROCEDURE...
END##
DELIMITER;

syntax error inserting values using store procedure

create procedure sp_FldBkgData
(
IN awb int,
IN origin varchar(2),
IN dist varchar(2),
IN courier int,
IN pdate varchar(10),
IN acc int,
IN service varchar(1),
IN agent1 varchar(5),
IN agent2 varchar(5),
IN bkgtime int,
IN sname varchar(45),
IN sadd1 varchar(45),
IN sadd2 varchar(45),
IN sadd3 varchar(45),
IN scity varchar(45),
IN sstate varchar(25),
IN spostal int,
IN scountry varchar(8),
IN sph varchar(11),
IN sfax varchar(25),
IN smail varchar(45),
IN sref varchar(45),
IN carrier varchar(45),
IN ccompany varchar(45),
IN cname varchar(45),
IN cadd1 varchar(45),
IN cadd2 varchar(45),
IN cadd3 varchar(45),
IN ccity varchar(25),
IN cstate varchar(15),
IN ccountry varchar(45),
IN cgateway varchar(3),
IN cph varchar(25),
IN goods varchar(45),
IN paymode varchar(7),
IN curr varchar(3),
IN val int,
IN pcs int,
IN weight double,
IN handling varchar(1),
IN insurrance varchar(3),
IN fac int,
IN gst int
)
BEGIN
INSERT INTO cms_module_fieldbooking (Awb) VALUES (awb);
END
Here is the error:
To create a procedure in the MySQL Workbench Query Editor, you need to specify a delimiter other than the semicolon.
For example:
DELIMITER $$
CREATE PROCEDURE foo()
BEGIN
SELECT 'foo';
END$$
DELIMITER ;
As an alternative, MySQL Workbench also has a "routine" editor that automatically handles the delimiter for you. (In the schema list on the left, right click on Procedures, and select "Create Procedure". When you click Apply, you'll see the actual script, that includes the DELIMITER statements.