Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Return the colid of primary key for table
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00663859
Message ID:
00663905
Vues:
22
First, Have you looked at using SQL-DMO?

When I read your question, I asked myself how SEM flags the proper columns as participating in the PK. Using the SQL Profiler, I noticed a stored proc called "sp_MShelpcolumns." This proc is used by the SEM to gather various information about the specified table. The usage of the proc can be determined with the following call:
EXECUTE master..sp_MShelpcolumns '?'
And returns the following:
Usage:  sp_MShelpcolumns @tablename, @flags int = 0
 where @flags is a bitmask of:
 0x0200		= No DRI (ignore Checks, Primary/Foreign/Unique Keys, etc.)
 0x0400		= UDDTs --> Base type
 0x80000		= TimestampToBinary (convert timestamp cols to binary(8))
 0x40000000	= No Identity attribute
The call from the SEM looked like this:
sp_MShelpcolumns N'dbo.authors', null, 'id', 1
'id' goes into a parameter called @orderby. The source has the following comment for the last parameter - @flags2:

/*** @flags2 added for DaVinci uses. If the bit isn't set, use 6.5 ***/

The result set contains a column called col_flags. If the column participates in the PK, the third bit will be set as the following source shows:
/* Determine if the column is in the primary key */
if (@flags & 0x0200 = 0 and (OBJECTPROPERTY(@objid, N'TableHasPrimaryKey') <> 0)) begin
		declare @indid int
		select @indid = indid from dbo.sysindexes i where i.id = @objid and i.status & 0x0800 <> 0
		if (@indid is not null)
			update #sphelpcols set col_flags = col_flags | 0x0004
			from #sphelpcols c, dbo.sysindexkeys i
				where i.id = @objid and i.indid = @indid and i.colid = c.col_id
I'd consider calling this proc to retrieve the information that you're looking for to be about a half-step safer than getting it from the system tables directly.

-Mike
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform