将select语句中的值赋给MySQL中的变量

时间:2012-12-07 03:49:11

标签: mysql

我对SQL一般都是新手,对MySQL来说更是如此,我遇到了绊脚石。我试图使用一个过程将一个字段的值复制到另一个字段,如果原始字段不为null,则每当表更新或插入新行时,触发器都会调用此过程。以下是我到目前为止的情况:

-- WORK_NOTES_PROCEDURE - This copies the contents of the estimate notes to the work   order notes if the original estimate had any notes with it.
DROP PROCEDURE IF EXISTS 'WORK_NOTES_PROCEDURE';
DELIMITER $$

CREATE PROCEDURE WORK_NOTES_PROCEDURE()
BEGIN
DECLARE var_temp VARCHAR(50);

SET var_temp := (SELECT ESTIMATE_NOTES FROM ESTIMATES WHERE ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER);

IF var_temp IS NOT NULL THEN

UPDATE WORK_ORDERS SET WORK_ORDER_NOTES = var_temp WHERE WORK_ORDERS.ESTIMATE NUMBER = ESTIMATES.ESTIMATE_NUMBER;

END IF; 
END$$

DELIMITER ;

绝对可以理解任何帮助,我得到的错误是我为var_temp赋值的行的语法错误。

1 个答案:

答案 0 :(得分:1)

试,

SET var_temp = (SELECT ESTIMATE_NOTES 
                FROM ESTIMATES INNER JOIN WORK_ORDERS
                     ON ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER
                LIMIT 1);