与nodejs的mysql存储过程问题

时间:2016-03-14 15:24:26

标签: mysql node.js stored-procedures

我遇到了MySQL程序的一个奇怪问题。如果我从NodeJS调用以下程序,那么它的运行绝对正常。

BEGIN
    INSERT INTO `tblProcessCCDs`(`CCDName`, `JSON`, `fldDIRECTEmailAttachmentID`) 
    VALUES (CCDName, JSON, fldDIRECTEmailAttachmentID);
END

然而,如果我将其更新为以下版本,那么它只会插入第一条记录。

BEGIN
DECLARE ProcessCCDsID INT;
SET ProcessCCDsID = (SELECT ID FROM tblProcessCCDs WHERE `fldDIRECTEmailAttachmentID` = fldDIRECTEmailAttachmentID LIMIT 1);

IF(ProcessCCDsID = 0 OR ProcessCCDsID IS NULL) THEN BEGIN
    INSERT INTO `tblProcessCCDs`(`CCDName`, `JSON`, `fldDIRECTEmailAttachmentID`) 
    VALUES (CCDName, JSON, fldDIRECTEmailAttachmentID);
END; END IF;

END

NodeJS代码:

var argv = require('minimist')(process.argv.slice(2));
var bb = require("blue-button");
var fs = require("fs");
var async = require("async");
var mysql = require("mysql2");
xml2js = require('xml2js');
const util = require('util');
var parser = new xml2js.Parser({explicitArray : false});

var ROW_XMLPath = "/disk2/Clinical/TCM/DIRECT/Testing/DOC00001.XML" ;
var ROW_EAD = "77" ;

if ( typeof ROW_XMLPath !== 'undefined' && ROW_XMLPath ) {
    var EmailAttachmentID = ROW_EAD;
    console.log("Email Attachment ID: "+EmailAttachmentID);

    var CCDFilePath = ROW_XMLPath;
    var CCDData = fs.readFileSync(CCDFilePath, 'utf-8');
    var CCDDoc = bb.parse(CCDData);

    async.series([
        //Load user to get userId first
        function(callback) { //ProcessCCDs
            var connection = mysql.createConnection({user: 'mf287',database: 'DIRECTTCM',host: 'localhost',password: 'mfTest-HITEC_307',multipleStatements: true});
            connection.connect();
            console.log("connection Connected");
            var testvar = "Sample Value";
            var qryInsertProcessCCDs = 'CALL `spINSERT_tblProcessCCDs`(?,?,?);';
            connection.query(qryInsertProcessCCDs, [CCDFilePath,testvar,EmailAttachmentID], function (err, rows, fields) {
                if (!err) {
                    console.log("   Inserted into Process");
                    callback();
                } else{
                    console.log('Error while performing Query. ',err);
                    callback(err);
                }
            });
            connection.end();
        }], function(err) { //This function gets called after the two tasks have called their "task callbacks"
        if(err) return(err);
        console.log("\n\nPATID: ", PATDEMOGRAPHICID,"\n\n");
    });
}

我一直试图解决这个问题。但是,到目前为止没有运气。在这里寻求帮助。感谢。

0 个答案:

没有答案