Solved

VB.net SQL Check if Primary key is an identity column

Posted on 2013-06-25
5
467 Views
Last Modified: 2013-06-25
Hi

I use the following VB.net code to check if a column is a primary key.
How do I find if it is an identity column
For example "[ID] bigint identity(1,1) primary key" is an identity column
and "[ID] bigint primary key" is not an identity column


               sSQL = "SELECT column_name "
                sSQL = sSQL & "FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE "
                sSQL = sSQL & "WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1"
                sSQL = sSQL & "AND table_name = '" & oTableName & "'"

                Dim connection As New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
                Dim cmd As New SqlCommand(sSQL, connection)
                connection.Open()
                Get_Primary_Key = cmd.ExecuteScalar().ToString
                connection.Close()
0
Comment
Question by:murbro
5 Comments
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 167 total points
ID: 39274091
Try:
SELECT name, is_identity FROM sys.columns WHERE [object_id] = object_id('table_name')

Open in new window

which will list column name with is_identity column value as 1 if the column has identity
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 166 total points
ID: 39274094
to check if column is mandatory, u simply check the column.AllowDBNull property:

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.allowdbnull.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
0
 
LVL 13

Accepted Solution

by:
Philip Pinnell earned 167 total points
ID: 39274096
You probably want something like

SELECT [IS_NULLABLE]
FROM [msdb].[INFORMATION_SCHEMA].[COLUMNS]
where [column_name] = 'myColumn'
0
 

Author Closing Comment

by:murbro
ID: 39274109
Thanks very much
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 39274243
Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question