Sequelize sytax for calling stored procedure with input parameters using MSSQL

杀马特。学长 韩版系。学妹 提交于 2019-12-08 03:12:31

问题


I'm getting an error for the code below and cannot find accurate information for the syntax to call a MSSQL store procedure through sequelize. I have also tried the syntx from the other posts on stackoverflow similar to CALL spcName(param1, ...)

error:

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

for code:

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

回答1:


Code looks like this and works for MSSQL. Wanted to show date example because of formatting:

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

Following the question example:

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



回答2:


You can try

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



回答3:


Use : to indicate parameter and replacements to map values. See example below.

   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;
        }
    }


来源:https://stackoverflow.com/questions/42538525/sequelize-sytax-for-calling-stored-procedure-with-input-parameters-using-mssql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!