存储过程在更新之前检查值

时间:2015-03-06 07:51:18

标签: mysql sql

我正在尝试创建一个存储过程,如果有任何更改,则检查IN值,如果有继续更新,否则不要更新。

 CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`(
 IN id_ INT,
 IN data1_ VARCHAR(45),
 IN data2_ VARCHAR(45)
 )
 BEGIN
 DECLARE data1check varchar(45);
 DECLARE data2check varchar(45);

 SELECT data1, data2 FROM main AS B;
 SET data1check = B.data1;
 SET data2check = B.data2;

 IF (data1check <> data1_ OR data2check <> data2_)

 THEN

    UPDATE main SET 
    version = version + 1,
    data1 = COALESCE(data1_, data1), 
    data2 = COALESCE(data2_, data2) 
    WHERE id = id_;

 END IF;
 END

我用这个查询试了一下:

CALL new_procedure(1,'Violet','Green')

但我明白了:

Error Code: 1109 Unknown table 'B' in field list

1 个答案:

答案 0 :(得分:0)

你试过用过吗?

SET data1check = main.data1;
SET data2check = main.data2;

而不是

SET data1check = B.data1;
SET data2check = B.data2;