1.A. Try create an example Table
-----code:start----------------------------------------------------
CREATE TABLE [dbo].[AAccBS]
(
[ANO] [int] NOT NULL,
[ID] [varchar](10) NOT NULL,
[BSTYPE] [varchar](1) NOT NULL,
[Fee] [float] NULL,
[FeeAmount] [float] NULL,
[FeeRate] [float] NULL,
[Discount] [float] NULL,
[NetF] [bit] NULL,
CONSTRAINT [I_AAccBS] PRIMARY KEY NONCLUSTERED
(
[ANO] ASC,
[ID] ASC,
[BSTYPE] ASC
)
) ON [PRIMARY]
GO
-----code:end----------------------------------------------------
1.B. Try create an example Table
-----code:start----------------------------------------------------
CREATE TABLE [dbo].[AAccBsDet](
[ANO] [int] NOT NULL,
[ID] [varchar](10) NOT NULL,
[BSTYPE] [varchar](1) NOT NULL,
[CODE] [varchar](10) NOT NULL,
[PERIOD] [int] NOT NULL,
[FEE] [float] NULL,
[FeeAmount] [float] NULL,
[Discount] [float] NULL,
CONSTRAINT [I_AAccBsDet] PRIMARY KEY NONCLUSTERED
(
[ANO] ASC,
[ID] ASC,
[BSTYPE] ASC,
[CODE] ASC,
[PERIOD] ASC
)
) ON [PRIMARY]
GO
-----code:end----------------------------------------------------
2. Run this Query
-----code:start----------------------------------------------------
SELECT
t.name as TableName
,col.name AS ColumnName
,i.name AS PrimaryKey_Name
FROM
sys.tables t
INNER JOIN sys.indexes i ON t.object_id = i.object_id
INNER JOIN sys.index_columns c ON t.object_id = c.object_id
AND i.index_id = c.index_id
INNER JOIN sys.columns col ON c.object_id = col.object_id
AND c.column_id = col.column_id
WHERE
i.is_primary_key = 1
AND t.name IN (SELECT name from sys.tables)
ORDER BY t.name, c.key_ordinal
-----code:end----------------------------------------------------
3. Example Results Messages:
Source:
http://www.sqlservercentral.com/scripts/Primary+Key+%28PK%29/93288/
Article : Find Primary Column Of All Tables In Database
By Nitesh Dedhia