dbTalk Databases Forums  

Determine If Table Has Identity Field

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Determine If Table Has Identity Field in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Derek Hart
 
Posts: n/a

Default Determine If Table Has Identity Field - 05-05-2006 , 12:58 PM






Is there a way to read a system table to determine if a specific table has
an identity column?

Derek Hart



Reply With Quote
  #2  
Old   
Tibor Karaszi
 
Posts: n/a

Default Re: Determine If Table Has Identity Field - 05-05-2006 , 01:04 PM






Use the OBJECTPROPERTY() function for this (tip: TableHasIdentity).

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


"Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote

Quote:
Is there a way to read a system table to determine if a specific table has an identity column?

Derek Hart



Reply With Quote
  #3  
Old   
Aaron Bertrand [SQL Server MVP]
 
Posts: n/a

Default Re: Determine If Table Has Identity Field - 05-05-2006 , 01:06 PM



SELECT OBJECTPROPERTY(OBJECT_ID('tablename'), 'TableHasIdentity');

See OBJECTPROPERTY in Books Online for more information.




"Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote

Quote:
Is there a way to read a system table to determine if a specific table has
an identity column?

Derek Hart




Reply With Quote
  #4  
Old   
David Portas
 
Posts: n/a

Default Re: Determine If Table Has Identity Field - 05-05-2006 , 01:07 PM



Derek Hart wrote:
Quote:
Is there a way to read a system table to determine if a specific table has
an identity column?

Derek Hart
Always state what version you are using.

In 2000:
....
EXISTS
(SELECT *
FROM dbo.syscolumns AS C
WHERE id = OBJECT_ID('dbo.tablename')
AND COLUMNPROPERTY(id,name,'IsIdentity')=1) ;

In 2005:
....
EXISTS
(SELECT *
FROM sys.identity_columns
WHERE object_id = OBJECT_ID('dbo.tablename')) ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--



Reply With Quote
  #5  
Old   
Adam Machanic
 
Posts: n/a

Default Re: Determine If Table Has Identity Field - 05-05-2006 , 01:08 PM



In SQL Server 2005:

SELECT 'yes'
WHERE EXISTS
(
SELECT *
FROM sys.columns
WHERE
object_id = object_id('yourtablename')
AND is_identity = 1
)


In SQL Server 2000:


SELECT 'yes'
WHERE EXISTS
(
SELECT *
FROM syscolumns
WHERE
id = object_id('yourtablename')
AND COLUMNPROPERTY(id, name, 'IsIdentity') = 1
)


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--


"Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote

Quote:
Is there a way to read a system table to determine if a specific table has
an identity column?

Derek Hart




Reply With Quote
  #6  
Old   
Adam Machanic
 
Posts: n/a

Default Re: Determine If Table Has Identity Field - 05-05-2006 , 01:12 PM



Nice, now we have many ways to do it:

2000 / 2005:
OBJECTPROPERTY

2000 (and 2005 via the compatability view):
COLUMNPROPERTY over the syscolumns table

2005 only:
Is_Identity from the sys.columns view
sys.Identity_Columns view (that's one I've missed until now -- thanks for
pointing it out, David)


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--


"David Portas" <REMOVE_BEFORE_REPLYING_dportas (AT) acm (DOT) org> wrote

Quote:
Derek Hart wrote:
Is there a way to read a system table to determine if a specific table
has
an identity column?

Derek Hart

Always state what version you are using.

In 2000:
...
EXISTS
(SELECT *
FROM dbo.syscolumns AS C
WHERE id = OBJECT_ID('dbo.tablename')
AND COLUMNPROPERTY(id,name,'IsIdentity')=1) ;

In 2005:
...
EXISTS
(SELECT *
FROM sys.identity_columns
WHERE object_id = OBJECT_ID('dbo.tablename')) ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.