Solved

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

Posted on 2013-06-25
5
462 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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 to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

18 Experts available now in Live!

Get 1:1 Help Now