我创建了一个包含更新的过程。有两个变量otherCategoryId
和otherCategoryId
。
当我尝试在命令行上运行该过程时,它给了我一个错误,例如:
您的 SQL 语法有错误;检查与您的 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 ##