Technological Guide.

Fetch the incremented Identity value from MS-SQL Server

Wednesday, June 10th, 2009

The Microsoft SQL Server Database provides an Identity value which is generated automatically when a record is added in such tables which contains IDENTITY. We can fetch the auto incremented value from MS-SQL Server database to our applications using built in function @@IDENTITY( ) (used in MS-Access) or SCOPE_IDENTITY( ) which is the recommended method for obtaining identity value on SQL Server. Scope_Identity( ) retrieves the last identity value created in the current scope which is a single module that can be a stored procedure, batch, function or trigger. We can implement the coding to retrieve the Scope_Identity in many ways.
1. Using batch command with two sql queries seperated with a semicolor, with ExecuteScalar( ) against the batch command to return single value.

2. We can use output parameter from a stored procedure and ExecuteNonQuery( ).
Stored Procedure declaration

Our Application Coding…

Show all tables having identity columns

Monday, May 18th, 2009

The following query will be very much helpful when your project manager asks you to write a query to show all the tables in a database (SQL Server 2000/2005/2008) which are having identity columns. Its very simple and straight forward.

Query for SQL Server 2005 / 2008

Results

Query for SQL Server 2000