I'm doing some POC. I've written one stored procedure in MySQL. I'm using MySQLWorkbench for database operations like creating new table, stored procedures, query execution etc. I'm observing some unexpected execution behavior even though code looks correct syntactically and logically.
Here are the points.
Approach 1 -
In first approach, I'm creating temporary table and adding records using INSERT INTO ...SELECT statement with selected columns.
CREATE TEMPORARY TABLE XYZ(....);
INSERT INTO XYZ (....) SELECT (....) FROM ABC WHERE clause;
After doing this, I get null values only in couple of columns of temp table and all other temp table columns gets populated with correct value.
If I run same select statement (for same where clause) in separate SQL tab, I can see correct output even for those couple of columns getting null value in temp table.
If I change above INSERT INTO..SELECT statement with * (all columns), then I get correct output in temp table..
INSERT INTO XYZ SELECT * FROM ABC WHERE clause;
But don't want all columns. I also tried creating temp table with ENGINE = value. But didn't work. Even I tried all this approach with normal table (without TEMPORARY), but unfortunately
Approach 2 -
In second approach, I modified same stored procedure with cursor instead of temp table. But even in this case cursor is not getting any record(s).
DECLARE cur1 CURSOR FOR SELECT (....) FROM ABC WHERE clause;
If I run same select statement (for same where clause) in separate SQL tab, I can see correct output.
Am I doing something wrong? Can I get any suggestion(s) on this?
Thanks in advance.
Looking at SQL Profiler results from these queries (each were run 10 times and averages are below) we can see that the CTE just slightly outperforms both the temporary table and table variable queries when it comes to overall duration.
MySQL has a feature to create a special table called a Temporary Table that allows us to keep temporary data. We can reuse this table several times in a particular session. It is available in MySQL for the user from version 3.23, and above so if we use an older version, this table cannot be used.
Stored procedures can reference temporary tables that are created during the current session. Within a stored procedure, you cannot create a temporary table, drop it, and then create a new temporary table with the same name.
I had figured out the problem in Approach #1. The root cause was the variable name used as one of IN parameter. I had used same IN variable name as my table column name. And that is why, I believe, somehow it was over writing actual table values. I changed IN variable name and my stored proc ran correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With