MYSQL - Stored Procedure - mysql

I'm trying to create a temporaryTable in a stored procedure with the following code:
DELIMITER $$
CREATE PROCEDURE testprocedure
AS
BEGIN
CREATE TEMPORARY TABLE Best_MitarbeiterLieferant
(`enter code here`
angestellter VARCHAR (50),
AnzahlBestellungen INT NOT NULL,
Lieferant VARCHAR(50)
)
#Test Datensatz
INSERT INTO Best_MitarbeiterLieferant VALUES ('Stefan', 12, 'UPS')
#Testabfrage
SELECT * FROM Best_MitarbeiterLieferant
# Tabelle wieder löschen
DROP TABLE Best_MitarbeiterLieferant
END $$
DELIMITER ;
But I am getting an error, and I don't know how to solve it. Appreciate help!
1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'AS BEGIN CREATE TEMPORARY TABLE Best_MitarbeiterLieferant (
angeste' at line 3
Thanks in advance!

Try out this:
DELIMITER $$
CREATE PROCEDURE testprocedure()
BEGIN
CREATE TEMPORARY TABLE Best_MitarbeiterLieferant
(
angestellter VARCHAR (50),
AnzahlBestellungen INT NOT NULL,
Lieferant VARCHAR(50)
);
#Test Datensatz
INSERT INTO Best_MitarbeiterLieferant VALUES ('Stefan', 12, 'UPS');
#Testabfrage
SELECT * FROM Best_MitarbeiterLieferant;
# Tabelle wieder löschen
DROP TABLE Best_MitarbeiterLieferant;
END $$
DELIMITER ;

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

Error 1064 in a stored procedure in MySQL 8.0

I am using MySQL Workbench 8.0 CE and i'm trying to create a stored procedure to show 2 fields from my table. I am receiving the following error:
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 '' at line 3
This is my table:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(200),
age INT,
final_grade DOUBLE,
sex VARCHAR(1)
)
And this is the procedure:
CREATE PROCEDURE show_name_grade ()
BEGIN
SELECT name,final_grade FROM student;
END
You will need to redefine Delimiter to something else other than ;. At the end, define it back to ;
DELIMITER $$
CREATE PROCEDURE show_name_grade ()
BEGIN
SELECT name,final_grade FROM student;
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 ;

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 ;

mysql stored procedure (with 2 insert) error

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();