Node.js 在Nodejs中使用async Wait运行SQL Server存储过程

Node.js 在Nodejs中使用async Wait运行SQL Server存储过程,node.js,sql-server,node-mssql,Node.js,Sql Server,Node Mssql,我不熟悉将SQL Server与nodejs一起使用。我正在使用节点mssql库。我发现存储过程有点挑战性 我的存储过程是: CREATE PROCEDURE [dbo].[sp_addStudent] @StudentID int, @StudentName varchar(50), @TimeStamp datetime, @Notes varchar(MAX) AS BEGIN INSERT INTO Students (Student

我不熟悉将SQL Server与nodejs一起使用。我正在使用
节点mssql
库。我发现存储过程有点挑战性

我的存储过程是:

CREATE PROCEDURE [dbo].[sp_addStudent] 
    @StudentID int, 
    @StudentName varchar(50), 
    @TimeStamp datetime, 
    @Notes varchar(MAX) 
AS 
BEGIN 
    INSERT INTO Students (StudentID , StudentName , [TimeStamp], Notes) 
    VALUES (@StudentID , @StudentName , @TimeStamp, @Notes); 
END
我找到了一种使用try-catch在没有参数的情况下在线执行此存储过程的方法:

async function getDataFromProcedure(dbConfig, procedureName) {
  try {
    await sql.connect(dbConfig);
    const request = new sql.Request();
    recordsets = await request.execute(procedureName);
    return recordsets[0];
  } catch (error) {
    // handle error here
  }
};
如何使用try-catch向存储过程中添加参数?

快速查看一下如何添加输入/输出参数。例如:

const request = new sql.Request();
request.input('input_parameter', sql.Int, value);
request.output('output_parameter', sql.Int);
const result = await request.execute(procedureName);
这将包括一个名为
value
的变量,作为名为
input\u parameter
的输入参数,并设置一个名为
output\u parameter
的预期输出参数(这里假设它们都已在存储过程中定义)。稍后可以在
结果中观察到输出参数。输出

侧注:不应在存储过程中使用
sp_u
前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用
sp.
并使用其他东西作为前缀,或者根本不使用前缀!