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