Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_pkeys

  No additional text.


Syntax

create procedure sys.sp_pkeys
(
    @table_name      sysname,
    @table_owner     sysname = null,
    @table_qualifier sysname = null
)
as
    declare @table_id           int
    -- quotename() returns up to 258 chars
    declare @full_table_name    nvarchar(517) -- 258 + 1 + 258

    if @table_qualifier is not null
    begin
        if db_name() <> @table_qualifier
        begin   -- If qualifier doesn't match current database
            raiserror (15250, -1,-1)
            return
        end
    end

    if @table_owner is null
    begin   -- If unqualified table name
        select @full_table_name = quotename(@table_name)
    end
    else
    begin   -- Qualified table name
        if @table_owner = ''
        begin   -- If empty owner name
            select @full_table_name = quotename(@table_owner)
        end
        else
        begin
            select @full_table_name = quotename(@table_owner) + '.' + quotename(@table_name)
        end
    end

    select @table_id = object_id(@full_table_name)

    select
        TABLE_QUALIFIER = convert(sysname,db_name()),
        TABLE_OWNER = convert(sysname,schema_name(o.schema_id)),
        TABLE_NAME = convert(sysname,o.name),
        COLUMN_NAME = convert(sysname,c.name),
        KEY_SEQ = convert (smallint,
            case
                when c.name = index_col(@full_table_name, i.index_id,  1) then 1
                when c.name = index_col(@full_table_name, i.index_id,  2) then 2
                when c.name = index_col(@full_table_name, i.index_id,  3) then 3
                when c.name = index_col(@full_table_name, i.index_id,  4) then 4
                when c.name = index_col(@full_table_name, i.index_id,  5) then 5
                when c.name = index_col(@full_table_name, i.index_id,  6) then 6
                when c.name = index_col(@full_table_name, i.index_id,  7) then 7
                when c.name = index_col(@full_table_name, i.index_id,  8) then 8
                when c.name = index_col(@full_table_name, i.index_id,  9) then 9
                when c.name = index_col(@full_table_name, i.index_id, 10) then 10
                when c.name = index_col(@full_table_name, i.index_id, 11) then 11
                when c.name = index_col(@full_table_name, i.index_id, 12) then 12
                when c.name = index_col(@full_table_name, i.index_id, 13) then 13
                when c.name = index_col(@full_table_name, i.index_id, 14) then 14
                when c.name = index_col(@full_table_name, i.index_id, 15) then 15
                when c.name = index_col(@full_table_name, i.index_id, 16) then 16
            end),
        PK_NAME = convert(sysname,i.name)
    from
        sys.indexes i,
        sys.all_columns c,
        sys.all_objects o
    where
        o.object_id = @table_id and
        o.object_id = c.object_id and
        o.object_id = i.object_id and
        i.is_primary_key = 1 and
        (c.name = index_col (@full_table_name, i.index_id,  1) or
         c.name = index_col (@full_table_name, i.index_id,  2) or
         c.name = index_col (@full_table_name, i.index_id,  3) or
         c.name = index_col (@full_table_name, i.index_id,  4) or
         c.name = index_col (@full_table_name, i.index_id,  5) or
         c.name = index_col (@full_table_name, i.index_id,  6) or
         c.name = index_col (@full_table_name, i.index_id,  7) or
         c.name = index_col (@full_table_name, i.index_id,  8) or
         c.name = index_col (@full_table_name, i.index_id,  9) or
         c.name = index_col (@full_table_name, i.index_id, 10) or
         c.name = index_col (@full_table_name, i.index_id, 11) or
         c.name = index_col (@full_table_name, i.index_id, 12) or
         c.name = index_col (@full_table_name, i.index_id, 13) or
         c.name = index_col (@full_table_name, i.index_id, 14) or
         c.name = index_col (@full_table_name, i.index_id, 15) or
         c.name = index_col (@full_table_name, i.index_id, 16))
    order by 1, 2, 3, 5

 
Last revision 2008RTM
See also

  sp_ddopen (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash