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.
Related
//This is the code
create procedure st_insertRole(
#name varchar(30) //I get the error here
as
insert into roles values(#name)
);
here is the screenshot
My SQL does not allow session variables to be used for procedure parameters. You also have parentheses misplaced. The code in MySQL would look more like:
delimiter $$
create procedure st_insertRole (
in_name varchar(30)
)
begin
insert into roles (name)
values (in_name);
end; $$
The syntax is wrong
create procedure st_insertRole(
name_ varchar(30) )
insert into roles values(name_)
;
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 ;
I have below basic table:
create table hobbies (
id int primary key auto_increment,
name varchar(32) unique);
And I am trying to create a stored procedure to insert a new row in this table, like below:
delimiter //
create procedure add_hobby(hobby varchar(32))
begin
insert into hobbies(name) values(hobby);
end
delimiter //
call add_hobby('randomHobby');
When I call the procedure like above I am getting message "An unexpected error occured". I am running the queries on db-fiddle.com, MySQL ver 8.0. Could anyone offer some guidance if I've done something wrong or missed something? I can mention that procedure with select operation and no parameters works. Many thanks in advance.
You need to set your delimiter to begin with and then reset it at the end
for your parameters you need to set weather they are IN or out then the name and the type
delimiter //
create procedure add_hobby(IN hobby varchar(32))
begin
insert into hobbies(name) values(hobby);
end//
delimiter ;
My tables are
create table employee(
id int(10) auto_increment primary key,
name varchar(100),
addressId int(10)
);
go
create table address(
id varchar(10) auto_increment primary key,
name varchar(100)
);
Here is my procedure
create procedure insert_employee(IN emp_name varchar(100),IN emp_address varchar(100))
begin
DECLARE #addressId varchar(10);
SELECT #addressId:=id from address where name LIKE '%'+emp_address+'%';
IF #addressId = ''
THEN
set #addressId= 'DBS-2136';-- It will come form function
INSERT INTO address values(#addressId,emp_address);
END IF
INSERT INTO employee values(emp_name,#addressId);
END
I don't understand what is the problem. If i write this type of if condition in ms sql server there is not error. every time execute the procedure ti say error in end if. I have search in google but there is not idea about this. there is a problem in declare variable. If i copy form mysql documentation that also not work. why is that?
please help me
1. What is the proper way to declare variable under mysql stored procedure,
2. how to write if condition in mysql stored procedure.
thank you
Lots of differences between mysql and mssql. Declared variables should not include '#', all statements must be terminated, + is an arithmetic operator, if you procedure has multiple statements you must set delimiters before and after.
Further reading
How to declare a variable in MySQL?
https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
From MySQL reference Manual
An IF ... END IF block, like all other flow-control blocks used within stored programs, must be terminated with a semicolon
IF #addressId = ''
THEN
set #addressId= 'DBS-2136';-- It will come form function
INSERT INTO address values(#addressId,emp_address);
END IF;
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.