views:

32

answers:

2

Hey

Is there a way to get the number of records by an update-statement before executing the actual update?

I know you can just create a select count(1) statement, but my process needs something more generic.

+1  A: 

Duplicate question?

http://stackoverflow.com/questions/2560737/count-number-of-rows-to-be-affected-before-update-in-trigger

Matt Roberts
That's postgres sql not sql server. But I guess the idea of adding an INSTEAD OF trigger could still work and would be generic.
Martin Smith
A: 

Do you mean something like this? So you can use the Count in the Update statement itself?

WITH p 
AS
(
SELECT *,
COUNT(*) OVER() AS C
FROM tbl
WHERE id IN (1,7,8,9)
)
UPDATE P SET Col = C
Martin Smith