Discussion in 'Getting Started' started by rv62, Jun 29, 2007.
i need to find last count value while inserting new record in the table
Which "count"? If the table has an identity column, it increases automatically.
If you need to use the identity value for the next step after the insertion, you can use the SCOPE_IDENTITY function to see the identity value for the row that was just inserted. Check Books Online for further options - there are two other functions related to SCOPE_IDENTITY, but you have to be aware of the differences.
And if you're inserting multiple rows, you should consider using set-based logic to retrieve the new identity values (lots of programmers take a step-by-step approach, instead of using SQL properly).
And if you need to know the exact number of rows currently in a table:
DECLARE @count INT
SELECT @count = COUNT(*) FROM mytable
If you want to make sure no other process can insert rows while you're working on it, add a transaction.
Separate names with a comma.