I just found a cool new feature in Sql Server 2005: the OUTPUT statement: for example, the following query

INSERT INTO Transport(transportTypeCode, departureCodeWay, arrivalCodeWay, departureCodeBack, arrivalCodeBack)
OUTPUT INSERTED.transportId
VALUES ('vl', 'BRU', 'LAX', 'LAX', 'BRU')

returns:

|transportID|
|1|

So you no longer have to add a

SELECT @@SCOPE_IDENTITY

to retrieve the id of the last inserted record (in the current scope)!

Advertisements