April 22, 2004 at 2:39 am
I need to get a list of tables where there is an identity field. I didn't find these information in syscolumns.
Thanks in advance.
April 22, 2004 at 3:06 am
The AUTOVAL column of the syscolumns table is NOT NULL if the column has the identity on. The following query illustrate an exampe. In combination with information in the view INFORMATION_SCHEMA.COLUMNS you can do a lot. Hope this help.
SELECT syscolumns.NAME, syscolumns.AUTOVAL
FROM syscolumns, sysobjects
WHERE syscolumns.ID = sysobjects.ID
and sysobjects.name = 'TABLE_NAME'
April 22, 2004 at 3:32 am
if your identity cols are all primary keys you could use
select CONSTRAINT_CATALOG AS [Database], TABLE_NAME
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'PRIMARY KEY'
which will return the database name and table name
not ideal but it might get you a bit closer
cheers
dbgeezer
April 22, 2004 at 3:53 am
This will do it for a specific database. cheers .
SELECT sysobjects.name as 'Table Name', syscolumns.NAME AS 'Identiy Column Name'
FROM syscolumns, sysobjects
WHERE syscolumns.ID = sysobjects.ID
and sysobjects.name in (select name
FROM sysobjects where xtype = 'U' )
and syscolumns.AUTOVAL IS NOT NULL
April 22, 2004 at 4:55 am
Thanks evebody.
You realy help me.
April 22, 2004 at 5:15 am
For the sake of completeness, you can also take advantage of the mostly overlooked OBJECTPROPERTY stuff like this
SELECT
*
FROM
sysobjects
WHERE
OBJECTPROPERTY(id,'TableHasIdentity')=1
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 23, 2004 at 8:55 am
Some MetaData from INFORMATION_SCHEMA.COLUMNS in case You don't want to select from systables..
/rockmoose
SELECT
ic.ORDINAL_POSITION,
ic.COLUMN_NAME,
ic.DATA_TYPE,
ic.CHARACTER_MAXIMUM_LENGTH,
ic.NUMERIC_PRECISION,
ic.NUMERIC_SCALE,
IS_IDENTITY = COLUMNPROPERTY ( OBJECT_ID( ic.TABLE_CATALOG + '.' + ic.TABLE_SCHEMA + '.' + ic.TABLE_NAME ) , ic.COLUMN_NAME , 'IsIdentity' ),
IS_NULLABLE = CASE UPPER(ic.IS_NULLABLE) WHEN 'YES' THEN 1 ELSE 0 END,
IS_COMPUTED = COLUMNPROPERTY ( OBJECT_ID( ic.TABLE_CATALOG + '.' + ic.TABLE_SCHEMA + '.' + ic.TABLE_NAME ) , ic.COLUMN_NAME , 'IsComputed' ),
COLUMN_DEFAULT = ic.COLUMN_DEFAULT
FROM
INFORMATION_SCHEMA.COLUMNS ic
WHERE
ic.TABLE_SCHEMA = 'dbo'
AND ic.TABLE_NAME = 'MY_Table_Neme'
ORDER BY
ic.ORDINAL_POSITION
You must unlearn what You have learnt
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply