Am I able to write a stored procedure with a parameter, which is the mysql query and the stored procedure returns the column names of the query?
For example I call the procedure:
call selector('select * from users')
And the procedure returns the column names.
It would be easy with information.schema but if I have a more complicated query and alias in it?
Found a solution
CREATE DEFINER=`admin`#`localhost` PROCEDURE `selector`(
IN `sql_query` VARCHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGiN
drop table if exists tmp111;
SET #sql = concat('create table tmp111 as ', sql_query);
PREPARE stmt FROM #sql;
Execute stmt;
describe tmp111;
END
Then call it like:
call selector('select name as n, email as e from users')
Related
I want to have a MySQL procedure, that makes SELECT FROM table and return the result in JSON format via JSON_ARRAYAGG, but the table name to be a parameter.
So there is my solution, but it doesn't work when I call it, because the system thinks the parameter is table name. I will be very thankful if someone can help me.
CREATE DEFINER=`ME` PROCEDURE `main_list`(
IN `the_table` CHAR(50)
)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
SELECT JSON_ARRAYAGG(JSON_OBJECT('id', id,'name', NAME)) from the_table;
END
The call and error:
CALL `main_list`('martin');
/* SQL Error (1146): Table 'my_server.the_table' doesn't exist */
As Akina and gotgn already said you can't use tablename in that way and in some other ise PREPARE Statetemnts for it
DELIMITER //
CREATE PROCEDURE `main_list`(
IN `the_table` CHAR(50)
)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
SET #sql := CONCAT("SELECT JSON_ARRAYAGG(JSON_OBJECT('id', id,'name', NAME)) FROM ",the_table);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
DELIMITER ;
delimiter //
DROP PROCEDURE IF EXISTS drop_table//
create procedure drop_table(in table_name varchar) #创建带参数的存储过程
begin
drop table if EXISTS table_name;
end//
delimiter ;
call drop_table('a')
i use Navicat to create a procedure to drop a parameter table in mysql.
Error:Invalid Stored Procedure Syntax
As commented by P.Salmon, you cannot pass substitute the procedure argument (ie a variable) directly in a sql statement. You need dynamic sql: build the query as a string, then execute it.
delimiter //
create procedure drop_table(in p_table_name varchar(200))
begin
set #q = concat('drop table if exists ', p_table_name);
prepare stmt from #q;
execute stmt;
deallocate prepare stmt;
end//
delimiter ;
Demo on DB Fiddle
Note: the varchar datatype needs a length.
There are two tables
1.FinalTable(f1,f2,f3,f4)
2.DemoTable_1(d1,d2,d3,d4)
I want to insert data from DemoTable_1 to FinalTable using the following SQL query:
INSERT INTO `FinalTable`
(`f1`,`f2`,`f3`,`f4`)
(SELECT `d1`,`d2`,`d3`,`d4`
FROM DemoTable_1);
So I want to create the procedure by passing DemoTable_1 as a parameter for the procedure.
So I can change the source table as DemoTable_2, DemoTable_3, DemoTable_4 and so on..
I am using SQLyog community
DELIMITER //
CREATE OR REPLACE PROCEDURE FinalTable_insert(IN source_table VARCHAR(40))
BEGIN
INSERT INTO `FinalTable`
(`f1`,`f2`,`f3`,`f4`)
(SELECT `d1`,`d2`,`d3`,`d4`
FROM source_table);
END //
DELIMITER ;
CALL FinalTable_insert('DemoTable_1');
Error Code: 1146
Table 'source_table' doesn't exist
The input variables doesn't evaluate as table names, you must build a query in a string and execute that using prepared statement:
DELIMITER //
DROP PROCEDURE IF EXISTS FinalTable_insert //
CREATE PROCEDURE FinalTable_insert(IN source_table VARCHAR(40))
BEGIN
SET #sql = CONCAT("INSERT INTO `FinalTable`
(`f1`,`f2`,`f3`,`f4`)
SELECT `d1`,`d2`,`d3`,`d4`
FROM `",source_table,"`");
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END //
DELIMITER ;
This looks like a pretty rudimentary question but for someone who is new to MySQL, it has proven to be a tough nut to crack.
I've been trying to create a stored procedure in the MySQL and this is what I tried:
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_procedure`(
IN dtst_nm varchar(42)
)
BEGIN
SELECT
COUNT(*)
FROM
data_hub.dtst_nm
;
END
When I run this, I get the error that dtst_nm doesn't exist. The exact error message is:
"Error Code: 1146. Table 'data_hub.dtst_nm' doesn't exist"
Clearly the variable is not getting resolved.
From what I gathered, the syntax seems to be right. What am I missing?
This is a Dynamic SQL problem. You cannot directly specify variables in place of table and column names. You will need to use string functions of create SQL query string. Then use Prepare with Execute to run the query.
Try:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_procedure` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_procedure`(
IN `dtst_nm` varchar(42)
)
BEGIN
SET #s = CONCAT('SELECT COUNT(*) FROM ', dtst_nm );
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
I have a stored procedure in MySQL that calls a function that inserts values into a table.
The insert statement is like
INSERT INTO Table1 SELECT * from Table2
Table2 has a variable name, depending on the date the procedure is called, like
table_201410 , if the procedure was called in October 2014. Soevery month, the procedure should make the select from another table.
I have read some answers about preparing and executing, however as I am new to MySQL/Programming. I am a bit lost.
You can use a prepared statement like this:
DELIMITER $$
DROP PROCEDURE copy_table $$
CREATE PROCEDURE copy_table ()
BEGIN
set #table_name = date_format(now(),'%Y%m'); -- getting year and month
-- concatenating
set #sql = concat('INSERT INTO Table1 SELECT * from table_',#table_name);
-- creating a prepared statement and executing
PREPARE insert_stmt FROM #sql;
EXECUTE insert_stmt;
DEALLOCATE PREPARE insert_stmt;
END$$
DELIMITER ;