I have the following SQL Server 2008 table:
CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT, nID INT)
INSERT tbl VALUES
(1, '05:00', '10:00', 1, 1), --will be changed
(2, '08:00', '16:00', 2, 1),
(3, '02:00', '08:00', 1, 1), --will be changed
(4, '07:30', '11:00', 1, 1)
that I use the following SQL to update records that are partially overlapping:
UPDATE tbl
SET dtOut = COALESCE((
SELECT MIN(dtIn)
FROM tbl as t2
WHERE t2.type = tbl.type AND
t2.id <> tbl.id AND
t2.dtIn >= tbl.dtIn AND t2.dtIn < tbl.dtOut
), dtOut)
WHERE nID=1;
SELECT ROWCOUNT_BIG();
With the last SELECT I thought to retrieve the number of records updated, but it doesn't work.
So my question is, how can I rewrite this statement to get the number of records actually updated? (2 in the data table above.)