Solved

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

Posted on 2013-06-25
5
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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