0

I have defined a following procedure.


create procedure deleteData()
   begin
     DECLARE no_tbls INT;
     DECLARE tbl VARCHAR(64);
     DECLARE tbls_cr CURSOR for SELECT DISTINCT table_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='db';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_tbls=1;
    OPEN tbls_cr;
    SET no_tbls=0;
    while no_tbls=0 do
      fetch tbls_cr into tbl;
      select tbl.updated_at from tbl limit 1;
    end while;
    close tbls_cr;
  end

After running this procedure i am getting an error db.tbl doesn't exist. So i was searching if there is a way to use a cursor fetched object in another query. The problem i am doing all this tedious stuff is that i would like to delete data from all tables of a db with a particular where clause. Note: All tables has a column updated_at with date format. (I am a newbie to MySQL stored procs).

2
  • procedure's database name and tables database name are same ? Commented Mar 22, 2016 at 9:16
  • yes.. both DBs are the same Commented Mar 22, 2016 at 9:25

2 Answers 2

1

You cannot have a variable in place of a table name in the from clause in a plain select statement, mysql will look for a table named tbl in the database.

You need to use string concatenation and prepared statements to dynamically create and execute sql statements:

mysql> USE test;

mysql> CREATE TABLE t1 (a INT NOT NULL);

mysql> INSERT INTO t1 VALUES (4), (8), (11), (32), (80);

mysql> SET @table = 't1';

mysql> SET @s = CONCAT('SELECT * FROM ',@table);

mysql> PREPARE stmt3 FROM @s;

mysql> EXECUTE stmt3;

mysql> DEALLOCATE PREPARE stmt3;

Prepared statements also work in stored procedures, the above example demonstrates how to create the sql statement by concatenating string literals with variables, prepare the statement, execute it, and then release the prepared statement from memory.

Sign up to request clarification or add additional context in comments.

5 Comments

but how can i integrate this in a stored proc? if you can explain this ex. with the context of my ex. it'll be very helpful.. :)
I'm sorry, but I do not understand your question! You use the same commands as above, just the variables need to be declared differently, but you already know how to do that.
I got you.. I was trying in a same way like @VipinJain answered. ty :)
The only difference between my answer and @VipinJain is that he provided a copy-paste code, while I did not. It's easy to provide an answer that requires even less effort from the OP once sy else answered the question.
ur right @shadow but before his paste i was trying the same thing he wrote but missing a few things which got cleared by him.
0

Using Prepare Statement you can fetch data dynamically

create procedure deleteData()
   begin
     DECLARE no_tbls INT;
     DECLARE tbl VARCHAR(64);
     DECLARE tbls_cr CURSOR for 
         SELECT DISTINCT table_name 
         FROM INFORMATION_SCHEMA.COLUMNS 
         WHERE table_schema='db';
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_tbls=1;
     OPEN tbls_cr;   
     start_loop : LOOP
      fetch tbls_cr into tbl;
      set @b =  concat('select ', tbl, '.updated_at from ' , tbl, ' limit 1');
      prepare stmt3 from @b;
      execute stmt3;
      DEALLOCATE PREPARE stmt3;          
    END LOOP start_loop;
    close tbls_cr;
  end

3 Comments

Cool!! i think its working but where can i see the output? As it is a select query no changes to the DB will take place.. where should i put the final select statement wch will display the output of a select query?
this is also show the output, but if you want to put together then you can create a temp table and insert updated_at
its working.. i added external select statements to display values of @b & tbl variables. ty :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.