从firebird到mysql的存储过程移植

yftpprvb  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(369)

这个问题在这里已经有答案了

将int转换为varchar(8个答案)
两年前关门了。
我是mysql新手,我正在尝试将一个sql存储过程代码从firebird移植到mysql。我尽了最大努力,但仍在concat语句行上生成错误。
代码如下:

CREATE PROCEDURE BUILD_MATRIX(IN RECURSIVECOLLECTION VarChar(1), collectionID Integer)
BEGIN
    declare collectionsort varchar(255);
    declare zotero_itemid integer;
    declare itemcount integer;
    declare countlinks integer;
    declare totals varchar(150);
    declare acollectionid integer;
    declare acollectionsort varchar(255);
    declare azotero_itemid integer;
    declare amodalityid integer;
    declare atagid integer;
    declare aname varchar(255);
    declare bcollectionid integer;
    declare bcollectionsort varchar(255);
    declare bzotero_itemid integer;
    declare bmodalityid integer;
    declare btagid integer;
    declare bname varchar(255);

    IF (RecursiveCollection = '1') THEN               /* Recursive Collections Enabled  */
        SET collectionSort = CONCAT('[',CAST(collectionID AS varchar(255)) , ']');
        WHILE
            Select zotero_itemID, count(zotero_itemID) As ItemCount 
            From factors 
            WHERE POSITION(collectionSort IN :collectionSort) <> 0 GROUP BY zotero_itemID
            INTO :zotero_itemID, :ItemCount
        DO
            IF (ItemCount > 1) THEN
                WHILE
                    Select a.collectionID as acollectionID, a.collectionSort as acollectionSort, a.zotero_itemID as azotero_itemID, a.modalityID as amodalityID, a.tagID as atagID, a.name as aname,
                    b.collectionID as bcollectionID, b.collectionSort as bcollectionSort, b.zotero_itemID as bzotero_itemID, b.modalityID as bmodalityID, b.tagID as btagID, b.name as bname
                    From factors a
                    JOIN factors b On a.modalityID <= b.modalityID
                    WHERE (a.modalityID > 0 AND b.modalityID > 0) AND (a.tagID <> b.tagID) AND (POSITION(a.collectionSort IN :collectionSort) <> 0) And (a.zotero_itemID = :zotero_itemID) And (POSITION(b.collectionSort IN :collectionSort) <> 0) And (b.zotero_itemID = :zotero_itemID)
                    ORDER BY a.modalityID
                    INTO :ACOLLECTIONID, :ACOLLECTIONSORT, :AZOTERO_ITEMID, :amodalityID, :atagID, :ANAME, :BCOLLECTIONID, :BCOLLECTIONSORT, :BZOTERO_ITEMID, :bmodalityID, :btagID, :BNAME
                DO
                    /* Verify if there are links with the same normative-determinative modalities and factors to avoid duplications  */
                    SELECT count(linkID) as countLinks
                    FROM matrix 
                    WHERE collectionID = :collectionID And recursiveCollection = :recursiveCollection AND normativeModality = :amodalityID AND normativeFactorID = :atagID And determinativeModality = :bmodalityID AND determinativeFactorID = :btagID
                    INTO :countLinks;

                    IF (countLinks > 0) THEN
                        /* Set synchronized = True only. To avoid duplications */
                        SELECT totals
                        FROM get_total_items(:atagid, :btagID, :recursivecollection, :collectionID)
                        INTO totals;

                        UPDATE matrix
                        SET totals = :totals, synchronized = '1' 
                        WHERE collectionID = :collectionID AND recursiveCollection = :recursiveCollection AND normativeModality = :amodalityID AND normativeFactorID = :atagID And determinativeModality = :bmodalityID AND determinativeFactorID = :btagID;
                    ELSE
                        /* Add only new links and ignore similar combinations like A-B and B-A if the table Matrix already has one */
                        SELECT count(linkID) as countLinks 
                        FROM matrix 
                        WHERE collectionID = :collectionID AND recursiveCollection = :recursiveCollection AND normativeModality = :amodalityID AND normativeFactorID = :btagID And determinativeModality = :bmodalityID AND determinativeFactorID = :atagID
                        INTO :countLinks;

                        IF (countLinks = 0) THEN
                            SELECT totals
                            FROM get_total_items(:atagid, :btagID, :recursivecollection, :collectionID)
                            INTO totals;

                            INSERT INTO matrix (collectionID,normativeModality,normativeFactorID,determinativeModality,determinativeFactorID, totals, synchronized, recursiveCollection) 
                            VALUES (:collectionID,:amodalityID,:atagID,:bmodalityID,:btagID, :totals, '1', :recursiveCollection);
                        END IF;   
                    END IF;
                END WHILE;
            END IF;
        END WHILE;
    ELSE IF (RecursiveCollection = '0') THEN          /* One Collection Only */ 
        WHILE 
            Select zotero_itemID, count(zotero_itemID) As ItemCount 
            From factors 
            WHERE collectionID = :collectionID GROUP BY zotero_itemID
            INTO :zotero_itemID, :ItemCount
        DO
            IF (ItemCount > 1) THEN
                WHILE
                    Select a.collectionID as acollectionID, a.collectionSort as acollectionSort, a.zotero_itemID as azotero_itemID, a.modalityID as amodalityID, a.tagID as atagID, a.name as aname,
                    b.collectionID as bcollectionID, b.collectionSort as bcollectionSort, b.zotero_itemID as bzotero_itemID, b.modalityID as bmodalityID, b.tagID as btagID, b.name as bname
                    From factors a
                    JOIN factors b On a.modalityID <= b.modalityID
                    WHERE (a.modalityID > 0 AND b.modalityID > 0) AND (a.tagID <> b.tagID) AND (a.collectionID = :collectionID) And (a.zotero_itemID = :zotero_ItemID) And (b.collectionID = :collectionID) And (b.zotero_itemID = :zotero_ItemID)
                    ORDER BY a.modalityID
                    INTO :ACOLLECTIONID, :ACOLLECTIONSORT, :AZOTERO_ITEMID, :amodalityID, :atagID, :ANAME, :BCOLLECTIONID, :BCOLLECTIONSORT, :BZOTERO_ITEMID, :bmodalityID, :btagID, :BNAME
                DO
                    /* Verify if there are links with the same normative-determinative modalities and factors to avoid duplications  */
                    SELECT count(linkID) as countLinks
                    FROM matrix 
                    WHERE collectionID = :collectionID And recursiveCollection = :recursiveCollection AND normativeModality = :amodalityID AND normativeFactorID = :atagID And determinativeModality = :bmodalityID AND determinativeFactorID = :btagID
                    INTO :countLinks;

                    IF (countLinks > 0) THEN
                        /* Set synchronized = True only. To avoid duplications */
                        SELECT totals
                        FROM get_total_items(:atagid, :btagID, :recursivecollection, :collectionID)
                        INTO totals;

                        UPDATE matrix
                        SET totals = :totals, synchronized = '1'
                        WHERE collectionID = :collectionID AND recursiveCollection = :recursiveCollection AND normativeModality = :amodalityID AND normativeFactorID = :atagID And determinativeModality = :bmodalityID AND determinativeFactorID = :btagID;
                    ELSE
                        /* Add only new links and ignore similar combinations like A-B and B-A if the table Matrix already has one */
                        SELECT count(linkID) as countLinks 
                        FROM matrix 
                        WHERE collectionID = :collectionID AND recursiveCollection = :recursiveCollection AND normativeModality = :amodalityID AND normativeFactorID = :btagID And determinativeModality = :bmodalityID AND determinativeFactorID = :atagID
                        INTO :countLinks;

                        IF (countLinks = 0) THEN
                            SELECT totals
                            FROM get_total_items(:atagid, :btagID, :recursivecollection, :collectionID)
                            INTO totals;

                            INSERT INTO matrix (collectionID,normativeModality,normativeFactorID,determinativeModality,determinativeFactorID, totals, synchronized, recursiveCollection) 
                            VALUES (:collectionID,:amodalityID,:atagID,:bmodalityID,:btagID, :totals, '1', :recursiveCollection);
                        END IF;
                    END IF;
                END WHILE;
            END IF;
        END WHILE;
    END IF;

    /* Delete all rows that where not updated. It means that all rows with synchronized fields set 0 aren't present at Zotero anymore. */
    DELETE FROM matrix
    WHERE collectionID = :collectionID AND recursiveCollection = :recursiveCollection AND synchronized = '0';

    /* Set all rows updated and added to synchronized = 0 */
    UPDATE matrix
    SET synchronized = '0'
    WHERE collectionID = :collectionID AND recursiveCollection = :recursiveCollection AND synchronized = '1';
    SUSPEND;
END

当我尝试运行时,收到以下消息:
错误代码:1064。sql语法有错误;请查看与您的mysql服务器版本对应的手册,以了解使用near'('[',cast(collectionid as varchar(255)),'])作为collectionsort的正确语法;在22号线的时候
你能帮帮我吗?

相关问题