I have to test some features with store procedures in mysql, so i trying to create one in MySQL:
CREATE OR REPLACE PROCEDURE insert_test(IN in_id Varchar(20), IN in_name Varchar(20))
BEGIN
insert into test(id, name) values(in_id, in_name);
END
but I have an error:
Error executing INSERT statement. Unknown column 'in_id' in 'field list' - Connection: MySQLConnection: 16ms
What's wrong with that? If i change in_d and in_name variables to static values like 'test1', 'test2' it works ok.
upd:
I tried with delimeters - no effect:
DELIMITER //
CREATE OR REPLACE PROCEDURE insert_test(IN in_id Varchar(10), IN in_name
Varchar(30))
BEGIN
insert into test(id, name) values(in_id, in_name);
END //
DELIMITER ;
Ok, i have created my table and store procedure via DB Browser in Intellij Idea, m.b. therw was a problem. I just recreated table and procedure via mysql console.
Related
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.
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_or_update`$$
CREATE PROCEDURE insert_or_update(
IN username VARCHAR(70),
IN score INT,
IN titlein VARCHAR(70)
)
begin
IF EXISTS (SELECT * FROM two_player WHERE title=titlein and user1!=username and user2='') THEN
UPDATE two_player SET score12=score , user2=username WHERE title=titlein and user1!=username and user2='' limit 1;
ELSE
INSERT INTO two_player (user1,score11,title) values (username, score, titlein);
END if;
END$$
DELIMITER ;
call insert_or_update('sara',20,'math');
I create a procedure. But when I try to call it I get this error message:
#1305 - PROCEDURE u941310304_menu.insert_or_update does not exist
What's wrong?
I tested you code and the call to procedure works.
Your default database is u941310304_menu, it seems you are creating the procedure in another db. You can create the procedure specifying the destination database.
DELIMITER $$
DROP PROCEDURE IF EXISTS `u941310304_menu`.`insert_or_update`$$
CREATE PROCEDURE `u941310304_menu`.insert_or_update(
[...]
If the procedure is in another database you must specify the db name as prefix:
call `another_database`.insert_or_update('sara',20,'math');
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();
Getting error during Creating Stored Procedure for Callable Statement:
I know some thing very simple going wrong, but i'm just unable to figure out!
My QUERY:
USE demo;
1. CREATE PROCEDURE
2. INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
3. BEGIN
4. INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
5. END;
/
SQL query:
CREATE PROCEDURE
INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
BEGIN
INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
MySQL said: Documentation
#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 '' at line 4
Appreciate your help!
Thank for your time!
When you write a stored procedure in MySQL, you should use the DELIMITER statement. In addition, you should name your columns so they do not conflict with column names. And, when using INSERT always list the columns name. So:
DELIMITER $$
CREATE PROCEDURE INSERT_emp_data (
IN IN_ID INT,
IN IN_NAME VARCHAR(2),
IN IN_AGE INT,
IN IN_IMAGE BLOB
)
BEGIN
INSERT INTO emp_data(id, name, age, image)
VALUES(IN_ID, IN_NAME, IN_AGE, IN_IMAGE);
END;
$$
DELIMITER ;
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);