Shiny
Shiny

Reputation: 1083

How to identify whether the table has identity column

I want to find out whether the table has an identity column or not. Table is unknown to me. I have not done the structure of the table. Using Query?

I am using Sql Server Compact Edition.

Upvotes: 54

Views: 84349

Answers (16)

Mahendra Mulraj Bhatt
Mahendra Mulraj Bhatt

Reputation: 11

One way to do this.

IF NOT EXISTS (SELECT * FROM SYS.COLUMNS WHERE OBJECT_ID = OBJECT_ID('YOURTABLENAME') AND is_identity = 1) BEGIN
    -- your code here
END

Upvotes: 1

Kemal AL GAZZAH
Kemal AL GAZZAH

Reputation: 1037

One way to list all Tables with their identity column if it exists to get you desired table add at the end of the filter "and o.name='TableName'" where Tbale Nam is the table you are looking for

SELECT o.[Name][TableName],i.[name][IdentityColName] FROM 
sys.objects o 
left outer join sys.identity_columns i on i.object_id=o.object_id
where o.type='U'

Upvotes: 0

prashanth kumar
prashanth kumar

Reputation: 91

Very simple answer would be to run this:

SELECT IDENT_CURRENT('TABLE-NAME')

This would give max value of identity column if exists, if the column doesn't exist, it gives 1 as result.

Based on max value, you can identify which column is having that and determine the identity column.

Upvotes: 3

Alexander Kvist
Alexander Kvist

Reputation: 589

If you like me, needed to be able to do this for tables in an arbitrary database, then I found the following solution:

IF EXISTS (
    SELECT 1
    FROM [database name].sys.identity_columns AS id_col
    INNER JOIN [database name].sys.objects
        ON objects.object_id = id_col.object_id
    INNER JOIN [database name].sys.schemas
        ON schemas.schema_id = objects.schema_id
        AND schemas.name = 'schema name'
    WHERE OBJECT_NAME(id_col.object_id, DB_ID('database name')) = 'table name'
) SELECT 1 ELSE SELECT 0

Upvotes: 1

Ashish Thombre
Ashish Thombre

Reputation: 11

select t.name as TableName,c.name as ColumnName
from sys.identity_columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name = 'TableName'

Upvotes: 1

johnj1234
johnj1234

Reputation: 31

... declare @tblhasIdentCol bit = IF (IDENT_CURRENT( @dbName +'.'+ @schemaName +'.'+ @tableName ) IS NOT NULL , 1 , 0 )

You get numeric value if table has identity

Upvotes: 3

user3415785
user3415785

Reputation: 11

CREATE FUNCTION dbo.fnTableHasIdentity(@Tbl sysname)
RETURNS TINYINT
BEGIN 
  RETURN OBJECTPROPERTY(OBJECT_ID(@Tbl), 'TableHasIdentity')
END 

--As simple as that!

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176886

This query returns a table's identity column name:

CREATE PROCEDURE dbo.usp_GetIdentity
@schemaname nvarchar(128) = 'dbo'  
,@tablename nvarchar(128)
AS
BEGIN
    SELECT   OBJECT_NAME(OBJECT_ID) AS TABLENAME, 
             NAME AS COLUMNNAME, 
             SEED_VALUE, 
             INCREMENT_VALUE, 
             LAST_VALUE, 
             IS_NOT_FOR_REPLICATION 
    FROM     SYS.IDENTITY_COLUMNS 
    WHERE OBJECT_NAME(OBJECT_ID) = @tablename
    AND OBJECT_SCHEMA_NAME(object_id) = @schemaname
END

Then form the code side.

Call this stored procedure using the datareader role, then check datareader.hasrows(). If the condition value is true (1), then the table has identity column if set. If not then it doesn't have an identity column.

Upvotes: 32

Sayed M. Idrees
Sayed M. Idrees

Reputation: 1408

you can get the 1 or 0 Boolean Form if the current table has identity Columns by using this

SELECT Count(Column_ID) FROM sys.identity_columns WHERE OBJECT_NAME(object_id) = 'tableName'

Upvotes: 0

h3n
h3n

Reputation: 451

IF (OBJECTPROPERTY(OBJECT_ID('TABLE_NAME'), 'TableHasIdentity') = 1) 

ObjectProperty is available starting sql server 2008 Reference: OBJECTPROPERTY

Upvotes: 38

johnmcp
johnmcp

Reputation: 921

I would just like to add this option as well as I think it is the simplest

SELECT COLUMNPROPERTY(OBJECT_ID('TableName'),'ColumnName','isidentity')

Upvotes: 7

Aravind Goud
Aravind Goud

Reputation: 120

This the query that get u all the tableNames, columnnames of the table, and is_identity or not in the selected database

SELECT
     sys.columns.name
   , sys.tables.name
   , is_identity
FROM sys.columns
INNER JOIN sys.tables ON sys.tables.object_id = sys.columns.object_id
    AND sys.columns.is_identity = 1

Upvotes: 1

Bhavneet
Bhavneet

Reputation: 161

Any of the below queries can be used to check if an Identity Column is present in the table

1)

SELECT *
FROM sys.identity_columns
WHERE OBJECT_NAME(object_id) = 'TableName'

2)

SELECT *
FROM sys.identity_columns
WHERE object_id = (
        SELECT id
        FROM sysobjects
        WHERE name = 'TableName'
    )

Upvotes: 16

Wahid Bitar
Wahid Bitar

Reputation: 14074

I know it's long time ago but i found this helpful

try this :

IF EXISTS (SELECT * from syscolumns where id = Object_ID(@TABLE_NAME) and colstat & 1 = 1)
BEGIN
   -- Do your things
END

Upvotes: 19

Patrick
Patrick

Reputation: 1827

@Pranay: he said Compact Edition. Stored procedures aren't supported, and there is no sys.anything.

This is the call:

SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE AUTOINC_INCREMENT IS NOT NULL AND TABLE_NAME='this_table'

It will return either 1 (true) or 0 (false).

Upvotes: 3

Kyle Rosendo
Kyle Rosendo

Reputation: 25277

One way to do this would be to make use of the stored procedure sp_help. I.e:

sp_help MyTable

This will return a DataSet that has all the information you would need on the table. There is a specific Table that has information on identities.

I.e:

If it does not contain an identity field, the Identity column will say: "No identity column defined".

Upvotes: 4

Related Questions