CREATE PROCEDURE ins(in @codes VARCHAR(100),in @param1 VARCHAR(100),in @param2 VARCHAR(100)) BEGIN set @tableName = ''; set @tableAttri = ''; SELECT TABLE_NAME into @tableName from iplat62.twzss31 where REC_ID=@codes and @codes BETWEEN 42 AND 10000; SELECT TABLE_ATTRI into @tableAttri from iplat62.twzss31 where REC_ID=@codes and @codes BETWEEN 42 AND 10000; set @sql=CONCAT('INSERT INTO ',@tableName,' (',@tableAttri,', TIME_STAMP) VALUES (?, ?)'); PREPARE stmt FROM @sql; EXECUTE stmt USING @param1, @param2; DEALLOCATE PREPARE stmt; END 是否有语法错误
时间: 2024-03-21 16:42:02 浏览: 63
这段代码中存在语法错误。在定义存储过程中指定参数时,应该使用 `IN` 关键字,而不是 `in`。因此,应该将以下代码:
```
CREATE PROCEDURE ins(in @codes VARCHAR(100),in @param1 VARCHAR(100),in @param2 VARCHAR(100))
```
改为:
```
CREATE PROCEDURE ins(IN codes VARCHAR(100), IN param1 VARCHAR(100), IN param2 VARCHAR(100))
```
另外,在动态生成 SQL 语句时,需要在表名和列名之间添加空格,否则会导致 SQL 语法错误。可以将这一行代码:
```
set @sql=CONCAT('INSERT INTO ',@tableName,' (',@tableAttri,', TIME_STAMP) VALUES (?, ?)');
```
改为:
```
set @sql=CONCAT('INSERT INTO ', @tableName, ' (', @tableAttri, ', TIME_STAMP) VALUES (?, ?)');
```
相关问题
CREATE TRIGGER `insertTo` AFTER INSERT ON iplat62.data11 FOR EACH ROW BEGIN CALL ins(new._NUMERICID,NEW._VALUE,NEW._TIMESTAMP) END CREATE PROCEDURE ins(in @codes VARCHAR,in @param1 VARCHAR,in @param2 VARCHAR) BEGIN set @tableName = ''; set @tableAttri = ''; SELECT TABLE_NAME into @tableName from iplat62.twzss31 where REC_ID=@codes and @codes BETWEEN 42 AND 10000; SELECT TABLE_ATTRI into @tableAttri from iplat62.twzss31 where REC_ID=@codes and @codes BETWEEN 42 AND 10000; set @sql=CONCAT('INSERT into',@tableName,'(',@tableAttri,'TIME_STAMP) VALUES (?, ?)'); PREPARE stmt FROM @sql; EXECUTE stmt USING @param1, @param2; DEALLOCATE PREPARE stmt; END
这是一个 MySQL 数据库的触发器和存储过程,用于在 iplat62 数据库的 data11 表插入数据后,自动调用 ins 存储过程插入数据到相应的表中。其中,ins 存储过程中,根据传入的参数 codes(REC_ID)、param1(_VALUE)和param2(_TIMESTAMP),动态生成 SQL 语句,并通过 PREPARE 和 EXECUTE 语句执行动态生成的 SQL 语句插入数据到相应的表中。
阅读全文