Mysql Stored procedure to create table - mysql

Plzzz Answer
I want to create a table in a stored procedure
But tabel name is stored in a variable.
Example
create procedure ABC
(
#tablename. // is a parameter in which table
name is stored
)
AS
BEGIN
Create table #tablename
(
Colmn one....
Colmn. two...
...............
)
End
but error is shown where
table #tablename
Q ) how can I fix this
I want to set a table name which is stored in a variable #tablename

You can't do that this way, You need prepared statements
DROP procedure IF EXISTS `ABC`;
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `ABC`(
IN _tablename VARCHAR(50) -- is a parameter in which table
-- name is stored
)
BEGIN
SET #sql = CONCAT('
Create table ',_tablename,'
(
Colmn1 INT,
Colmn2 INT
);');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
End$$
DELIMITER ;

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 ;

MySQL stored procedure Error - Create table from param names

I'm trying the following but I'm getting the following error:
ERROR 1054 (42S22): Unknown column 'f' in 'where clause'
I'm seriously confused because f is a parameter of createtableTest...
CREATE PROCEDURE createtableTest
(
tname2 varchar(20),
f varchar(20)
)
BEGIN
DROP TABLE IF EXISTS tname2;
CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like f;
END;
Since f is contains the value, a dynamic sql is needed so we can concatenate it with the original query,
DELIMITER $$
CREATE PROCEDURE createtableTest(IN tname2 varchar(20),IN f varchar(20))
BEGIN
DROP TABLE IF EXISTS tname2;
SET #sql = CONCAT('CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like ''%',f,'%''');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
for example, the value of f is hello, the concatenated string will produce
CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like '%hello%'
UPDATE
MySQL Prepared Statements
Aside from concatenation, you can also parameterized the value which is the best way, ex
DELIMITER $$
CREATE PROCEDURE createtableTest(IN tname2 varchar(20),IN f varchar(20))
BEGIN
DROP TABLE IF EXISTS tname2;
SET #sql = CONCAT('CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like ?');
PREPARE stmt FROM #sql;
SET #val = CONCAT('%', f, '%');
EXECUTE stmt USING #val;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;

MySQL Stored Procedure using IN variable name not value

I have the following stored procedure defined:
DROP PROCEDURE IF EXISTS `Create_Domain_Table`;
DELIMITER ;;
PROCEDURE `Create_Domain_Table`(IN newTable VARCHAR(255))
BEGIN
CREATE TABLE newTable LIKE `domain_template`;
END;;
DELIMITER ;
However when I call the procedure e.g.
CALL Create_Domain_Table('test_domain_table');
it creates a new table with the a name of newtable and not test_domain_table.
Is there something wrong with my syntax or am I referencing the parameter incorrectly?
Thanks
Use Dynamic SQL on this,
DROP PROCEDURE IF EXISTS `Create_Domain_Table`;
DELIMITER ;;
CREATE PROCEDURE `Create_Domain_Table`(IN newTable VARCHAR(255))
BEGIN
SET #sql = CONCAT('CREATE TABLE `', newTable,'` LIKE domain_template');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;;
DELIMITER ;

mysql stored procedure executing SHOW CREATE TABLE

I'm trying to write a stored procedure that wraps around SHOW CREATE TABLE. My final goal is to dynamically look up the schema name from information_schema and run SHOW CREATE TABLE schema.tableName.
It seems that I can't run SHOW CREATE TABLE inside a store procedure.
DELIMITER $$
DROP PROCEDURE IF EXISTS ct$$
CREATE PROCEDURE ct (tableName VARCHAR(50))
BEGIN
SHOW CREATE TABLE tableName;
END$$
DELIMITER ;
mysql> CALL ct('users');
ERROR 1146 (42S02): Table 'adcentraldb.tableName' doesn't exist
mysql>
For those that are interested this is what I end up with for the SHOW CREATE TABLE wrapper that is working
DELIMITER $$
DROP PROCEDURE IF EXISTS ct$$
-- Wraps around SHOW CREATE TABLE. Look at other schemas other than current.
CREATE PROCEDURE ct (tableName VARCHAR(50))
BEGIN
DECLARE dbName VARCHAR(50);
SET dbName = (SELECT `TABLE_SCHEMA` FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE `TABLE_NAME` = tableName LIMIT 1);
SET #a=CONCAT("SHOW CREATE TABLE ", dbName, '.', tableName);
PREPARE stmt1 FROM #a;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;
You can't pass table name like this but you can use prepare statement for this purpose like this-
DELIMITER $$
DROP PROCEDURE IF EXISTS ct$$
CREATE PROCEDURE ct (tableName VARCHAR(50))
BEGIN
set #a=concat("SHOW CREATE TABLE ",tableName);
PREPARE stmt1 FROM #a;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;