带有过程中变量的mysql update语句

时间:2012-08-20 08:57:00

标签: mysql variables stored-procedures sql-update

我创建了一个包含更新的过程。有两个变量otherCategoryIdotherCategoryId

当我尝试在命令行上运行该过程时,它给出了一个错误,如:

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'update ...

附近使用正确的语法

更新声明如下:

abstract_type set parent_fk = otherCategoryId  where id = otherCategoryId;

更新:我的程序源代码是:

CREATE PROCEDURE fixCommodityIPRTypes(IN typeClass VARCHAR(255), IN commodityClass VARCHAR(255),IN parrentFk VARCHAR(255))
BEGIN

    DECLARE otherCategoryId BIGINT;
    DECLARE wrongNoOfAccesses INT; 
    DECLARE wrongTypeId INT; 
    DECLARE loop_count INT  DEFAULT 1;
    DECLARE num_rows INT; 
    DECLARE nameOfType VARCHAR(1255);

    select otherCategoryId = pt.id from abstract_type pt  where pt.class = typeClass and pt.name = 'Other' and  pt.normalized = 1 and pt.normalize_link_fk is null;

    -- declare cursor for all other - specify types  with parent fk specified

    block_cursor: BEGIN
    DECLARE changeOSTypesCursor CURSOR FOR SELECT x.id, x.name, x.no_of_accesses from abstract_type as x  where class = typeClass and normalized = 0 and parent_fk = parrentFk;

    open changeOSTypesCursor;
    select FOUND_ROWS() into num_rows;

    update_loop:LOOP
         fetch  changeOSTypesCursor into wrongTypeId, nameOfType, wrongNoOfAccesses;
      -- for each distinct other-specify and parent the category called

      inside_loop: BEGIN
      DECLARE nrOfAccess BIGINT;
      DECLARE otherId BIGINT;
      select NO_OF_ACCESSES , Id from  abstract_type where class = typeClass and normalized = 0 and parent_fk = otherCategoryId and name = nameOfType into nrOfAccess, otherId ;    
      -- if there are no types with the same text as the current os type with parent category 'OTHER'
      -- then we just change the parent to be the category 'OTHER' 
      if nrOfAccess =0
        update abstract_type set parent_fk = otherCategoryId  where id = wrongTypeId;
      -- else we must set the no_of_accesses for the type with parent 'OTHER' = 
      -- current no_of_acceses + accesses from the type with specified parent,
      -- replace the foreign_key in commodities with the type with parent_other and 
      -- delete 
      else if nrOfAccess > 0 then
      begin
          update abstract_type set no_of_accesses = wrongNoOfAccesses + nrOfAccess where id = otherId;
          update commodity set goods_type_fk = otherId where class = commodityClass and goods_type_fk = wrongTypeId;
          delete from abstract_type where id = wrongTypeId;
      end   
      if num_rows <= loop_count then
         leave update_loop;
      end if
      end inside_loop;
    end LOOP;

    close changeOSTypesCursor;
    END block_cursor;
      -- update trademarks with parent specified and delete  the category of level 1
    update type_xref set ref_parents_fk = otherCategoryId  where  ref_parents_fk = parrentFk;
    delete from abstract_type where id = parrentFk;
END ##

1 个答案:

答案 0 :(得分:1)

你错过了THEN

MySQL manual IF statement.

更改此

  if nrOfAccess =0
    update abstract_type set parent_fk = otherCategoryId  where ...

  if nrOfAccess =0 THEN
    update abstract_type set parent_fk = otherCategoryId  where ...

<强>更新

CREATE PROCEDURE fixCommodityIPRTypes(IN typeClass VARCHAR(255), IN commodityClass VARCHAR(255),IN parrentFk VARCHAR(255))
BEGIN

    DECLARE otherCategoryId BIGINT;
    DECLARE wrongNoOfAccesses INT; 
    DECLARE wrongTypeId INT; 
    DECLARE loop_count INT  DEFAULT 1;
    DECLARE num_rows INT; 
    DECLARE nameOfType VARCHAR(1255);

    select otherCategoryId := pt.id from abstract_type pt  where pt.class = typeClass and pt.name = 'Other' and  pt.normalized = 1 and pt.normalize_link_fk is null;
    --here you want to use the assignment operator := rather than is_equal operator =

    block_cursor: BEGIN
    DECLARE changeOSTypesCursor CURSOR FOR SELECT x.id, x.name, x.no_of_accesses from abstract_type as x  where class = typeClass and normalized = 0 and parent_fk = parrentFk;

    open changeOSTypesCursor;
    select FOUND_ROWS() into num_rows;
    --FOUND_ROWS() will return a random number or zero or NULL here, since you need to do a query with SELECT SQL_CALC_FOUND_ROWS col1, col2 FROM ... first.

    --before you fetch anything from cursor, you want to declare a continue handler when there are no more rows found.
    --have a look at this example here: http://dev.mysql.com/doc/refman/5.0/en/cursors.html

         fetch  changeOSTypesCursor into wrongTypeId, nameOfType, wrongNoOfAccesses;

      DECLARE nrOfAccess BIGINT;
      DECLARE otherId BIGINT;
      select NO_OF_ACCESSES , Id from  abstract_type where class = typeClass and normalized = 0 and parent_fk = otherCategoryId and name = nameOfType into nrOfAccess, otherId ;    

      if nrOfAccess =0 THEN
        update abstract_type set parent_fk = otherCategoryId  where id = wrongTypeId;
      else if nrOfAccess > 0 then
      begin
          update abstract_type set no_of_accesses = wrongNoOfAccesses + nrOfAccess where id = otherId;
          update commodity set goods_type_fk = otherId where class = commodityClass and goods_type_fk = wrongTypeId;
          delete from abstract_type where id = wrongTypeId;
      end IF;
      --you were missing an IF here. Please read proper syntax in the manual before posting questions. I'll leave the rest to you. We want to solve real problems here, you know? Not waste our time with syntax errors.