mysql中的存储过程语法

时间:2012-06-12 07:39:13

标签: mysql sql stored-procedures syntax

我是编写存储过程的新手,我似乎无法在此过程中找到错误。 错误标记在where部分附近。

我试着寻找一个基于if条件完成集合的例子,但我似乎找不到这样的例子。有人能指出我的错误吗?

DELIMITER $$

CREATE PROCEDURE `incubation`.`bt_voice_modification` 
    (in input_slot varchar(45),in input_port varchar(45))
BEGIN
    SET @SVLAN_STH:=1000;
    SET @SVLAN_DTH:=999;
    SET @CVLAN_DTH:=1000;
    SET @FLOW_INSTANCE:=1;

    UPDATE one_2_one_table
        SET  L2S_USER_FLOW_INSTANCE = @FLOW_INSTANCE := @FLOW_INSTANCE+1;
        SET  L2S_NW_SLOT = input_slot;
        SET  L2S_NW_PORT = input_port;
        IF STH_DTH = 'STH' then
            set  L2S_NW_SVLAN = @SVLAN_STH :=@SVLAN_STH + 1;
        ELSE 
            set  L2S_NW_SVLAN = @SVLAN_DTH ;
            set  L2S_NW_CVLAN = @CVLAN_DTH :=@CVLAN_DTH + 1;
        END if;
        WHERE IPDSLAM_USER_SLOT = 2 AND L2S_USER_TYPE like "%gplt%";

END

1 个答案:

答案 0 :(得分:1)

您的代码建议您不清楚UPDATE语句的工作原理或确切语法。当您放置;时,它标记(UPDATE)语句的结束。语法是 - 对于一个表:

UPDATE tableX

  SET columnA = what_value_should_columnA-get ,
      columnB = what_value_should_columnB_get ,
      ...
      columnX = what_value_should_columnX_get 

WHERE (conditions that restrict the rows that will be affected)

ORDER BY SomeColumn ;   --- this can be used in MySQL only. Standard SQL 
                        --- (and most DBMS) do not allow ORDER BY clause in
                        --- UPDATE statements. Since you are using variables
                        --- and the order of updating affects the updated values,
                        --- it's essential that you include an ordering.

所以,你的UPDATE会是这样的:

UPDATE one_2_one_table
    SET  L2S_USER_FLOW_INSTANCE = @FLOW_INSTANCE := @FLOW_INSTANCE+1 ,
         L2S_NW_SLOT = input_slot,
         L2S_NW_PORT = input_port,
         L2S_NW_SVLAN = CASE WHEN STH_DTH = 'STH'
                               THEN @SVLAN_STH := @SVLAN_STH + 1
                               ELSE @SVLAN_DTH 
                        END ,
         L2S_NW_CVLAN = CASE WHEN STH_DTH = 'STH'
                               THEN L2S_NW_CVLAN
                               ELSE @CVLAN_DTH := @CVLAN_DTH + 1
                        END

WHERE IPDSLAM_USER_SLOT = 2 
  AND L2S_USER_TYPE like '%gplt%' 

ORDER BY SomeColumn ;