添加多个输入以使用 'node-mssql' npm 包查询 MS SQL 服务器实例
Adding multiple inputs to query MS SQL Server instance using 'node-mssql' npm package
我想查询 MS SQL 服务器实例来处理一个非常简单的 POST
请求。我正在使用 nodejs + express。以下代码(异步版本)仅考虑第一个 input
类型,因此 returns 错误 -
let pool = await sql.connect(dbCredentials);
let bookName = "string to be matched" ;
let ranksVal= 10
let result = await pool
.request()
.input("item", sql.VarChar, bookName)
.input("ranksVal", sql.Int , ranksVal)
.query(`select top @ranksVal * from dbTable where book = @item order by counts desc`);
理想情况下,上面的代码应该 return 以下 SQL 查询的结果:
select top 10 * from dbTable where book = "string to be matched" order by counts desc
或者,以下解决方案有效:
let result = await pool
.request()
.input("item", sql.VarChar, bookName)
.query(`select top ${ranksVal} * from dbTable where book = @item order by counts desc`);
但我想了解如何将多个值传递给 req.input()
方法。
谢谢。
要在 TOP 值子句中使用表达式或参数,您必须使用括号。例如
.query(`select top (@ranksVal) * from dbTable where book = @item order by counts desc`);
我想查询 MS SQL 服务器实例来处理一个非常简单的 POST
请求。我正在使用 nodejs + express。以下代码(异步版本)仅考虑第一个 input
类型,因此 returns 错误 -
let pool = await sql.connect(dbCredentials);
let bookName = "string to be matched" ;
let ranksVal= 10
let result = await pool
.request()
.input("item", sql.VarChar, bookName)
.input("ranksVal", sql.Int , ranksVal)
.query(`select top @ranksVal * from dbTable where book = @item order by counts desc`);
理想情况下,上面的代码应该 return 以下 SQL 查询的结果:
select top 10 * from dbTable where book = "string to be matched" order by counts desc
或者,以下解决方案有效:
let result = await pool
.request()
.input("item", sql.VarChar, bookName)
.query(`select top ${ranksVal} * from dbTable where book = @item order by counts desc`);
但我想了解如何将多个值传递给 req.input()
方法。
谢谢。
要在 TOP 值子句中使用表达式或参数,您必须使用括号。例如
.query(`select top (@ranksVal) * from dbTable where book = @item order by counts desc`);