使用MSSQL使用输入参数调用存储过程的sequelize sytax

时间:2017-03-01 17:44:34

标签: node.js sequelize.js

我收到以下代码的错误,无法找到通过sequelize调用MSSQL存储过程的语法的准确信息。我也尝试过stackoverflow上的其他帖子,类似于CALL spcName(param1,...)

错误:

(node:14580) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SequelizeDatabaseError: Incorrect syntax near '@param1'.

代码:

await sequelize.query('CALL spcName(@param1, @param2, @param3, @param4);', [value1, value2, value3, value4])

3 个答案:

答案 0 :(得分:4)

代码看起来像这样,适用于MSSQL。由于格式化,希望显示日期示例:

return await sequelize.query('scpTest :inDate', {replacements: {inDate: moment().subtract('d', 4).toISOString(), type: sequelize.QueryTypes.SELECT}

在问题示例之后:

await sequelize.query('spcName :param1, :param2, :param3, :param4)', {replacements: {param1: value, param2: value, param3: value, param4: value}, type: sequelize.QueryTypes.SELECT})

答案 1 :(得分:1)

你可以尝试

await sequelize.query('CALL spcName(:params )', { replacements: {params : ['value1, value2, value3, value4']} })

答案 2 :(得分:0)

使用:表示参数,使用replacements来映射值。见下面的例子。

   async getPlayers(UserId: number, requestObject: any): Promise<any> {
        this.sequelize = Server.sequelize;
        try {
            let o_rowcnt = 0;
            let playersList = await this.sequelize.query('CALL sp_discovery (:p_user, :p_game, :p_platform, :p_headphone,:p_start,:p_end , :p_stanceId , @o_rowcnt )',
                {
                    replacements: { 
                        p_user: UserId, 
                        p_game: requestObject.gameid,
                        p_platform: requestObject.platformid,
                        p_headphone:  requestObject.headphone,
                        p_start: requestObject.index,
                        p_end: requestObject.limit,
                        p_stanceId: requestObject.stanceid 
                    }
                }
            );
            return playersList;
        } catch (e) {
            return e;
        }
    }