我试图获得一个字段,如果它不存在,我希望将其插入表中但是出现错误。对于我尝试的第一个查询
CREATE PROCEDURE reg_val( IN pid VARCHAR(30), IN pyd VARCHAR(50), IN dat VARCHAR(20), OUT value VARCHAR(50) )
BEGIN TRANSACTION
DECLARE @id AS VARCHAR(50)
SELECT @id = paymentid FROM table WHERE phoneid=pid
IF @id IS NULL
BEGIN
INSERT INTO user_info (phoneid, paymentid, created_at) VALUES (pid, pyd, dat)
SELECT @id = SCOPE_IDENTITY()
END
SELECT @id
COMMIT TRANSACTION
我收到此错误
您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在#DEC; DECLARE @id AS VARCHAR附近使用正确的语法(50)SELECT @id = paymentid FROM table WHERE phoneid ='在第3行
所以我无法解决这个问题,我尝试了这个新的查询
CREATE PROCEDURE reg_val( IN pid VARCHAR(30), IN pyd VARCHAR(50), IN dat VARCHAR(20), OUT value VARCHAR(50) )
BEGIN
IF EXISTS (SELECT phoneid FROM user_info WHERE phoneide=pid) THEN
SELECT paymentid FROM user_info WHERE phoneid=pid
ELSE
INSERT INTO user_info (phoneid, paymentid, created_at) VALUES (pid, pyd, dat)
SELECT paymentid FROM user_info WHERE phoneid=SCOPE_IDENTITY()
END IF
但我也收到此错误
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,然后使用' ELSE THEN INSERT INTO user_info(phoneid,paymentid,created_at)VAL'在第5行
也许我对形成复杂查询的理解是关闭的,但任何帮助都会受到赞赏。
答案 0 :(得分:0)
请按以下方式更改Stored Procedure
(查看评论中的更改):
DELIMITER $$ // change delimiter
CREATE PROCEDURE reg_val( IN pid VARCHAR(30), IN pyd VARCHAR(50), IN dat VARCHAR(20), OUT VALUE VARCHAR(50) )
BEGIN
IF EXISTS (SELECT phoneid FROM user_info WHERE phoneide=pid) THEN
SELECT paymentid FROM user_info WHERE phoneid=pid; // use semicolon
ELSE
INSERT INTO user_info (phoneid, paymentid, created_at) VALUES (pid, pyd, dat);// use semicolon
SELECT paymentid FROM user_info WHERE phoneid=SCOPE_IDENTITY();// use semicolon
END IF;// use semicolon
END;// use end and semicolon