mysql stored procedure (with 2 insert) error - mysql

i have this table
create table eveniment( +
evenimentId bigint not null auto_increment primary key,
evenimentDenumire varchar(500),
adresaId int not null);
create table adresa(
adresaId bigint not null auto_increment primary key,
localitate varchar(500),
judet varchar(500),
codPostal varchar(50),
strada varchar(500),
nr varchar(50),
bl varchar(50),
ap varchar(5),
email varchar(500),
www varchar(500));
and this procedure
DELIMITER //
drop procedure IF EXISTS insertEveniment;
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
and i get this error
ERROR 1064 (42000): 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 'insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) values(' at line 4
if i try the insert into adresa ... separately in mysql command i don't get error , what i do wrong?

Try like this (tried and it works for me):
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0;
DECLARE vEvenimentId int DEFAULT 0;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
You are using drop procedure statement with wrong delimiter (you were setting delimiter to // but then you put ; at the end of your statement. Put your drop procedure statement before you set the delimiter, and it will work just fine. Also, put delimiters after DECLARE

DECLARE is a separate statement, thus it needs to be delimited with a ;, like INSERT or SELECT or any other statement.
Also, move the first DELIMITER statement to just before the beginning of the procedure declaration.
So,
DELIMITER //
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0 ;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0 ;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();

Related

When I create a variable in create procedure on mySQL I get an error

//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_)
;

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 ;

MySQL (5.6.14) Syntax error with Declare and Temporary Table inside Procedure

I have following SQL-procedure:
DELIMITER $$
CREATE PROCEDURE Do_Stuff()
LANGUAGE SQL
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS users_temp AS (
SELECT id FROM users);
DECLARE i INT DEFAULT 0;
END;
$$
DELIMITER ;
Which gives an error: "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 'DECLARE i INT DEFAULT 0; END' at line 8"
Now, if I remove the Temporary Table, it runs fine.
DELIMITER $$
CREATE PROCEDURE Do_Stuff()
LANGUAGE SQL
BEGIN
DECLARE i INT DEFAULT 0;
END;
$$
DELIMITER ;
Also if I have some other statement other than the Declare in the Procedure it runs fine.
DELIMITER $$
CREATE PROCEDURE Do_Stuff()
LANGUAGE SQL
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS users_temp AS (
SELECT id FROM users);
SELECT * FROM users;
END;
$$
DELIMITER ;
What might be causing this syntax error?
From the docs:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
Move it before CREATE TEMPORARY TABLE.
Try:
DELIMITER $$
CREATE PROCEDURE Do_Stuff()
LANGUAGE SQL
BEGIN
DECLARE i INT DEFAULT 0;
CREATE TEMPORARY TABLE IF NOT EXISTS users_temp AS (
SELECT id FROM users);
-- DECLARE i INT DEFAULT 0;
END$$
DELIMITER ;

Set the value of a variable to the result from a SELECT statement in MySQL

I want to create the following function in Mysql, but the function does not get created but fails with an error
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE #AccountType varchar(50);
SELECT AccountType INTO #AccountType
FROM AccountType
WHERE AccountTypeID = AccountTypeID);
RETURN AccountType;
END $$
DELIMITER ;
A description of my table
CREATE TABLE AccountType(
AccountTypeID INT AUTO_INCREMENT PRIMARY KEY,
AccountType varchar(100) UNIQUE NOT NULL
);
The error I am getting is
ERROR 1064 (42000): 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 '#AccountType varchar(50);
I can't seem to find out what I am doing wrong. Can someone please help.
This should work:
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
SELECT AccountType INTO #AccountType FROM AccountType
WHERE AccountTypeID = AccountTypeID;
RETURN #AccountType;
END $$
DELIMITER ;
This will also work:
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE v_AccountType varchar(50) DEFAULT NULL;
SELECT AccountType INTO v_AccountType FROM AccountType
WHERE AccountTypeID = AccountTypeID;
RETURN v_AccountType;
END $$
DELIMITER ;
The first example uses a "global" variable (those starting with an '#' symbol). The second example uses a "local" variable.

SQL Syntax error creating stored procedure

I am creating a small (for now) stored procedure to insert some data. When I create this I get an error.
DROP PROCEDURE IF EXISTS InsertTESTData;
delimiter $$
CREATE PROCEDURE InsertTESTData (
p_pink_no VARCHAR,
p_carrier VARCHAR,
p_thisno INT
)
BEGIN
INSERT INTO RefDB (pink_no,carrier,thisno)
VALUES
(p_pink_no,p_carrier,p_thisno);
END $$
delimiter ;
And the error I always get is a 1064 SQL error as follows.
Error : 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 'p_carrier VARCHAR,p_thisno IN) BEGIN INSERT INTO RefDB (pink_no,carrier,t' at line 2
I thought I had everything covered and am now pulling my hair out!
You need to define a length for your varchar parameters like this
p_pink_no VARCHAR(100)
and change
p_thisno IN
to
p_thisno INT
Complete example:
DROP PROCEDURE IF EXISTS InsertTESTData;
delimiter $$
CREATE PROCEDURE InsertTESTData (
p_pink_no VARCHAR(100),
p_carrier VARCHAR(100),
p_thisno INT)
BEGIN
INSERT INTO RefDB (pink_no,carrier,thisno)
VALUES (p_pink_no,p_carrier,p_thisno);
END $$
delimiter ;
you have to declare the size of the data type ,after thal i am sure your code will be execute successfully.
follow this
DROP PROCEDURE IF EXISTS InsertTESTData;
delimiter $$
CREATE PROCEDURE InsertTESTData (
p_pink_no VARCHAR(20),
p_carrier VARCHAR(200),
p_thisno number(23))
BEGIN
INSERT INTO RefDB (pink_no,carrier,thisno)
VALUES (p_pink_no,p_carrier,p_thisno);
END $$
delimiter ;