我在mysql
中创建存储过程时出错delimiter //
CREATE PROCEDURE insertvaluefield(IN p_field INT)
BEGIN
DECLARE v_type VARCHAR(80) DEFAULT '';
SELECT type_field
FROM Field
WHERE id_field = p_field
LIMIT 1;
IF v_type IN ('text', 'textarea') THEN
DELETE FROM ValueField
WHERE field_valuefield = p_field;
END IF;
END;
//
delimiter ;
我这样做是为了在我的数据库中保存这个程序:
mysql -uXXXX -pXXXX DataBase < script.sql
这是结果错误:
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE PROCEDURE insertvaluefield(IN p_field INT) BEGIN DECLARE v_ty' at line 1
答案 0 :(得分:0)
从终端执行SQL脚本的正确语法是:
mysql -uXXXX -pXXXX -e "use your_db_name; source path_to_sql_file/script.sql;"
检查输出,它显示在测试DB中:
mysql> show procedure status;
+------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+------- ---------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+------+------------------+-----------+----------------+-------------------- -+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| test | insertvaluefield | PROCEDURE | root@localhost | 2015-11-19 21:18:34 | 2015-11-19 21:18:34 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
答案 1 :(得分:0)
我无法重现这个问题。在我的测试用例中,一切都按预期工作。
请务必将type_field
分配给v_type
:
...
-- SELECT `type_field`
SELECT `type_field` INTO `v_type`
...
档案:/path/to/file/script.sql
USE `test`;
DELIMITER //
SELECT DATABASE()//
DROP TABLE IF EXISTS `Field`//
DROP PROCEDURE IF EXISTS `insertvaluefield`//
CREATE TABLE `Field` (
`id_field` INT,
`type_field` VARCHAR(80)
)//
CREATE PROCEDURE `insertvaluefield`(IN `p_field` INT)
BEGIN
DECLARE `v_type` VARCHAR(80) DEFAULT '';
-- SELECT `type_field`
SELECT `type_field` INTO `v_type`
FROM `Field`
WHERE `id_field` = `p_field`
LIMIT 1;
IF `v_type` IN ('text', 'textarea') THEN
/*
DELETE FROM ValueField
WHERE field_valuefield = p_field;
*/
/* TEST CASE */
SELECT CONCAT('
DELETE FROM `ValueField`
WHERE `field_valuefield` = ', `p_field`, ';
') `DELETE`;
END IF;
END//
INSERT INTO `Field`
(`id_field`, `type_field`)
VALUES
(1, 'text'),
(2, 'textarea')//
DELIMITER ;
CALL `insertvaluefield`(1)\G
CALL `insertvaluefield`(2)\G
测试:
$ mysql -u XXXX -p < /path/to/file/script.sql
Enter password:
DATABASE()
test
*************************** 1. row ***************************
DELETE:
DELETE FROM `ValueField`
WHERE `field_valuefield` = 1;
*************************** 1. row ***************************
DELETE:
DELETE FROM `ValueField`
WHERE `field_valuefield` = 2;