【问题标题】:Trying to create a stored procedure with 2 inserts [closed]尝试使用 2 个插入创建存储过程 [关闭]
【发布时间】:2012-01-06 15:12:57
【问题描述】:

我对存储过程很陌生。我正在尝试创建一个将记录插入两个不同表的存储过程,但我遇到了问题。

我正在使用 C# 编码,使用 .NET 连接到 MySQL。

我有两张桌子,prop_detailsprop_accountprop_details 表有一个自增列 (id),prop_account 表定义了一个与 prop_details.id 匹配的外键 prop_id

下面的伪代码解释了存储过程应该做什么:

insert form elements into table prop_details
insert relationship of prop_details.id into table prop_account

这是我尝试创建的存储过程

DROP PROCEDURE IF EXISTS `InsertPropertybyClientID`;

CREATE DEFINER = `root`@`localhost` PROCEDURE `InsertPropertybyClientID`(IN 'in_account' int(11), IN `in_title` varchar(100), IN 'in_type' int(2), IN 'in_active' tinyint(1), IN 'in_created' datetime)
BEGIN
INSERT INTO prop_details 
    (prop_title, prop_type, prop_active)
VALUES 
    (in_title, in_type, in_active, in_created);

INSERT INTO prop_account
    (prop_id, acnt_id)
VALUES
    (LAST_INSERT_ID(), in_account);
END;

在尝试保存存储过程时,出现此错误

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 "'in_account' int(11), IN 'in_title' varchar(100), IN 'in_type' int(2), IN 'in_ac' at line 附近使用正确的语法1

我做错了什么?

【问题讨论】:

  • 尝试一下,不要在参数名称周围加上引号。即使用 IN in_account int(11) 而不是 IN 'in_account' int(11)

标签: c# mysql stored-procedures mysql-error-1064


【解决方案1】:

其实错误细节表达了什么问题:

靠近“'in_account' int(11), IN 'in_title' varchar(100), IN 'in_type' int(2), IN 'in_ac' 在第 1 行

identifiers 上的 MySQL 参考文档中指出:

标识符引号字符是反引号(“`”):

如果启用了 ANSI_QUOTES SQL 模式,也允许在双引号内引用标识符:

这意味着除非启用ANSI_QUOTES SQL 模式,否则不应使用单引号或双引号来表示标识符。如果您使用的单词不是 reserved word,您可以使用反引号 (`) 或省略反引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 2023-04-03
    • 2021-04-30
    • 1970-01-01
    • 2022-07-30
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多