如何从第一个过程MySQL填充第二个过程变量

时间:2016-02-07 17:04:24

标签: mysql stored-procedures

您好我无法弄清楚如何将值放在第一个过程顶部的连接的第二个过程中。我用“CALL Procedure2('value','value')测试了它”并且它工作得非常好,只是坚持让它现在可用。谢谢任何帮助都会很棒。    我不介意不把它包括在这里的第二个程序,但是这产生了如何将Left JOINS放在if else语句中的问题。如果我能让这个工作变得很棒。

CREATE DEFINER=`root`@`localhost` PROCEDURE `Procedure1`()
BEGIN

DECLARE Proc1ValueA INT;
DECLARE Proc1ValueB VarChar(40);

UPDATE table4
LEFT JOIN `table1` ON `table1`.`var1` = `table4`.`var1`
LEFT JOIN `table2` ON `table2`.`var2` = `table4`.`var2`
LEFT JOIN `table3` ON `table3`.`var3` = `table4`.`var3`

SET 
table1.var6 = table3.var8,
table2.var2 = table1.var2,
table3.var1 = table2.var5;

SET table1.var2 = @Proc1ValueA;
SET table2.var3 = @Proc1ValueB;

CALL Procedure2(@Proc1ValueA,@Proc1ValueB);
END

编辑: 以下是我尝试在procedure2中实现的Joins结构的一些表:

Table 1
+----+----------+--------+
| ID | Table2FK | ValueA |
+----+----------+--------+
|  1 |       12 | var5   |
+----+----------+--------+

Table 2
+----+----------+--------+
| ID | Table3FK | ValueB |
+----+----------+--------+
| 12 |       8  | var2   |
+----+----------+--------+

Table 3 - ValueC is a unique index
+----+---------+
| ID | ValueC |
+----+---------+
|  8 | var3    |
+----+---------+

Table 4 (source of data)
+----+--------+--------+
| ID | ValueD | ValueE |
+----+--------+--------+
|  1 | var4   | var5   |
+----+--------+--------+


CREATE PROCEDURE Procedure2(in vartable1 INT(12), vartable4 INT(8),    
vartable3 INT, vartable2 INT)
Begin

IF EXISTS (select table3.ID from table3 where table3.ValueC = vartable4) 
THEN
    UPDATE table2 SET table2.table3FK = (select table3.ID from table3 
where table3.ValueC = vartable4)  WHERE table2.ID = vartable2;      
END IF;

IF EXISTS (select table3.ID from table3 where table3.ValueC = vartable4) 
THEN
    DELETE FROM table3 WHERE table3.ID = vartable3;
ELSE 
    UPDATE table3 SET table3.ValueC = vartable4 WHERE table3.ID = 
vartable3;
END IF;
END$$

如果我需要,我会在开头添加一些变量。

0 个答案:

没有答案