Call procedure sql-tool vscode - mysql

I couldn't figure out where the problem is!
The procedure is successfully created but the call throws the following error:
Cannot read properties of undefined (reading 'undefined')
Full example:
-- #block
CREATE TABLE index_table(
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
creat_at TimeSTAMP DEFAULT now()
);
-- #block
INSERT INTO index_table(first_name, last_name)
VALUES ('fname1', 'lname1'),
('fname2', 'lname2'),
('fname3', 'lname3'),
('fname4', 'lname4');
-- #block
DROP PROCEDURE if EXISTS get_table;
CREATE PROCEDURE get_table()
BEGIN
SELECT 2 ;
END
-- #block
CALL get_table();

You missed changing the delimiter. Basically, you need to change the delimiter to something else, so the creation of the procedure will be a single command and will not be confounded with the inner commands of the procedure. This is a tested and working version of your code:
-- #block
CREATE TABLE index_table(
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
creat_at TimeSTAMP DEFAULT now()
);
-- #block
INSERT INTO index_table(first_name, last_name)
VALUES ('fname1', 'lname1'),
('fname2', 'lname2'),
('fname3', 'lname3'),
('fname4', 'lname4');
delimiter //
-- #block
DROP PROCEDURE if EXISTS get_table;
CREATE PROCEDURE get_table()
BEGIN
SELECT 2 ;
END //
delimiter ;
-- #block
CALL get_table();

Related

Error while creating a procedure mysql CRUD

I keep getting a syntax error at line 9
CREATE PROCEDURE `ItemsAddOrEdit`(
_itm_id INT,
_itm_name VARCHAR(255),
_itm_price FLOAT(8,2)
)
BEGIN
IF _itm_id = 0 THEN
INSERT INTO items (itm_name, itm_price)
VALUES (_itm_name, _itm_price);
ELSE
UPDATE items
SET
itm_name = _itm_name,
itm_price = _itm_price
WHERE itm_id = _itm_id;
END IF;
END
Are the variables the problem? I've checked the table to see if I messed the names up, but it all seems fine to me.
Here's the table code
CREATE TABLE `items` (
`itm_id` INT(255) NOT NULL AUTO_INCREMENT,
`itm_name` VARCHAR(255) NOT NULL,
`itm_price` FLOAT(8,2) NOT NULL,
PRIMARY KEY (`itm_id`),
UNIQUE INDEX `itm_name` (`itm_name`)
)
You need to redefine Delimiter to something else, for eg: $$. This allows parser to ignore ; (hence do not execute statement on reaching ;).
Also, as a good practice, always use DROP PROCEDURE IF EXISTS, to avoid failing out in case procedure with same name already exists.
At the end, redefine the Delimiter back to ;
Try the following:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ItemsAddOrEdit` $$
CREATE PROCEDURE `ItemsAddOrEdit`(
_itm_id INT,
_itm_name VARCHAR(255),
_itm_price FLOAT(8,2)
)
BEGIN
IF _itm_id = 0 THEN
INSERT INTO items (itm_name, itm_price)
VALUES (_itm_name, _itm_price);
ELSE
UPDATE items
SET
itm_name = _itm_name,
itm_price = _itm_price
WHERE itm_id = _itm_id;
END IF;
END $$
DELIMITER ;

MYSQL TRIGGER with IF condition

I'm a noob im MYSQL and I'm trying to make a trigger who will auto-fill 2 fields(customername, customersurname) in an invoice table. Can anyoane explain me please what I do wrong in that trigger? Thank you very much! :)
CREATE TABLE customer(
customerID INT(6) PRIMARY KEY AUTO_INCREMENT,
customername VARCHAR(20) NOT NULL,
customersurname VARCHAR(20) NOT NULL,
customeraddress VARCHAR(200)
);
CREATE TABLE invoice(
invoiceID INT (6) Primary KEY AUTO_INCREMENT,
customerID INT (6) REFERENCES customer(customerID),
customername VARCHAR(20) REFERENCES customer(customername),
invoicedate DATE,
orderID INT(6) REFERENCES orders(orderID),
products VARCHAR(200) REFERENCES orderlines(productname),
FOREIGN KEY(orderID) REFERENCES orders(orderID)
);
CREATE TRIGGER autofill_invoice BEFORE INSERT ON invoice FOR EACH ROW
BEGIN
IF (new.customerID = customer.customerID,
new.customersurname = customer.customersurname )
THEN
SET new.customername = customer.customername,
new.customersurname = customer.customersurname;
END IF;
END;
As mysql documentation on defining stored programs explains:
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. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
So, use the delimiter command when you define the stored procedure:
DELIMITER //
CREATE TRIGGER autofill_invoice BEFORE INSERT ON invoice FOR EACH ROW
BEGIN
IF (new.customerID = customer.customerID,
new.customersurname = customer.customersurname )
THEN
SET new.customername = customer.customername,
new.customersurname = customer.customersurname;
END IF;
END//
DELIMITER ;
Next time pls provide exact error description in your question!

SELECT inside stored proc doesn't work correctly whereas outside it does

i have a simple database as follows in mysql:
DROP TABLE IF EXISTS MEDIAVEND.ITEMDETAILS;
CREATE TABLE IF NOT EXISTS MEDIAVEND.ITEMDETAILS (
ItemID BIGINT UNSIGNED NOT NULL,
ITEMTYPE ENUM('MUSIC', 'MOVIE', 'GAME') NOT NULL,
ITEMMEDIATYPE ENUM('CD', 'DVD', 'BLURAY') NOT NULL,
ITEMNAME CHAR(100) NOT NULL,
ITEMDESCRIPTION CHAR(200) NOT NULL,
ITEMCOUNT BIGINT NOT NULL,
MACHINEID BIGINT NOT NULL,
PRIMARY KEY (ITEMID)
);
INSERT INTO MEDIAVEND.ITEMDETAILS VALUES
(1,'MUSIC','CD','ALBUM 1','SOME DESCRIPTION OF 1',100,1),
(2,'MUSIC','CD','ALBUM 2','SOME DESCRIPTION OF 2',20,1),
(3,'MOVIE','DVD','MOVIE 1','SOME DESCRIPTION OF 1',30,1),
(4,'MOVIE','BLURAY','MOVIE 2','SOME DESCRIPTION OF 2',40,1),
(5,'GAME','DVD','GAME 1','SOME DESCRIPTION OF 1',50,1),
(6,'GAME','BLURAY','GAME 2','SOME DESCRIPTION OF 2',60,1);
and a stored proc as :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `info_ItemType`(in itemType char(10))
BEGIN
SELECT * FROM MEDIAVEND.ITEMDETAILS WHERE ITEMTYPE= itemType;
END
so when i call following i see different results.
select * from mediavend.itemdetails where itemType='MUSIC';
-- 2 rows returned
call mediavend.info_ItemType('MUSIC');
-- 6 rows returned
i am really baffled, pardon me if it is a silly mistake, but i have gone through lots of questions and don't see any relevant to this.
Rename your input parameter since it has the same name as the column.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `info_ItemType`(in itemType_Param char(10))
BEGIN
SELECT * FROM MEDIAVEND.ITEMDETAILS
WHERE ITEMTYPE = itemType_Param;
END
$$
MySQL is not case sensitive, so when you say where ITEMTYPE = itemType, you're looking for where the column is equal to itself, hence all rows are returned.
You need to use a unique parameter name.

MySQL error in a procedure #1351 - View's SELECT contains a variable or parameter

PhpMyAdmin doesn't accept the parameters "id_jugador_IN" inside the procedure.
This is the procedure we're executing:
SET GLOBAL event_scheduler = 1;
use traviandatabase;
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_monitoritzacio_jugador $$
CREATE PROCEDURE sp_monitoritzacio_jugador(IN id_jugador_IN INT(10))
BEGIN
CREATE OR REPLACE
VIEW dades_ususari AS
SELECT j.nom AS jugador, j.sexe AS sexe, j.edat AS edat, j.correu AS correu, a.nom AS alianca, p.nom AS pais, c.nom_ciutat AS ciutat
FROM jugador AS j
JOIN alianca AS a
JOIN pais AS p
JOIN ciutat AS c
ON j.id_jugador = id_jugador_IN
GROUP BY id_jugador_IN;
END $$
DELIMITER ;
CALL sp_monitoritzacio_jugador(1);
And this is the table "jugador":
DROP TABLE IF EXISTS `jugador`;
CREATE TABLE `jugador` (
`id_jugador` int(10) NOT NULL AUTO_INCREMENT,
`id_raca` int(10) NOT NULL,
`id_pais` int(10) NOT NULL,
`id_alianca` int(10) DEFAULT '0',
`nom` varchar(20) NOT NULL,
`sexe` enum('Home','Dona') NOT NULL,
`edat` int(10) NOT NULL,
`correu` varchar(20) NOT NULL,
PRIMARY KEY (`id_jugador`),
KEY `jugador-alianca` (`id_alianca`),
KEY `id_pais` (`id_pais`),
KEY `id_raca` (`id_raca`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So it doesn't stop displaying the same error:
MySQL said: 1351 - View's SELECT contains a variable or parameter
As you see - view cannot use variables, it is a limitation. You are trying to create a view with exact WHERE condition; construct the CREATE VIEW statement text firstly, then use prepared statements to execute the statement, it will help you. But, do you really need it, to create, create and create new view?
EDIT: This is the content of the reference in the comment.
Olexandr Melnyk A simple workaround for MySQL's limitation on local variables usage in views is to use a function, which returns variable's value:
create function book_subject
returns varchar(64) as
return #book_subject;
create view thematical_books as
select title
, author
from books
where subject = book_subject();

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