Create dynamic stored procedure in mysql with table name only - mysql

I am trying to create a dynamic stored procedure for table name only, I mean when I call the stored procedure with table name as parameter it should be display all details of given table name.
I am using this code:-
CREATE PROCEDURE `test1`(IN tab_name VARCHAR(40))
BEGIN
SET #t1 =CONCAT("SELECT * FROM '",tab_name,"' ");
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END
It display error, and the error is:-
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
Any one can help me.

Back-ticks surround table name.
DROP PROCEDURE IF EXISTS `test1`;
DELIMITER $$
CREATE PROCEDURE `test1`(IN tab_name VARCHAR(40))
BEGIN
SET #t1 =CONCAT("SELECT * FROM `",tab_name,"`"); -- back-ticks around tbl name
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END$$
DELIMITER ;
or (PHPMyAdmin or those not requiring delimiters):
DROP PROCEDURE IF EXISTS `test1`;
CREATE PROCEDURE `test1`(IN tab_name VARCHAR(40))
BEGIN
SET #t1 =CONCAT("SELECT * FROM `",tab_name,"`"); -- back-ticks around tbl name
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END
Hopefully you are going to do something different with this stored proc because it seems trivial and will be slower than just calling the select.

Related

Can i create a procedure or fuction to drop a parameter table in mysql?

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.

How to create stored procedure for insert data from one table to other table by passing parameter

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 ;

How to Use Multiple Parameters in a MySQL *Prepared* Stored Procedure

Although there are some good examples of multiple parameters being used in MySQL stored procedures, I have been unable to find a simple example that shows how to use them in a stored procedure that is prepared.
The code below returns 'Incorrect arguments to EXECUTE' when calling it using: `call test_parms('my report','example.com');
I've tried with and without '#' in front of the parameter names (just gives an unknown column error), and different variations of the code . What am I doing wrong?
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_parms`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_parms`(REPORT VARCHAR(255),DOMAIN_NAME VARCHAR(255))
BEGIN
SET #sql = "Select #DOMAIN_NAME,#REPORT";
set #REPORT=REPORT;
set #DOMAIN_NAME=DOMAIN_NAME;
PREPARE stmt FROM #sql;
EXECUTE stmt using #DOMAIN_NAME,#REPORT;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
The following section of the documentation will be helpful: 13.5.1. PREPARE Syntax.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_parms`$$
CREATE PROCEDURE `test_parms`(`REPORT` VARCHAR(255), `DOMAIN_NAME` VARCHAR(255))
BEGIN
SET #`sql` := 'SELECT ? `DOMAIN_NAME`, ? `REPORT`';
SET #`REPORT` := `REPORT`;
SET #`DOMAIN_NAME` := `DOMAIN_NAME`;
PREPARE `stmt` FROM #`sql`;
EXECUTE `stmt` USING #`DOMAIN_NAME`, #`REPORT`;
DEALLOCATE PREPARE `stmt`;
END$$
DELIMITER ;
SQL Fiddle demo
UPDATE
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_parms`$$
CREATE PROCEDURE `test_parms`(`REPORT` VARCHAR(255), `DOMAIN_NAME` VARCHAR(255))
BEGIN
SELECT `DOMAIN_NAME` `DOMAIN_NAME`, `REPORT` `REPORT`;
END$$
DELIMITER ;
SQL Fiddle demo
Looks like I was unnecessarily setting user defined variables prior to execution, which was included in some of the stored procedure examples, but apparently doesn't work if the stored procedure is prepared.
To fix, Replace the following code:
set #REPORT=REPORT;
set #DOMAIN_NAME=DOMAIN_NAME;
PREPARE stmt FROM #sql;
EXECUTE stmt using #DOMAIN_NAME,#REPORT;
with this :
PREPARE stmt FROM #sql;
EXECUTE stmt;
So the corrected code looks like:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_parms`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_parms`(REPORT VARCHAR(255),DOMAIN_NAME VARCHAR(255))
BEGIN
SET #sql = "Select #DOMAIN_NAME,#REPORT";
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
Update
I ended up with the following which works with any user with privileges to execute procedures ('process' permission is not required like it is for the previous code) and works correctly on SQL Fiddle:
DROP PROCEDURE IF EXISTS `test_parms`//
CREATE PROCEDURE `test_parms`(REPORT VARCHAR(255),DOMAIN_NAME VARCHAR(255))
BEGIN
SET #sql = "Select #DOMAIN_NAME,#REPORT";
SET #DOMAIN_NAME=DOMAIN_NAME;
SET #REPORT=REPORT;
PREPARE stmt FROM #sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT ;
END//
SQL Fiddle - Updated Final

MySQL stored procedures - How to use parameter in FROM clause

I have the following stored procedure:
DELIMITER ///
CREATE PROCEDURE tmp_test_proc(__TABLE__NAME varchar(255))
BEGIN
SELECT COUNT(*) FROM __TABLE__NAME;
END///
DELIMITER ;
I would like to select from the parameter __TABLE__NAME, but MySQL tells me there is no table __TABLE__NAME... So is there a way to use the value of the parameter in the from clause?
you cannot parameterized table names (as well as column names) by default, you need to create PreparedStatement for this,
DELIMITER ///
CREATE PROCEDURE tmp_test_proc(__TABLE__NAME varchar(255))
BEGIN
SET #sql = CONCAT('SELECT COUNT(*) FROM ', __TABLE__NAME);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END///
DELIMITER ;

Mysql stored procedure

What I want to do is,create a table in mysql by passing the table name as a parameter in the stored procedure.I'm using following code for stored procedure in mysql.
DELIMITER //
CREATE PROCEDURE createtable(IN tablename varchar(20))
BEGIN
SET #s=CONCAT('CREATE TABLE', tablename, '(month varchar(20))');
PREPARE stmt FROM #s;
EXECUTE stmt;
END //
and when i call it
CALL createtable('account');
I get the following error
You have an error in your SQL syntax; check the MySQL server version for the right syntax to us...
I don't know where I'm wrong..
You forgot the spaces before and after your table name. Try
DELIMITER //
CREATE PROCEDURE createtable(IN tablename varchar(20))
BEGIN
SET #s=CONCAT('CREATE TABLE ', tablename, ' (month varchar(20))');
PREPARE stmt FROM #s;
EXECUTE stmt;
END //