SQL STORED PROCEDURE SAVING INTO VARIABLE - mysql

Can anybody spot the problem with this stored procedure? mysql is reporting the following error:
1048 Column 'categoryID' cannot be null
DELIMITER $$
CREATE PROCEDURE catalogue_assign_product_to_subcategory(
IN inProductId INT,
IN insubCategoryId INT)
BEGIN
DECLARE catID INT;
SELECT subcategoryParent FROM tblSubcategory
WHERE subcategoryID = insubCategoryId INTO catID;
INSERT INTO tblProdCat (productID, categoryID)
VALUES (inProductId, 'catID');
END

'catID' should be without the singlequotes in the INSERT statement.
I guess the field is defined as INT, you now try to insert a STRING
INSERT INTO tblProdCat (productID, categoryID)
VALUES (inProductId, catID);

Related

Declaring INT variable to return in MYSQL Function problem

I'm trying to build a mysql function which inserts values into a table and returns me the last_id inserted, which should be the same after executing the insertion. But Xampp gives me an error in the line where im declaring the "last_bill_id" variable. Can someone please help me to understand what am I doing wrong?
Here is the code for the function:
CREATE FUNCTION insert_bill(
client_id varchar (12),
bill_date date
) RETURNS INT
BEGIN
DECLARE last_bill INT;
INSERT INTO bill
(
client_id, bill_date
)
VALUES
(
client_id, bill_date
);
SET last_bill = LAST_INSERT_ID();
RETURN last_bill;
END $$
DELIMITER ;
Error: #1064 - Something is wrong about 'INT
you need another DELIMITER at the start
DELiMiTER $$
CREATE FUNCTION insert_bill(
client_id varchar (12),
bill_date date
) RETURNS INT
DETERMINISTiC
BEGIN
DECLARE last_bill INT;
INSERT INTO bill
(
client_id, bill_date
)
VALUES
(
client_id, bill_date
);
SET last_bill := LAST_INSERT_ID();
RETURN last_bill;
END $$
DELIMITER ;

Insert current datetime into mysql table resulting an error

I am creating a procedure called AccLikesVid inserting into a table named a_likes_v (table infomation):
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), type BOOL)
BEGIN
IF (type = 1) THEN
INSERT INTO a_likes_v VALUES (username, vidid, NOW(), 1);
ENDIF;
END$$
But when I execute the above code, MySQL Workbench generates an error: Error Code 1193. Unknown system variable 'now'
Could you tell me what's wrong with my code?
You should use:
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), v_type BOOL)
BEGIN
IF (v_type = 1) THEN -- TYPE is keyword, avoid such identifiers
INSERT INTO a_likes_v(account_name, video_id, dtime, liked)
VALUES (username, vidid, NOW(), 1); -- avoid blind insert
END IF; -- END IF not ENDIF
END$$
DBFiddle Demo

Create procedure in SQL fails

At the moment I'm trying to create a procedure in my databse with this query:
CREATE PROCEDURE InsertTitle(title VARCHAR(50), interpret VARCHAR(50), album VARCHAR(50))
BEGIN
INSERT IGNORE INTO Interpret (Name) VALUES (interpret);
SET #idInterpret := (SELECT id FROM Interpret WHERE Name = interpret);
INSERT IGNORE INTO Album (Name, Interpret) VALUES (#album, #idInterpret);
SET #idAlbum := (SELECT id FROM Interpret WHERE Name = album AND Interpret = #idInterpret);
INSERT INTO Lied (Name, Album, Interpret) VALUES (title, #idAlbum, #idInterpret,);
END;
But now I get the following error which doesn't tell my anything:
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
Do you have an idea where my mistake is? I use a MySQL server.
First, your problem might just be a delimited. Second, you are approaching the calculation of the inserted id incorrectly. You should use LAST_INSERT_ID():
DELIMITER $$
CREATE PROCEDURE InsertTitle(title VARCHAR(50), interpret VARCHAR(50), album VARCHAR(50))
BEGIN
INSERT IGNORE INTO Interpret (Name)
VALUES (interpret);
SELECT #idInterpret :-= LAST_INSERT_ID()
INSERT IGNORE INTO Album (Name, Interpret)
VALUES (#album, #idInterpret);
SELECT #idAlbum := LAST_INSERT_ID()
INSERT INTO Lied (Name, Album, Interpret)
VALUES (title, #idAlbum, #idInterpret);
END;
$$
DELIMITER ;
Also, set uses =, not :=. The latter is only needed in SELECT statements. And, you had an extra comma in the last VALUES() statement.
Try to use this solution:
delimiter //
CREATE PROCEDURE InsertTitle(title VARCHAR(50), interpret VARCHAR(50), album VARCHAR(50))
BEGIN
INSERT IGNORE INTO Interpret (Name) VALUES (interpret);
SET #idInterpret := (SELECT id FROM Interpret WHERE Name = interpret);
INSERT IGNORE INTO Album (Name, Interpret) VALUES (#album, #idInterpret);
SET #idAlbum := (SELECT id FROM Interpret WHERE Name = album AND Interpret = #idInterpret);
INSERT INTO Lied (Name, Album, Interpret) VALUES (title, #idAlbum, #idInterpret,);
END//
delimiter ;
For more details please see: MySQL CREATE PROCEDURE

MySql stored procedure no data fetched

SQLFiddle
I need to loop throught my cursor and insert new values in another table.
I have two tables:
CREATE TABLE peoples
(
id int auto_increment primary key,
name varchar(20),
enabled varchar(1)
)
/
INSERT INTO peoples
(name, enabled)
VALUES
('john', 'F'),
('jane', 'T')
/
CREATE TABLE test
(
id int,
logins int
)
/
I need to select all peoples enabled = T and insert a new entry in my second table 'test'. I created a stored procedure:
CREATE PROCEDURE sp_a()
BEGIN
DECLARE p int;
DECLARE done INT DEFAULT FALSE;
DECLARE peoples_cur CURSOR FOR select p.id from peoples p where p.enabled='T';
OPEN peoples_cur;
REPEAT
FETCH peoples_cur INTO p;
IF NOT done THEN
INSERT INTO test
(id, logins)
VALUES
(p, '999');
END IF;
UNTIL done END REPEAT;
CLOSE peoples_cur;
END;
/
But i got the error:
No data - zero rows fetched, selected, or processed: CALL sp_a()
SQLFiddle
Simple way:
DROP PROCEDURE IF EXISTS sp_a;
DELIMITER $$
CREATE PROCEDURE `sp_a`()
BEGIN
INSERT INTO test (logins)
SELECT COUNT(id) AS l FROM peoples WHERE enabled = 'T';
END$$
CALL sp_a();

mysql concat and insert into not working

i have a code in stored procedure which adds "PID-" to the id number, so if id number is 1, the result shoulb be PID-1. but it's not working.
Here's the code:
DROP PROCEDURE `inserproducts`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `inserproducts`(pid int,pname varchar(50),pdesc varchar(50),psupp varchar(50),pdate date,pquant int)
begin
insert into products(productid,productname,proddescription,supplier,lastpurchasedate,quantityleft)
values(select concat('PID',pid,pname),pdesc,psupp,pdate,pquant));
select pid=last_insert_id();
end
how can i join insert into and concat together? Please help me with this one.
Use INSERT INTO...SELECT
insert into products(productname,proddescription,supplier,lastpurchasedate,quantityleft)
select concat('PID',pid,pname),pdesc,psupp,pdate,pquant
You can omit the column productid if it is an AUTO_INCREMENT column.
I was wondering why you need to execute select pid=last_insert_id(); when pid is an IN parameter.
UPDATE 1
DROP PROCEDURE `inserproducts`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `inserproducts`
(
pid int,
pname varchar(50),
pdesc varchar(50),
psupp varchar(50),
pdate date,
pquant int
)
begin
insert into products
(productname,
proddescription,
supplier,
lastpurchasedate,
quantityleft)
select concat('PID',pid,pname), pdesc, psupp, pdate, pquant;
select last_insert_id();
end$$
DELIMITER ;
just following up... if you specify values you don't need the select - there's probably a slight performance gain by just using values:
DROP PROCEDURE `inserproducts`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `inserproducts`
(
in pid int,
in pname varchar(50),
in pdesc varchar(50),
in psupp varchar(50),
in pdate date,
in pquant int
)
begin
insert into products
(productname, proddescription, supplier, lastpurchasedate, quantityleft)
values
(concat('PID',pid,pname), pdesc, psupp, pdate, pquant );
select last_insert_id();
end$$
DELIMITER ;
I'd leave select alone unless you actually need data from another table...