0

更新を含む手順を作成しました。との 2 つの変数がotherCategoryIdありotherCategoryIdます。

コマンドラインで手順を実行しようとすると、次のようなエラーが表示されます。

SQL 構文にエラーがあります。near 'update... を使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

更新ステートメントは次のようになります。

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 ##
4

1 に答える 1

1

そこにa がありませんTHEN

MySQL 手動 IF ステートメント。

これを変える

  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.
于 2012-08-20T09:21:36.577 に答える