简体   繁体   English

节点mssql更新查询,获取行数

[英]node mssql update query, get rowcount

I'm using the NodeJS package MSSQL ( https://npmjs.org/package/mssql#cfg-node-tds ) to connect to a MS SQL database and perform UPDATE queries. 我正在使用NodeJS包MSSQL( https://npmjs.org/package/mssql#cfg-node-tds )连接到MS SQL数据库并执行UPDATE查询。

I understand that if an UPDATE query ends up not affecting any rows, it will still return as a success event. 我了解,如果UPDATE查询最终影响任何行,它仍将作为成功事件返回。 I would like to handle the success event differently if zero rows were affected since this is not the intended outcome of the query. 如果影响零行,我想以不同的方式处理成功事件,因为这不是查询的预期结果。

After doing some research, I found that when performing SQL queries, you can use @@ROWCOUNT to get the number of affected rows, but I've yet to figure out how to use this with the MSSQL Node package. 经过研究,我发现执行SQL查询时,可以使用@@ ROWCOUNT来获取受影响的行数,但是我还没有弄清楚如何在MSSQL Node包中使用它。

Has anyone used this Node package and and handled UPDATE queries the way I am trying to? 有没有人使用过这个Node包并以我尝试的方式处理UPDATE查询?

Thanks! 谢谢!

Okay, right from the link your provided, the node package can call stored procedures. 好的,在您提供的链接中,节点包可以调用存储过程。

Either create the logic on the JS side or the TSQL side. 在JS端或TSQL端创建逻辑。

Since I am a DBA/Developer by trade, lets create a SP to perform the update an return the number of rows effected. 由于我是一名DBA /开发人员,因此,让我们创建一个SP来执行更新,并返回执行的行数。

I am using the adventure works sample database. 我正在使用冒险作品示例数据库。

-- use sample db
use AdventureWorks2012;
go

-- sample select
select * 
from [Person].[Person]
where lastname = 'Walters';
go

-- stored procedure
create procedure usp_Update_First_Name (@id int, @first varchar(50))
as
begin
    update [Person].[Person]
    set [FirstName] = @first
    where [BusinessEntityID] = @id;
    return(@@rowcount);
end
go

-- make a call
declare @ret int;
exec @ret = usp_Update_First_Name @id = 4, @first = 'Robert';
print @ret;

This call returns the following output. 该调用返回以下输出。

(1 row(s) affected) 1 (受影响的1行)1

In the JS code, do action A if @ret = 0 or action B if @ret > 0. 在JS代码中,如果@ret = 0,请执行操作A;如果@ret> 0,请执行操作B。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM