0
  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) 存在,我需要运行第二个查询。

4

2 回答 2

0

是的,解决了。@db_Name 和 @Project_List_Val 语句中的这些变量缺少单引号

于 2013-02-13T04:32:25.587 回答
0

像这样检查条件 @FoundCount=1 像这样

尝试这个

-- 声明 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);

我已经给了空间。复制这个然后给我打电话

于 2013-02-12T10:33:19.097 回答