mysql在未插入的存储过程中插入语句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql在未插入的存储过程中插入语句相关的知识,希望对你有一定的参考价值。

我创建了一个存储过程,它应该执行以下操作:插入Agencies表,在PK索引行中创建一个id将id保存在一个变量中将id和其他数据插入Users表

以下语法不会触发任何错误:

DELIMITER $$

CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN

INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (@aName, @numTrav, @polType);

SET @agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());

INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES 
(@agencyID, @uEmail, @uFName, @uLName, @uTitle);

END

但是,存储过程在执行时不会将我的参数插入表中。因此,在搜索之后我尝试像这样创建SP(包括“END”之后的$$并将分隔符重置为分号):

DELIMITER $$

CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN

INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (@aName, @numTrav, @polType);

SET @agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());

INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES 
(@agencyID, @uEmail, @uFName, @uLName, @uTitle);

END $$
DELIMITER ;

mysql创建存储过程但是给出了以下错误:

1064 - 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 'DELIMITER' at line 1

当我执行创建的存储过程时,它仍然不会在我的参数中插入数据。

任何帮助,将不胜感激。

答案

存储过程中的一些问题:

DELIMITER $$

CREATE PROCEDURE `insertNewAgencyAndAdmin` (
  /*    
  IN `aName` varchar (100),
  IN `numTrav` int,
  IN `polType` int,
  IN `uEmail` varchar(255),
  IN `uFName` varchar(40),
  IN `uLName` varchar(40),
  IN `uTitle` varchar(100)
  */
  IN `_aName` varchar (100),
  IN `_numTrav` int,
  IN `_polType` int,
  IN `_uEmail` varchar(255),
  IN `_uFName` varchar(40),
  IN `_uLName` varchar(40),
  IN `_uTitle` varchar(100)
)
BEGIN
  /*  
  INSERT INTO `btsAgency`.`Agencies` (
    `agencyName`,
    `numTrav`,
    `polType`
  ) VALUES (
    @`aName`,
    @`numTrav`,
    @`polType`
  );
  */    
  INSERT INTO `btsAgency`.`Agencies` (
    `agencyName`,
    `numTrav`,
    `polType`
  ) VALUES (
    `_aName`,
    `_numTrav`,
    `_polType`
  );    
  /*
  SET @`agencyID` = (SELECT `agencyID` from `btsAgency`.`Agencies` where `agencyID` = LAST_INSERT_ID());

  INSERT INTO `btsUsers`.`Users` (
    `userAgencyID`,
    `userEmail`,
    `userFirstName`,
    `userLastName`,
    `userTitle`
  ) VALUES (
    @`agencyID`,
    @`uEmail`,
    @`uFName`,
    @`uLName`,
    @`uTitle`
  );
  */    
  INSERT INTO `btsUsers`.`Users` (
    `userAgencyID`,
    `userEmail`,
    `userFirstName`,
    `userLastName`,
    `userTitle`
  ) VALUES (
    LAST_INSERT_ID(),
    `_uEmail`,
    `_uFName`,
    `_uLName`,
    `_uTitle`);
END$$

DELIMITER ;
另一答案

SELECT * FROM discounts $$ delimiter $$; CREATE PROCEDURE insertData(IN id int,IN title varchar(255),IN amount decimal)BEGIN

插入折扣价值(id,title,amount);结束; $$ call insertData(3,“sadasd”,56);

/ * DROP PROCEDURE insertData * /

以上是关于mysql在未插入的存储过程中插入语句的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL批量插入语句怎么写

利用mysql存储过程循环插入新数据并更新

带游标的Mysql存储过程

从后面的 C# 代码调用存储过程时插入语句不起作用

mysql 创建存储过程插入测试数据

mysql存储过程中 乱码问题解决办法