1

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])
Kentonbmax
  • 938
  • 1
  • 10
  • 16

3 Answers3

8

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})
Kentonbmax
  • 938
  • 1
  • 10
  • 16
  • FYI - I have logged an issue with Sequelize regarding the query for stored procedures returning duplicate result sets. – Kentonbmax Mar 03 '17 at 14:42
  • Also logged an issue where my stored procedure calls are returning duplicate results. Meaning an array of arrays of results or [[results], [results]] – Kentonbmax Mar 06 '17 at 20:16
  • Addition of the QueryTypes.SELECT fixes the duplicate results issue. – Kentonbmax Mar 08 '17 at 20:41
1

You can try

await sequelize.query('CALL spcName(:params )', { replacements: {params : ['value1, value2, value3, value4']} })
Keval
  • 3,246
  • 20
  • 28
0

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;
        }
    }
Kentonbmax
  • 938
  • 1
  • 10
  • 16
Spartans
  • 1
  • 1
  • 4