I need to utilize two insert statements in a single procedure in order to update a master and detail table, so I'm trying to use the delimiter keyword for this to work. I'm using phpMyAdmin 3.5.8, client libmysql - 5.0.96, and mysql 5.1.70. I've tried all sorts of syntax, but none seem to work. This is my query:
DELIMITER //
CREATE PROCEDURE `agregarPrincipio` (IN ID_PRODUCTO INT UNSIGNED, IN DESCR VARCHAR(100))
BEGIN
START TRANSACTION;
INSERT INTO `TM_PRINCIPIO_ACTIVO` (`DESCRIPCION`) VALUES(DESCR);
INSERT INTO TD_PRINCIPIO_ACTIVO(`ID_TD_PRINCIPIO_ACTIVO`,`ID_TM_PRINCIPIO_ACTIVO`) VALUES(ID_PRODUCTO,LAST_INSERT_ID());
COMMIT;
END
//
DELIMITER ;
Any ideas on what may be happening?
the problem is in the line
INSERT INTO TD_PRINCIPIO_ACTIVO(`ID_TD_PRINCIPIO_ACTIVO`,`ID_TM_PRINCIPIO_ACTIVO`) VALUES(ID_PRODUCTO,LAST_INSERT_ID());
. You are trying to directly call a stored procedure LAST_INSERT_ID() in your second select statement. If LAST_INSERT_ID() returns a single value, try to save it in a variable. Then use that variable
DECLARE last_id int default 0;
SET last_id= SELECT LAST_INSERT_ID(); #if it is a function other wise use exec
INSERT INTO TD_PRINCIPIO_ACTIVO(`ID_TD_PRINCIPIO_ACTIVO`,`ID_TM_PRINCIPIO_ACTIVO`) VALUES(ID_PRODUCTO,last_id);
Related
I am creating this basic procedure using MySQL Workbench to accept a single input parameter.
The table "unique_days" has a single PRIMARY KEY column called "dayid" which currently has a single ROW with a value of 1.
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET #maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
DELETE FROM unique_days WHERE dayid > 1;
WHILE (maxdate_current > maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
The procedure is then called with an integer parameter.
CALL dayid_iteration(11);
The variables are setting properly because I can run a select statement with the variable and it shows the correct new value. The deletion of dayid > 1 also works (Tested by manually adding additional rows, and then running procedure). However, I can't seem to get the WHILE statement to insert new rows with the value provided.
Any help is much appreciated. I searched multiple other questions, and countless forums, but everything looks like it should be working.
I am expecting the code to CREATE 9 ROWS for a total of 10 ROWS.
The following is included just so you can see the starting values of the table.
SELECT * FROM unique_days;
For anyone who finds this question, the following code functions correctly. The input variable on the parameter was not setting properly. Once the parameter had "IN" placed in front of the variable name, it correctly received the parameter.
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
WHILE (maxdate_current <= maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
CALL dayid_iteration(1000);
This call procedure now properly works.
CALL dayid_iteration(1000);
I write a procedure for MariaDB. I need to verify correct code.
DELIMITER //
CREATE PROCEDURE verifyInsert (IN value TINYTEXT)
BEGIN
IF NOT EXISTS (SELECT `table1`.Column1 FROM `table1`
WHERE `column1` = valore)
then INSERT INTO `table1`(`column1`) VALUES (value)
END IF;
END;
// DELIMITER;
This procedure must be verify if an inserted value is present.
If yes i do not anything else it insert in my database.
Someone could verify my code please?
As said, make unique column. And you can use
INSERT IGNORE INTO
so you just "eat" the error and go on as planned.
I am using the following stored procedure:
DELIMITER $$
USE `customer`$$
DROP PROCEDURE IF EXISTS `InsertCustomerEmail`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `InsertCustomerEmail`(IN p_Customer_ID INT(11),
IN p_from_who VARCHAR(50),
IN p_to_whom VARCHAR(50),
IN p_CC VARCHAR(50),
IN p_BCC VARCHAR(50),
IN p_Subject VARCHAR(500),
IN p_Massage VARCHAR(4000),
IN p_Is_Sent BIT(1),
IN p_Sent_When DATE,
IN p_Is_Active BIT(1),
OUT new_ID INT)
BEGIN
INSERT INTO customer_emails (
`Customer_ID`,
`from_who`,
`to_whom`,
`CC`,
`BCC`,
`Subject`,
`Massage`,
`Is_Sent`,
`Sent_When`,
`Is_Active`
) VALUES (p_Customer_ID,
p_from_who,
p_to_whom,
p_CC,
p_BCC,
p_Subject,
p_Massage,
p_Is_Sent,
p_Sent_When,
p_Is_Active);
SET #new_ID=SCOPE_IDENTITY();
END$$
DELIMITER ;
My database has an auto increment ID column, I would like to return this ID (the last one added) in the New_ID variable but when I run the CALL for the procedure it returns NULL for the New_ID.
Any suggestions?
Thanks!
The reason being the #new_ID assignment is not considered part of the transaction; the INSERT statement is. Move the following line outside the BEGIN ... END
SET #new_ID=SCOPE_IDENTITY();
Since you have only one operation in the batch, you can loose the BEGIN...END
EDIT
As Thorsten Dittmar suggested I need to explain a bit further.
The #new_ID assignment is executed right away, since it is not considered DML action and is not part of the batch. Thus SCOPE_IDENTITTY() is executed, but there is no identity generated yet, because the INSERT statement IS considered part of the batch and it is executed at its end.
EDIT 2
Since the OP added MySQL tag, above statement won't work, since there is no SCOPE_IDENTITY() in MySQL. Instead the correct function is LAST_INSERT_ID().
SET #new_ID=LAST_INSERT_ID();
I want to be able to pass arguments to stored procedure, so I searched the net and encountered something like this:
DELIMITER $$
CREATE PROCEDURE addTmpUser
#id varchar(10)
AS
BEGIN
//some sql code
END$$
DELIMITER ;
The problem is that I am getting a syntax error for the # character.
Note: I am using MySQL db.
You are mixing variable types.
#variable is a user variable with a scope for the entire connection.
The variables in stored procedures look different, they don't have the # before them.
Also, you need to declare them. Here is an example
DELIMITER $$
CREATE PROCEDURE addTmpUser(p_id varchar(10))
-- the variable is named p_id as a nameing convention.
-- It is easy for variables to be mixed up with column names otherwise.
BEGIN
DECLARE innerVariable int;
insert into user (id) values (p_id);
-- return all users
select * from user;
END$$
DELIMITER ;
-- and now call it
call addTmpUser(10);
You need to use IN,OUT,INOUT to specify the parameter. So you can try this
DELIMITER $$
CREATE PROCEDURE addTmpUser (IN id VARCHAR(10))
BEGIN
//some sql code
END$$
DELIMITER ;
Look at the documentation
I have a simple stored procedure which inserts records into four character fields in table. Below is the procedure
CREATE PROCEDURE dowhile()
BEGIN
DECLARE I INT DEFAULT 5
v1loop: WHILE I < 10000 DO
INSERT INTO TestTable1(A,B,C,D)
SELECT CONCAT(I,'A'), CONCAT(I,'B'), CONCAT(I,'C'), CONCAT(I,'D')
SET I = I + 1
END WHILE v1loop
END;
Checked online - there are no free MSSQL to MYSQL SQL Conversion Tools
Error is
- SQL Syntax Error in Insert - SELECT Statement
I have checked the syntax this seem to be correct.
Any pointers for this would be helpful.
Not to bad actually, you just need to add some semi-colons and change MySQL's default delimiter. This needs to be done since we're using SQL inside SQL.
DELIMITER $$
CREATE PROCEDURE dowhile()
BEGIN
DECLARE I INT DEFAULT 5;
v1loop: WHILE I < 10000 DO
INSERT INTO TestTable1(A,B,C,D)
SELECT CONCAT(I,'A'), CONCAT(I,'B'), CONCAT(I,'C'), CONCAT(I,'D');
SET I = I + 1;
END WHILE v1loop;
END$$
DELIMITER ;
Just tested this on my MySQL server.