How to access the output parameter in node mssql?

纵饮孤独 提交于 2019-12-11 13:53:20

问题


`  request.input('xyz',sql.Int,1);
    request.input('abc',sql.Numeric,2);
    request.output('pqr',sql.Int);
    request.output('def',sql.Char);
      request.execute('[StoredProcedure]',function(err,recordsets,returnvalue){
      next.ifError(err);
      console.log(returnvalue);
    })`

Now the problem is how will i access the output parameters.

Regards.


回答1:


request.parameters.pqr.value and request.parameters.def.value

Got the answer , so sharing it :)




回答2:


Actually the method described above did not work for me using "mssql": "^4.3.0".

I had to use the result object returned by the promise and extract the parameters from the output object. Something like:

res.output.

Here is the sample code which worked for me:

poolPromise
        .then(conn => conn.request())
        .then(request => {
            const forecastName = forecastDefinition.name;
            // Input parameters
            request.input('forecastName', sql.VarChar(50), forecastName);
            ...
            // Output parameters
            request.output('actualYear', sql.Int);
            request.output('actualMonth', sql.Int);
            ...
            return request.execute('[dbo].[' + generateForecastProcedure + ']')
        })
        .then((res, err) => {
            if (err) {
                console.log(`${generateForecastProcedure} error: `, err);
            } else {
                console.log(`${generateForecastProcedure} success: `, res);
                // Callback to deal with further processing.
                const output = (res.output || {});
                const actualYear = output.actualYear;
                const actualMonth = output.actualMonth;
                callback(forecastDefinition, actualYear, actualMonth);
            }
        })
        .catch(err => {
            console.log(`${generateForecastProcedure} exception: `, err);
        })



回答3:


After analysing 1 hour, I found this. Hope it will be useful to someone

Instead of this request.parameters.SQLReturn you can use recordsets.output.SQLReturn

request.output("SQLReturn");

            request.execute('sample', function (err, recordsets, returnValue, results) {    


                    res.json({ code: 1, message: recordsets.output.SQLReturn });
                }


来源:https://stackoverflow.com/questions/28669183/how-to-access-the-output-parameter-in-node-mssql

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