问题
I'm using ADO in a JScript (Microsoft JavaScript dialect) Windows Scripting Host script to update a SQL Server table. I'd like to get the number of rows affected by the update in the script, but JavaScript doesn't have pass-by-reference and so I can't do the usual thing where I receive the records affected from the Command#Execute function's RecordsAffected argument. So I'm looking for the best way to get that info.
For reasons not related directly to this query, I want to avoid using a stored procedure for this although I realize that that would work (I'd just return @@rowcount
out of the SP). I'm trying to find a reliable but simple non-SP means of doing it.
I looked around and found this syntax for the statement:
UPDATE MyTable
SET MyColumn = (blah blah blah)
WHERE (blah blah blah) ;
SELECT @@rowcount as 'RowsAffected'
...which sends me back a one-row ResultSet
containing the count. That does seem to work, and in my limited testing seems to work correctly (I don't get the wrong count when other operations are also happening, etc.), but it seems...kludgy for some reason.
Is that the best way to do it, given the perhaps-unreasonable constraints I've listed? Cross-platform solutions are not required (welcome, though, as always), it can be Microsoft SQL Server-specific (2005+).
Thanks in advance.
回答1:
Not sure why you think it's kludgy. Nothing wrong with this approach.
来源:https://stackoverflow.com/questions/2216630/how-can-i-get-the-of-rows-affected-by-a-statement-using-ado-with-javascript