我是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服务器版本相对应的手册 在'('[',CAST(collectionID AS varchar(255)),']')AS附近使用的语法 collectionSort;当在第22行时
可以帮我吗?
答案 0 :(得分:1)
删除CAST。只需使用collection_id。