Running a stored procedure with NodeJS and MSSQL package error

前端 未结 2 698
抹茶落季
抹茶落季 2020-12-05 12:29

Im trying to get the MSSQL nodejs package to return the results of a stored procedure from Microsoft SQL server using the code below. However the error I get is...



        
相关标签:
2条回答
  • 2020-12-05 12:34

    I think the line's

    req.input('ProductEntryID', req.Int, 3299);
    req.input('LoginEntryID', req.Int, 4);
    req.input('TempLoginEntryId', req.Int, -1);
    req.input('AddToWishList', req.Bit, 0);
    req.input('WebPortalId', req.Int, 0);
    

    which has req.input thats wrong it seems.

    Please try this code

    var sql = require('mssql');
    
    var config = {
        user: 'sa',
        password: '---',
        server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
        database: 'Test'
    }
    
    var getCities = function() {
      var conn = new sql.Connection(config);
      conn.connect().then(function(conn) {
        var request = new sql.Request(conn);
        request.input('City', sql.VarChar(30), 'Cbe');
        request.input('NameNew', sql.VarChar(30), 'Cbe');
        request.execute('spTest').then(function(err, recordsets, returnValue, affected) {
          console.dir(recordsets);
          console.dir(err);
        }).catch(function(err) {
          console.log(err);
        });
      });
    }
    
    getCities();
    

    I tried this myself and its giving the results.

    0 讨论(0)
  • 2020-12-05 13:00

    I don't know if this would be helpful or not but this is how I did

    let executeQuery = async (value, country) => {
        try {
            let pool = await sql.connect(dbConfig);
            let results = await pool.request()
                .input('input_parameter', sql.Int, value)
                .input('Country', sql.VarChar(50), country)
                // .output('output_parameter', sql.VarChar(50))
                .execute('procedure_name')
    
            console.dir(results);
    
        } catch (err) {
            res.json({
                "error": true,
                "message": "Error executing query"
            })
        }
    }
    
    executeQuery(value, country);
    

    I've used async and await method to make it more readable.

    0 讨论(0)
提交回复
热议问题