尽管返回ID为

时间:2015-08-09 11:31:38

标签: mysql stored-procedures

我是mysql存储过程的新手。我一直试图通过我在下面创建的商店程序插入数据。该过程确实返回@USER_ID。但是,该过程不会将数据插入表中。

//存储过程

DELIMITER $$
CREATE PROCEDURE USP_USR_PROFILE_InsProfile(
IN FIRST_NAME varchar(50),
IN LAST_NAME varchar(50),
IN EMAIL_ADDRESS varchar(50),
IN PASSWORD varchar(256),
IN DOB date,
IN GENDER varchar(10),
IN TIMEZONE varchar(100),
IN NATIONALITY_ID int(11),
IN RESIDENCE_CTRY_ID int(11),
IN ADDRESS_LINE1 varchar(200),
IN ADDRESS_LINE2 varchar(200),
IN ADDRESS_LINE3 varchar(200),
IN PO_BOX varchar(50),
IN CITY_TOWN varchar(50),
IN POSTCODE_ZIP varchar(50),
IN STATE_PROVINCE_REGION VARCHAR(50), 
IN DESCRIPTION varchar(500),
IN PROFILE_IMAGE_NAME varchar(50),
IN PROFILE_IMAGE_LOCATION varchar(100),
IN LAST_UPDATED_USER int(11),
OUT USER_ID int(11))
BEGIN

--table with only auto increate col
INSERT INTO SEQ_USR_PROFILE_USER_ID(ID) VALUES (NULL);

SELECT @USER_ID := MAX(ID) FROM SEQ_USR_PROFILE_USER_ID;

    INSERT INTO TBL_USR_PROFILE (
        USER_ID,
        FIRST_NAME,
        LAST_NAME,
        EMAIL_ADDRESS,
        PASSWORD,
        DOB,
        GENDER,
        TIMEZONE,
        NATIONALITY_ID,
        RESIDENCE_CTRY_ID,
        ADDRESS_LINE1,
        ADDRESS_LINE2,
        ADDRESS_LINE3,
        PO_BOX,
        CITY_TOWN,
        STATE_PROVINCE_REGION,
        DESCRIPTION,
        CREATED_DATE,
        PROFILE_IMAGE_NAME,
        PROFILE_IMAGE_LOCATION,
        STATUS_ID,
        LAST_UPDATED_DATE,
        LAST_UPDATED_USER)
    VALUES (
        @USER_ID,
        @FIRST_NAME,
        @LAST_NAME,
        @EMAIL_ADDRESS,
        @PASSWORD,
        @DOB,
        @GENDER,
        @TIMEZONE,
        @NATIONALITY_ID,
        @RESIDENCE_CTRY_ID,
        @ADDRESS_LINE1,
        @ADDRESS_LINE2,
        @ADDRESS_LINE3,
        @PO_BOX,
        @CITY_TOWN,
        @STATE_PROVINCE_REGION,
        @DESCRIPTION,
        CURRENT_TIMESTAMP,
        @PROFILE_IMAGE_NAME,
        @PROFILE_IMAGE_LOCATION,
        1, -- STATUS ACTIVE
        CURRENT_TIMESTAMP,
        @LAST_UPDATED_USER)

END $$

DELIMITER ;

//执行

CALL `USP_USR_PROFILE_InsProfile`('System Administrator', '', 'test@test.com', '1234567', '2015-08-09', 'MALE', 'Universal Time', '1', '1','','','','','','','','','','',0, @USER_ID);

//表创建

DROP TABLE IF EXISTS `TBL_USR_PROFILE`;

CREATE TABLE `TBL_USR_PROFILE` (
  `RUNNING_ID` int(11) NOT NULL AUTO_INCREMENT,
  `USER_ID` int(11) NOT NULL,
  `FIRST_NAME` varchar(50) NOT NULL,
  `LAST_NAME` varchar(50) NOT NULL,
  `EMAIL_ADDRESS` varchar(50) NOT NULL,
  `PASSWORD` varchar(256) NOT NULL,
  `DOB` date NOT NULL,
  `GENDER` varchar(10) NOT NULL,
  `TIMEZONE` varchar(100) NOT NULL,
  `NATIONALITY_ID` int(11) NOT NULL,
  `RESIDENCE_CTRY_ID` int(11) NOT NULL,
  `ADDRESS_LINE1` varchar(200) DEFAULT NULL,
  `ADDRESS_LINE2` varchar(200) DEFAULT NULL,
  `ADDRESS_LINE3` varchar(200) DEFAULT NULL,
  `PO_BOX` varchar(50) DEFAULT NULL,
  `CITY_TOWN` varchar(50) DEFAULT NULL,
  `POSTCODE_ZIP` varchar(50) DEFAULT NULL,
  `STATE_PROVINCE_REGION` varchar(50) DEFAULT NULL,
  `DESCRIPTION` varchar(500) DEFAULT NULL,
  `CREATED_DATE` datetime NOT NULL,
  `PROFILE_IMAGE_NAME` varchar(50) DEFAULT NULL,
  `PROFILE_IMAGE_LOCATION` varchar(100) DEFAULT NULL,
  `STATUS_ID` int(11) NOT NULL,
  `LAST_UPDATED_DATE` datetime NOT NULL,
  `LAST_UPDATED_USER` int(11) NOT NULL,
  PRIMARY KEY (`USER_ID`),
  KEY `RUNNING_ID` (`RUNNING_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

感谢任何人可以提供帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

首先:你所做的事情本质上是错误的。

  • INSERT INTO SEQ_USR_PROFILE_USER_ID(ID) VALUES (NULL);SELECT @USER_ID := MAX(ID) FROM SEQ_USR_PROFILE_USER_ID;之间存在竞争条件,最终可能会使用wromng用户ID。
  • 使用SELECT LAST_INSERT_ID();可以避免
  • 使用交易也可以避免

现在代码:第一眼就是CURRENT_TIMESTAMP是一个函数,所以你需要写CURRENT_TIMESTAMP()

我还建议删除序列表并直接在用户表上使用AUTO_INCREMENT,并通过SELECT LAST_INSERT_ID();轻松返回ID

答案 1 :(得分:0)

SELECT @USER_ID := MAX(ID) FROM SEQ_USR_PROFILE

是你的第一线吗?无论insert语句的返回(甚至是否存在),您的函数都将返回该函数