Question

I am iterating through a list of database columns using this code:

adapter = New SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" & i & "'", connection)
Dim ds_cols As New DataSet
adapter.Fill(ds_cols)

For Each c As DataRow In ds_cols.Tables(0).Rows
 'doing lots of things in here


Next

Now, i would need to query the database (or even better edit the existing one) to know if the current column i am analizing is an identity column.

Thank you

Was it helpful?

Solution

You can check the sys.columns view:

SELECT
    name, is_identity 
FROM sys.columns
WHERE name = 'YourColumnName' AND object_id = OBJECT_ID('dbo.YourTableName')

If you need the type of the column, you need to join to sys.types:

SELECT
    ColumnName = c.name ,
    c.is_identity,
    TypeName = ty.name
FROM sys.columns c
INNER JOIN sys.types ty ON ty.system_type_id = c.system_type_id
WHERE name = 'YourColumnName' AND object_id = OBJECT_ID('dbo.YourTableName')

OTHER TIPS

Using below query will give you 2 columns i.e name of column and whether it is identity or not.I guess thats what you need

SELECT name, is_identity FROM sys.columns
WHERE   object_id = OBJECT_ID('tableName')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top