Solved

VB.net Get a count of the number of primary keys in a SQL table

Posted on 2014-11-10
3
224 Views
Last Modified: 2014-11-10
Hi

I use the following code to get the primary key for a SQL table. If the table has more than one primary key
how do I get a count of the number of primary keys? Thanks

            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
3 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40432381
A table can only have one primary key.

More than one column can be engaged in the primary key, but there will only be one primary key in the table.
0
 
LVL 32

Accepted Solution

by:
ste5an earned 250 total points
ID: 40432487
E.g.

 
USE AdventureWorks2012;

SELECT  KC.name ,
        C.name 
FROM    sys.schemas S
        INNER JOIN sys.tables T ON T.schema_id = S.schema_id
        INNER JOIN sys.key_constraints KC ON KC.parent_object_id = T.object_id
        INNER JOIN sys.index_columns IC ON IC.object_id = T.object_id
                                           AND IC.index_id = KC.unique_index_id
        INNER JOIN sys.columns C ON C.object_id = T.object_id
                                    AND C.column_id = IC.column_id
WHERE   S.name = 'Production'
        AND T.name = 'ProductInventory'
	AND KC.type = 'PK';

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40432524
Thanks for clarifying that a table can only one primary key. Also thanks for the code
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now