calling stored procedure inserts NULLs into all columns MySQL - mysql

I've created a stored procedure in MySQL to help debug something, however when i call the stored procedure it inserts NULL values into my table for all columns
Table
CREATE TABLE `EncryptionDebug` (
`ObservedValue` mediumtext COLLATE utf8_unicode_ci,
`PublicKey` mediumtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Stored procedure
DELIMITER //
CREATE PROCEDURE `EncryptionDebug`(IN `ObservedValue` MEDIUMTEXT, IN `PublicKey` MEDIUMTEXT)
MODIFIES SQL DATA
BEGIN
DECLARE `ObservedValue` MEDIUMTEXT;
DECLARE `PublicKey` MEDIUMTEXT;
INSERT INTO `EncryptionDebug` (`ObservedValue`,`PublicKey`) VALUES (ObservedValue,PublicKey);
END//
DELIMITER ;
Calling the procedure like so
CALL EncryptionDebug('test','test');
Returns NULL for both columns when i SELECT * FROM EncryptionDebug
Thanks

From the documentation:
13.6.4.2 Local Variable Scope and Resolution
...
A local variable should not have the same name as a table column.
...
One option to try:
DELIMITER //
CREATE PROCEDURE `EncryptionDebug1`(
`ObservedValue` MEDIUMTEXT,
`PublicKey` MEDIUMTEXT
)
MODIFIES SQL DATA
BEGIN
/*
DECLARE `ObservedValue` MEDIUMTEXT;
DECLARE `PublicKey` MEDIUMTEXT;
*/
INSERT INTO `EncryptionDebug` (`ObservedValue`, `PublicKey`)
VALUES
(`ObservedValue`, `PublicKey`);
END//
DELIMITER ;
SQL Fiddle demo
Recommendation: Avoid naming parameters and variables as columns of your tables, here the cause: SQL Fiddle.

Related

Prepared Statement get wrong result in MYSQL

I have a table with design
CREATE TABLE IF NOT EXISTS InsuranceContract (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`enquiryCode` VARCHAR(20) DEFAULT NULL,
`contractCode` VARCHAR(20) DEFAULT NULL,
`createdAt` DATETIME DEFAULT CURRENT_TIMESTAMP (),
`updatedAt` DATETIME DEFAULT CURRENT_TIMESTAMP () ON UPDATE CURRENT_TIMESTAMP (),
UNIQUE KEY (`enquiryCode`)) ENGINE=INNODB DEFAULT CHARSET=UTF8 COLLATE = UTF8_BIN;
Then I was created a procedure like this
DROP procedure IF EXISTS `sp_insurance_contract_get`;
DELIMITER $$
CREATE PROCEDURE `sp_insurance_contract_get` (enquiryCode VARCHAR(20), contractCode VARCHAR(20))
BEGIN
SET #t1 = "SELECT * FROM InsuranceContract
WHERE InsuranceContract.enquiryCode = enquiryCode
AND InsuranceContract.contractCode = contractCode;";
PREPARE param_stmt FROM #t1;
EXECUTE param_stmt;
DEALLOCATE PREPARE param_stmt;
END$$
DELIMITER ;
And I was executed this procedure in MySQL Workbench by this command:
CALL sp_insurance_contract_get('EQ000000000014', '3001002');
I expected I will receive 1 row result but it selected all records in this table.
If I copy and create exactly this #t1 into plain SQL not using statement, it's correct.
Please help me to fix this error. I'm using MySQL 8.0.19
You can use placehoders on prepare statements, this is why we use them to prevent sql injection
One other thing never use column names as variables names, databases can not differentiate
DROP procedure IF EXISTS `sp_insurance_contract_get`;
DELIMITER $$
CREATE PROCEDURE `sp_insurance_contract_get` (enquiryCode_ VARCHAR(20), contractCode_ VARCHAR(20))
BEGIN
SET #t1 = "SELECT * FROM InsuranceContract
WHERE enquiryCode = ?
AND contractCode = ?;";
PREPARE param_stmt FROM #t1;
SET #a = enquiryCode_;
SET #b = contractCode_;
EXECUTE param_stmt USING #a, #b;
DEALLOCATE PREPARE param_stmt;
END$$
DELIMITER ;
When you say
WHERE enquiryCode = enquiryCode
you compare that named column to itself. The result is true always (unless the column value is NULL).
Change the names of your SP's parameters, so you can say something like
WHERE enquiryCode_param = enquiryCode
and things should work.
Notice that you have no need of a MySql "prepared statement" here. In the MySql / MariaDb world prepared statements are used for dynamic SQL. That's for constructing statements within the server from text strings. You don't need to do that here.

ERROR 1415: 1415: Not allowed to return a result set from a trigger

I have created a stored procedure with one argument. I have made a trigger to call stored procedure. Trigger calls stored procedure on change of a column value a table(device_session1).If is_active is updated to 'No' then trigger calls stored procedure. I pass that column value in stored procedure and procedure prints it but its giving error when i update column value.
Table-
CREATE TABLE `device_session1` (
`id` varchar(75) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`is_active` varchar(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
PRIMARY KEY (`id`));
insert into device_session1(id,is_active) values ('11','YES');
Trigger-
Delimiter $$
create trigger after_device_session_update
after update on device_session1
For Each Row
BEGIN
IF (NEW.is_active="NO") THEN
SET #session_id = new.id;
call new_procedure1(#session_id);
END IF;
END;
Delimiter;
Stored Procedure-
CREATE PROCEDURE `new_procedure1`(IN id VARCHAR(50))
BEGIN
select concat('id : ',id);
END

Drop procedure if exists in mysql

Hi i am trying to create a mysql script that I can run whenever I need to update my database. The script creates a table and then executes some stored procedures.
DELIMITER $$
CREATE TABLE IF NOT EXISTS tbl_name (
col1 bigint(20) NOT NULL AUTO_INCREMENT,
col2 varchar(255) NOT NULL,
col3 varchar(64) NOT NULL,
col4 datetime DEFAULT NULL,
PRIMARY KEY (`col1 `),
UNIQUE KEY col2 (`col2`)
) ENGINE=InnoDB AUTO_INCREMENT=572 DEFAULT CHARSET=utf8$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myproc`(IN username
VARCHAR(255))
BEGIN
DECLARE var1 VARCHAR(64);
DECLARE expirationDate DATETIME;
SET var1 = 12345;
SET expirationDate = DATE_ADD(NOW(), INTERVAL 30 SECOND);
REPLACE INTO tbl_name (col2, col3, col4) VALUES (someval, var1, expirationDate);
END$$
DELIMITER ;
When I ran the script first time, it created the table and executed the stored procedure in MySQL Workbench. When I ran the same thing second time, I got the error 1304 procedure already exists.
I looked online here about dropping the procedure and then create again. But when I entered the below command before creating the procedure, i got an error on CREATE command with code 1064.
DROP PROCEDURE IF EXISTS myproc;
CREATE DEFINER=`root`#`localhost` PROCEDURE `myproc`(IN username
VARCHAR(255))
.
.
.
I am very new to mysql and not sure how to execute the procedure if it already exists.
Any help would be appreciated.
Since you changed to DELIMITER $$ you need to use that delimiter at the end of each statement until you change it back.
DROP PROCEDURE and CREATE PROCEDURE are separate statements, and each requires its own statement delimiter.
DROP PROCEDURE IF EXISTS myproc $$
Note the delimiter at the end of the line above.
CREATE DEFINER=`root`#`localhost` PROCEDURE `myproc`(IN username
VARCHAR(255))
.
.
.
END $$
And another delimiter at the end of the whole CREATE PROCEDURE statement.

pass one stored procedure result into other stored procedure as parameter in Mysql

I have two function called in stored procedure:
CALL createPost(IN value,#Outvalue);
Now I want to pass '#Outvalue' to other stored procedure:
CALL createPostMedia(#Outvalue);
value is retrieved when we run SELECT #Outvalue; But #Outvalue value is not pass to createPostMedia() in parameters
I created a small test that can be useful:
DELIMITER //
CREATE PROCEDURE `createPost`(
IN `_value` VARCHAR(50),
OUT `_out_post_id` BIGINT UNSIGNED
)
BEGIN
INSERT INTO `post` (`value`) VALUES (`_value`);
SET `_out_post_id` := LAST_INSERT_ID();
END//
CREATE PROCEDURE `createPostMedia`(
IN `_in_post_id` BIGINT UNSIGNED
)
BEGIN
INSERT INTO `postmedia` (`post_id`) VALUES (`_in_post_id`);
END//
CREATE PROCEDURE `sp_test`()
BEGIN
CALL `createPost`('post_one', #`_out_post_id`);
CALL `createPostMedia`(#`_out_post_id`);
END//
CALL `sp_test`//
DELIMITER ;
SQL Fiddle demo

MySql Procedure values for insert as a variable

I have some trouble with MySql Procedure.
I have:
DROP TABLE IF EXISTS `employees2`;
CREATE TABLE `employees2` (
`LastName` varchar(20) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`FirstName` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL default ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It work's and is OK.
And MySQL Procedure:
DROP PROCEDURE IF EXISTS gen;
DELIMITER $$
CREATE PROCEDURE gen()
BEGIN
DECLARE e1 TEXT;
DECLARE e2 TEXT;
DECLARE e3 TEXT;
SET e1 = "Davolio";
SET e2 = "Nancy";
SET e3 = "Ron , Deplin";
insert into `employees2`(`LastName`,`FirstName`) values ('Nonew','adams');
insert into `employees2`(`LastName`,`FirstName`) values (e1,e2);
insert into `employees2`(`LastName`,`FirstName`) values (e3);
END $$
DELIMITER ;
call gen();
I would like to instert into table values from variable e3. "Ron" is for column LastName and "Deplin" is for column FirstName. But i got error: "Error Code: 1136. Column count doesn't match value count at row 1" First and second inserts works fine. How to force the third insert to work ?
You're only specifying one value with two columns named in your last insert, hence the unequal row count.
See this post on how to use MySQL to split a string like that got two value entries:
https://stackoverflow.com/a/9953163/2812842