C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to find whether a database column is identity or not?


Posted Date: 22 Apr 2007    Resource Type: Code Snippets    Category: SQL

Posted By: Vadivel Mohanakrishnan       Member Level: Diamond
Rating:     Points: 10



I have explained couple of methods to find whether a database column is an identity column or not.

Sample Table structure:



Create a sample table with an identity column in it.

Create table [order_details]
(
OrderId int identity,
OrderName varchar(10),
UnitPrice int
)



Method 1: [Easiest way]



Select ColumnProperty(Object_id('order_details'), 'OrderId', 'IsIdentity')



Method 2:

For some reasons if you don't want the above method!! then try this one



Declare @colName varchar(100)
Declare @RetColName varchar(100)

Set @colName = 'OrderId' -- Specify the column name for which you want to check

--Status column = 128 means its an identity column
Select @RetColName=[name] from syscolumns where (status & 128) = 128 and id=(select id
from sysobjects where name='order_details')

If @colName = @RetColName
Print 'Its Identity'
Else
Print 'Not an identity column'





Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Compatibility level of database
Previous Resource: Sort records in Database Views
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use