Home All Groups Group Topic Archive Search About

Need to query whether a column is identity

Author
10 Nov 2005 10:56 PM
Juan Dent
Hi,

I am desperately seeking for a query that I may issue against SQL
Server2000- that will tell me whether a column is identiy or not!!

Please help!!
--
Thanks in advance,

Juan Dent, M.Sc.

Author
10 Nov 2005 11:23 PM
Jeff A. Stucker
This might get you on the right path:

http://www.windowsitpro.com/Article/ArticleID/16091/16091.html

--
Cheers,

'('     Jeff A. Stucker
\

Senior Consultant
www.rapidigm.com
Show quote
"Juan Dent" <Juan_Dent@nospam.nospam> wrote in message
news:C71F6DBC-A3BB-4FD2-82B1-1C351AFBE556@microsoft.com...
> Hi,
>
> I am desperately seeking for a query that I may issue against SQL
> Server2000- that will tell me whether a column is identiy or not!!
>
> Please help!!
> --
> Thanks in advance,
>
> Juan Dent, M.Sc.
Author
11 Nov 2005 12:07 AM
ML
It's not really all that complicated: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_0mpl.asp

E.g.
use Northwind
go

select columnproperty(object_id('dbo.Categories'), 'CategoryID', 'IsIdentity')
go


ML
Author
11 Nov 2005 4:58 AM
Vadivel
I know of two ways of doing this. Let me try and explain it ...

-- Sample Table structure
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 and id=(select id
from sysobjects where name='order_details')

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

Best Regards
Vadivel

http://vadivel.blogspot.com
http://thinkingms.com/vadivel

Show quote
"Juan Dent" wrote:

> Hi,
>
> I am desperately seeking for a query that I may issue against SQL
> Server2000- that will tell me whether a column is identiy or not!!
>
> Please help!!
> --
> Thanks in advance,
>
> Juan Dent, M.Sc.
Author
11 Nov 2005 8:32 AM
ML
> --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 and id=(select id
> from sysobjects where name='order_details')

This may not work. The status column holds a bitmap and should be queried
appropriately:

where (status & 128 = 128)


ML

AddThis Social Bookmark Button