-1

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

nbk
  • 45,398
  • 8
  • 30
  • 47

1 Answers1

0

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 ;
nbk
  • 45,398
  • 8
  • 30
  • 47