How to execute a query which is stored in a table column MySQL?

让人想犯罪 __ 提交于 2019-12-03 16:31:22

You can use a cursor to get each REPORT_QUERY on CT table, and execute that using prepared statements:

delimiter $$
drop procedure if exists run_queries$$
create procedure run_queries()
begin

    declare s_query varchar(255);

    declare done bool default false;
    declare c_queries cursor for    
        select REPORT_QUERY from CT;
    declare continue handler for not found set done = true;


    open c_queries;
    read_loop: loop

        fetch c_queries into s_query;
        if done then 
            leave read_loop;
        end if;

        -- run the query
        set @sql = s_query;
        prepare stmt from  @sql;
        execute stmt;
        deallocate prepare stmt;
    end loop;

end$$

After create procedure, you can call as bellow:

call run_queries();

That's it.

If you want to execute SQL that is stored in column of some MySQL table, try this:

SELECT sql_text INTO @sql FROM sql_queries WHERE id = 1;
PREPARE sql_query FROM @sql;
EXECUTE sql_query;

Contents of the table sql_queries:

+----+--------------------------+
| id |         sql_text         |
+----+--------------------------+
|  1 | select * from companies; |
+----+--------------------------+

As i understand your question is that you need to run the queries which are stored in table column in SQL Form.

Here you can fetch the queries form table column and execute them in mysqli.

SQL QUERY TABLE IMAGE HERE THE QUERIES ARE STORED

SQL EMPLOYEE TABLE [WHICH IS DYNAMICALLY CALLED BY QUERY TABLE DURING EXECUTION]

PHP SQL CODE : `

<?php
    $link = new mysqli ('localhost','root','admin','demo');

    if($link->connect_error){
        die ("Connection Failed".$link->connect_error);
    }

    //YOU NEED THIS AS YOU NEED TO ACCESS THE COLUMN DATA IN TABLE
    $sql = "SELECT * FROM querytable WHERE id=1";

    if($res = $link->query($sql)){
        //IF THE ROW EXISTS
        if($res->num_rows > 0){
            while($row = $res->fetch_assoc()){

                $query = $row['report_query']; //HERE IS THE SQL QUERY STORED IN TABLE , WHICH IS STORED IN ANOTHER VARIABLE
                $result = $link->query($query); // NOW EXECUTE THE QUERY
                if($result->num_rows > 0){
            ?>
                    <table border="1" cellpadding="10">
                        <tr>
                            <th>First Name</th>
                            <th>Last Name</th>
                        </tr>
            <?php
                    while($rows = $result->fetch_assoc()){
                        //DISPLAY RESULTS HERE
            ?>          
                        <tr>
                            <td><?php echo $rows['first_name'];?></td>
                            <td><?php echo $rows['lastname'];?></td>
                        </tr>
            <?php
                    }
            ?>
                    </table>
        <?php
                }
            }
        }   
    }
    else
    {
        echo $link->error;
    }
?>

`

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!