mysql表存在于存储过程中的逻辑

时间:2013-02-12 10:25:01

标签: mysql stored-procedures

  DECLARE FoundCount INT;
  DECLARE db_Name VARCHAR(255);

SET @Project_List_val = CONCAT(Project_Number_val, '_List');
SET @db_Name='test_rs';

   SET @sql1 = CONCAT('SELECT COUNT(1) INTO', @FoundCount ,'
    FROM information_schema.tables
    WHERE table_schema =',@db_Name,'
    AND table_name =', @Project_List_val);

PREPARE stmt FROM @sql1;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

    IF FoundCount = 1 THEN
SET @sql = CONCAT(' INSERT INTO test (Panel_Id)
                    SELECT Panel_Id 
                    FROM ', @Project_List_val);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;

此存储过程执行正常。但是当试图运行它时会显示以下错误。

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第1行的“NULL”附近使用正确的语法

如果指定的表(@Project_List_val)存在,我需要运行第二个查询。

2 个答案:

答案 0 :(得分:0)

像这样检查条件@ FoundCount = 1

试试这个

- DECLARE FoundCount INT;

 DECLARE db_Name VARCHAR(255);

SET @Project_List_val = CONCAT(Project_Number_val, '_List');
SET @db_Name='test_rs';

   SET @sql1 = CONCAT('SELECT COUNT(1) INTO ', @FoundCount ,'
    FROM information_schema.tables
    WHERE table_schema = ',@db_Name,'
    AND table_name =', @Project_List_val);

PREPARE stmt FROM @sql1;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

    IF @FoundCount = 1 THEN
SET @sql = CONCAT(' INSERT INTO test (Panel_Id)
                    SELECT Panel_Id 
                    FROM ', @Project_List_val);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;

像这样使用:

SET @sql1 = CONCAT('SELECT COUNT(1) INTO ', @FoundCount ,'
    FROM information_schema.tables
    WHERE table_schema = ',@db_Name,'
    AND table_name =', @Project_List_val);

我在进入.copy后给了空间并打电话给我

答案 1 :(得分:0)

是的,已经解决了。 @db_Name和@Project_List_Val

语句中的那些变量缺少单引号