MySql - Create Table If Not Exists Else Truncate?

前端 未结 6 1635
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 00:23

Here is the updated question:

the current query is doing something like:

$sql1 = \"TRUNCATE TABLE fubar\";
$sql2 = \"CREATE TEMP         


        
6条回答
  •  隐瞒了意图╮
    2020-12-31 00:42

    execute any query if table exists.

    Usage: call Edit_table(database-name,table-name,query-string);

    • Procedure will check for existence of table-name under database-name and will execute query-string if it exists. Following is the stored procedure:
    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `Edit_table` $$
    CREATE PROCEDURE `Edit_table` (in_db_nm varchar(20), in_tbl_nm varchar(20), in_your_query varchar(200))
    DETERMINISTIC
    BEGIN
    
    DECLARE var_table_count INT;
    
    select count(*) INTO @var_table_count from information_schema.TABLES where TABLE_NAME=in_tbl_nm and TABLE_SCHEMA=in_db_nm;
    IF (@var_table_count > 0) THEN
      SET @in_your_query = in_your_query;
      #SELECT @in_your_query;
      PREPARE my_query FROM @in_your_query;
      EXECUTE my_query;
    
    ELSE
      select "Table Not Found";
    END IF;
    
    END $$
    DELIMITER ;
    

    More on Mysql

提交回复
热议问题