Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting expected output while using Temp Table or Cursor in MySQL

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.

like image 309
Shivaprasad Ligade Avatar asked Oct 24 '12 10:10

Shivaprasad Ligade


People also ask

Is using CTE better than temp table?

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.

Does MySQL support temp table?

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.

Can I pass temp table to stored procedure?

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.


1 Answers

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.

like image 99
Shivaprasad Ligade Avatar answered Oct 19 '22 11:10

Shivaprasad Ligade