Solved

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

Posted on 2013-06-25
5
458 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
Comment Utility
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
Comment Utility
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
Comment Utility
You probably want something like

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

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now