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...
Related
I am getting this error
Error Code: 1415. Not allowed to return a result set from a trigger
when I am trying to execute this code below:
DELIMITER //
DROP TRIGGER IF EXISTS after_payment_insert//
CREATE TRIGGER after_payment_insert
AFTER INSERT
ON Payment FOR EACH ROW
BEGIN
SET #p = (Select ProductCode from Product Where ProductName = New.ItemName Limit 1);
Call insertInvoice(New.Email,New.InvoiceNumber, #p, New.ItemName, '1', New.Total, New.Status);
END//
DELIMITER ;
Also tried this one:
DECLARE var_productcode varchar(10);
Select ProductCode into var_productcode from Product Where ProductName = New.ItemName Limit 1;
Call insertInvoice(New.Email,New.InvoiceNumber, var_productcode, New.ItemName, '1', New.Total, New.Status);
Both of the code above result to error code 1415.
The insertInvoice is just a simple stored procudure for inserting data from the invoice table.
Below is the InsertInvoice proc
DELIMITER //
DROP PROCEDURE IF EXISTS insertInvoice//
CREATE PROCEDURE insertInvoice(
IN insert_Email VARCHAR(255),
IN insert_InvoiceNumber VARCHAR(10),
IN insert_ProductCode VARCHAR(50),
IN insert_ItemName VARCHAR(10),
IN insert_Quantity VARCHAR(10),
IN insert_Price VARCHAR(10),
IN insert_Status VARCHAR(50))
BEGIN
INSERT INTO Invoice(Email, InvoiceNumber, ProductCode, ItemName, Quantity, Price, Status)
VALUES(insert_Email, insert_InvoiceNumber,insert_ProductCode, insert_ItemName, insert_Quantity, insert_Price, insert_Status);
END
//DELIMITER ;
But when I use the normal Insert Into (....) Values(...) it just works.
I'm attempting to do an insert store procedure on mySQL as a little test.
The end result inserts all zeros into a row rather than the values passed into the definer.
My Code:
CREATE PROCEDURE `Test` (
IN IDvar int,
IN FirstNamevar nvarchar(50),
IN LastNamevar nvarchar(50),
IN Nationalityvar nvarchar(50),
In Agevar nvarchar(50),
)
BEGIN
INSERT INTO testtable (
ID,
FirstName,
LastName,
Nationality,
Age)
VALUES(
IDvar,
FirstNamevar,
LastNamevar,
Nationalityvar,
Agevar);
END
I click apply and get the following code to construct the store procedure
USE `testdb`;
DROP procedure IF EXISTS `Test`;
DELIMITER $$
USE `testdb` $$
CREATE PROCEDURE `Test` (
IN IDvar int,
IN FirstNamevar nvarchar(50),
IN LastNamevar nvarchar(50),
IN Nationalityvar nvarchar(50),
In Agevar nvarchar(50),
)
BEGIN
INSERT INTO testtable (
ID,
FirstName,
LastName,
Nationality,
Age)
VALUES(
IDvar,
FirstNamevar,
LastNamevar,
Nationalityvar,
Agevar);
END$$
DELIMITER;
If I look at the constructed store procedure code I get the following
CREATE DEFINER=`root`#localhost` PROCEDURE `Test`(
IN IDvar int,
IN FirstNamevar nvarchar(50),
IN LastNamevar nvarchar(50),
IN Nationalityvar nvarchar(50),
In Agevar nvarchar(50),
)
BEGIN
INSERT INTO testtable (
ID,
FirstName,
LastName,
Nationality,
Age)
VALUES(
IDvar,
FirstNamevar,
LastNamevar,
Nationalityvar,
Agevar);
END
When I then execute this store procedure with values:
IDvar = 3
FirstNamevar = TestFirstName
LastNamevar = TestLastName
Nationalityvar = GER
Age = 20
I get this out
set #IDvar = 0;
set #FirstNamevar = '0';
set #LastNamevar = '0';
set #Nationalityvar = '0';
set #Agevar = '0';
call testdb.Test(#IDvar, #FirstNamevar, #LastNamevar, #Nationalityvar, #Agevar);
select #IDvar, #FirstNamevar, #LastNamevar, #Nationalityvar, #Agevar;
Obviously this end with a row with all zero values. Anyone know why this is happening?
Cheers
1) You are passing all zeros into the stored procedure. So that is what is then inserted into the table.
2) Your SELECT at the end does not read from the table you insert into in the stored procedure but rather just returns the values of the variables - all of which are 0.
So everything is working as your code is telling it to.
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();
So I have this stored procedure, that I have tried to write a few different way, all to no avail.
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
((CALL GetUserId(USER_EMAIL)), NAME, GENDER);
END
I am getting this error
#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 'CALL GetUserId(USER_EMAIL)), NAME, GENDER)' at line 6
I have tried tinkering with for a while.
GetUserId works. I tried to store the result in a temporary variable and then insert it but that did not work.
Not to be shameless but If you can determine a solution, a solution where the CALL GetUserId is stored in a variable would be best.
You can't use it like this. Rewrite your GetUserId procedure with an OUT parameter.
Something like this:
DELIMITER $$
CREATE PROCEDURE GetUserId(IN p_email varchar(20), OUT p_id int)
BEGIN
SELECT id INTO p_id FROM users where email = p_email;
/*or whatever your procedure does*/
END$$
DELIMITER ;
Then your procedure CreateHero would look like this:
DELIMITER $$
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
DECLARE v_id int;
CALL GetUserId(USER_EMAIL, v_id);
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
(v_id, NAME, GENDER);
END$$
DELIMITER ;
Old thread but could help some one looking for later...
Base on fancyPants answer but more beautiful like this:
DELIMITER $$
CREATE FUNCTION GetUserId(IN p_email varchar(20)) RETURNS int
BEGIN
RETURN(SELECT id FROM users where email = p_email);
/*or whatever your function does*/
END$$
DELIMITER ;
And then:
DELIMITER $$
CREATE PROCEDURE `CreateHero` (IN USER_EMAIL VARCHAR(40),
IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES (GetUserId(USER_EMAIL), NAME, GENDER);
END$$
DELIMITER ;
CREATE PROCEDURE getNumbers
(
#dName VARCHAR(20),
#iNum INT OUTPUT,
#sNum INT OUTPUT
)
AS
BEGIN
#iNum=SELECT count(i.ID) FROM instructor WHERE dept_name=#dName
#sNum=SELECT count(s.ID) FROM student AS s WHERE dept_name=#dName
END
Here is my first attempt at making a stored procedure. I keep getting an error in syntax at the line: #dName VARCHAR(20),
I have looked at other problems and tried their solutions but nothing works!
Is it MSSQL ? If so you don't need to use brackets and you should use following syntax to assign variables:
CREATE PROCEDURE getNumbers
#dName VARCHAR(20),
#iNum INT OUTPUT,
#sNum INT OUTPUT
AS
BEGIN
SELECT #iNum=count(i.ID) FROM instructor WHERE dept_name=#dName;
SELECT #sNum=count(s.ID) FROM student AS s WHERE dept_name=#dName;
END
If you do it in MYSql
DELIMITER $$
CREATE PROCEDURE getNumbers(
IN dName VARCHAR(20),
OUT iNum INT,
OUT sNum INT)
BEGIN
SELECT count(i.ID) INTO iNum FROM instructor WHERE dept_name=dName;
SELECT count(s.ID) INTO sNum FROM student AS s WHERE dept_name=dName;
END$$
DELIMITER ;
Try this form:
CREATE PROCEDURE getNumbers
#dName VARCHAR(20),
#iNum INT OUTPUT,
#sNum INT OUTPUT
AS
BEGIN