mysql insert stored procedure throwing an error - mysql

I am trying to write a stored procedure in mysql using Heidisql. I keep getting the error. Below is the screen shot:
below is the script of the stored procedure.
CREATE PROCEDURE index_insertPersonadata
(
IN p_firstName VARCHAR(50),
IN p_middleName VARCHAR(50),
IN p_lastName VARCHAR(50),
IN p_address VARCHAR(200),
IN p_City VARCHAR(50),
IN p_State VARCHAR(50),
IN p_ZIP VARCHAR(50)
)
BEGIN
INSERT INTO personalrecord(
FirstName,
MiddleName,
LastName,
Address,
City,
state,
ZIP
)
VALUES
(
p_firstName,
p_middleName,
p_lastName,
p_address,
p_City,
p_State,
p_ZIP
);
END;
any help will be highly appreciated.

As far as your question is concerned, you are just missing a delimiter command:
DELIMITER //
CREATE PROCEDURE index_insertPersonadata (
IN p_firstName VARCHAR(50),
IN p_middleName VARCHAR(50),
IN p_lastName VARCHAR(50),
IN p_address VARCHAR(200),
IN p_City VARCHAR(50),
IN p_State VARCHAR(50),
IN p_ZIP VARCHAR(50)
)
BEGIN
INSERT INTO personalrecord(FirstName, MiddleName, LastName, Address, City, state, ZIP)
VALUES(p_firstName, p_middleName, p_lastName, p_address, p_City, p_State, p_ZIP);
END;
//
DELIMITER ;
Why you need this is explained in the documentation:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command.

Related

I am trying to create a PROCEDURE that INSERTS values via paramerters but it's not working

DELIMITER &&
DROP PROCEDURE IF EXISTS add_row_in_emp_table;
CREATE PROCEDURE add_row_in_emp_table
(
empcode VARCHAR(15),
empname VARCHAR(15),
deptcode VARCHAR(15),
birthdate DATE,
sex CHAR(1),
desigcode varchar(15),
supcode varchar(15),
gradecode varchar(15),
gradelevel varchar(15),
basicpay varchar(15)
)
BEGIN
INSERT INTO emp
(empcode)
values(
empcode,empname,deptcode,birthdate,sex,desigcode,
supcode,gradecode,gradelevel,basicpay
);
END &&
DELIMITER ;
it is showing commands out of sync. can you please explain what is wrong?
i also tried using session variables that also is not working
Use columns name if few columns value are inserted. But if all columns value are inserted then columns name using are not mandatory but may be a chance for data type mismatch. Also passing session value with parameter or handle with variable inside procedure.
CREATE PROCEDURE add_row_in_emp_table (IN ecode varchar(15),IN ename varchar(15))
BEGIN
insert into emp (empcode, empname) values (ecode,ename);
END
N.B.: use columns name for inserting data or selecting data for better readability
Please check this url https://dbfiddle.uk/ODxp-NFH

MySQL stored procedure syntax troubles

I'm working on this procedure but I'm not too familiar with writing a stored procedure in MySQL, when I try to create the stored procedure as shown here, it keeps telling me that a syntax is incorrect at line 9
DELIMITER//
CREATE PROCEDURE InsertUser (
IN new_USER_ID BIGINT,
IN new_USER_NAME VARCHAR(36),
IN new_ENCRYTED_PASSWORD VARCHAR(128),
IN new_ENABLED BIT,
IN select_Role_ID BIGINT)
BEGIN
DECLARE user_role_id BIGINT
SET user_role_id = (SELECT max(id) from USER_ROLE);
Insert into App_User(USER_ID, USER_NAME, ENCRYTED_PASSWORD, ENABLED)
values(new_USER_ID, new_USER_NAME, new_ENCRYTED_PASSWORD, new_ENABLED);
INSERT INTO USER_ROLE(ID, USER_ID, ROLE_ID)
values(user_role_id+1, new_USER_ID, select_Role_ID);
END;//
DELIMITER;
Can someone show me how the correct syntax should be?
Here's the correct way of using delimiter. See mysql documentation
DELIMITER //
CREATE PROCEDURE InsertUser (
IN new_USER_ID BIGINT,
IN new_USER_NAME VARCHAR(36),
IN new_ENCRYTED_PASSWORD VARCHAR(128),
IN new_ENABLED BIT,
IN select_Role_ID BIGINT)
BEGIN
Insert into App_User(USER_ID, USER_NAME, ENCRYTED_PASSWORD, ENABLED)
values(new_USER_ID, new_USER_NAME, new_ENCRYTED_PASSWORD, new_ENABLED);
INSERT INTO USER_ROLE(ID, USER_ID, ROLE_ID)
values(user_role_id+1, new_USER_ID, select_Role_ID);
END //
DELIMITER ;

Unknown column in the field list, parametrized stored procedure

I've seen a lot of similar problems but none of them solved my problem. This is the code i used to create the stored procedure.
DELIMITER //
CREATE PROCEDURE sp_regUser(
IN user VARCHAR(100),
IN name VARCHAR(50),
IN surname VARCHAR(50),
IN email VARCHAR(250),
IN password VARCHAR(250),
IN telephone VARCHAR(15),
IN street VARCHAR(120),
IN postalcode VARCHAR(6),
IN id INT,
IN birthdate DATE,
IN message VARCHAR(320),
IN photo LONGBLOB,
IN photoName VARCHAR(50),
IN role VARCHAR(10)
)
BEGIN
INSERT INTO klientet(Perdoruesi,K_Emri,K_Mbiemri,K_Email,Fjalekalimi,K_T_Kontaktues,K_Rruga,K_KodiPostar,ID_Qyteti,K_Datelindja,Mesazh_shtese,Foto,Foto_Emri,Roli)
VALUES(user, name, surname, email, password, telephone, street, postalcode, id, birthdate, message, photo, photoName, role);
END//
DELIMITER ;
And this is the way i call it.
CALL sp_regUser('arlind','Arlind','Hajdari','arlind.hajdari#smth.com','perdorimi','0037745231807','Muhaxhiret 13','60000',8,'2016-01-01','miredita',LOAD_FILE('E:/Koala.jpg'),'image.jpg','user')
Doing this way, i get an error that says: Error Code: 1054. Unknown column 'K_Emri' in 'field list'. The columns specified in the insert syntax are correctly corresponding to ones in database, i have no idea whats the problem. Thanks in advance.
The problem was with a trigger that had to be triggered after the insertion to the klientet. Thank you for answers.

using stored procedure to insert values in a table

I'm new to MYSQL and I'm trying to use a stored procedure to insert values into a table.can someone help me with this please
create procedure insertcust(in lname varchar(30), in fname varchar(30),dob1 date)
begin
insert into customer (lname,fname,dob)
values(lname,fname,dob1);//it is saying i have an incorrect syntax ere
end
If you run your procedure directly in phpmyadmin then you should write something like below
CREATE PROCEDURE `insertcust`(IN `lname ` VARCHAR(30), IN `fname` VARCHAR(30), IN `dob1` DATE)
NOT DETERMINISTIC NO SQL SQL SECURITY
DEFINER
insert into customer (lname,fname,dob)values(lname,fname,dob1)
I hope this will help you.

Execute procedure in PHPMyAdmin

I have the following SQL code:
EXECUTE userAanmaken
#domeinNummer=1,
#gebruikerNaam='Jansen',
#gebruikerPass='Jaap',
#gebruikerEmail='jan#piet.nl',
#gebruikerVN='Joop',
#gebruikerTV='van',
#gebruikerAN='Heha',
#gebruikerGS='M',
#gebruikerOL='Hoog',
#gebruikerGD=2011-11-11
This code affects the following procedure:
CREATE PROCEDURE userAanmaken
(
IN domeinNummer INT(11),
IN gebruikerNaam VARCHAR(45),
IN gebruikerPass VARCHAR(45),
IN gebruikerEmail VARCHAR(45),
IN gebruikerVN VARCHAR(50),
IN gebruikerTV VARCHAR(10),
IN gebruikerAN VARCHAR(50),
IN gebruikerGS VARCHAR(1),
IN gebruikerOL VARCHAR(30),
IN gebruikerGD DATE
)
BEGIN
DECLARE lastID INT;
INSERT INTO Gebruiker(Domein_idDomeint)
VALUES (domeinNummer);
SET lastId=LAST_INSERT_ID();
INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (lastId,gebruikerNaam,gebruikerPass);
INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (lastId,gebruikerEmail,gebruikerVN,gebruikerTV,gebruikerAN,gebruikerGS,gebruikerOL,gebruikerGD);
END$$
When i run the execute statement it returns with a syntax error.
How do i define the parameters in the SQL execute statement?
Seems that you missing date quotation marks '' on DATE:
#gebruikerGD='2011-11-11'
Maybe you need to use DELIMITER // in beggining of procedure and DELIMITER; in end of procedure. Check documentation.