Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_statistics_100

  No additional text.


Syntax

create procedure sys.sp_statistics_100
(
    @table_name         sysname,        -- Wildcard pattern matching IS NOT supported.
    @table_owner        sysname = null, -- Wildcard pattern matching IS NOT supported.
    @table_qualifier    sysname = null,
    @index_name         sysname = '%',  -- Wildcard pattern matching is supported.
    @is_unique          char(1) = 'N',
    @accuracy           char(1) = 'Q'
)
as
    set nocount on

    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 @accuracy not in ('Q','E')
    begin
        raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
        return
    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
    --  Get Object ID
    select @table_id = object_id(@full_table_name)

    -- Rows for indexes.
    select
        TABLE_QUALIFIER     = db_name(),
        TABLE_OWNER         = schema_name(o.schema_id),
        TABLE_NAME          = o.name,
        NON_UNIQUE          = convert(smallint,
                              case
                                  when x.is_unique = 0 then 1         -- Nonunique index
                                  else 0                              -- Unique index
                              end),
        INDEX_QUALIFIER     = o.name,
        INDEX_NAME          = x.name,
        TYPE                = isnull(convert(smallint, -- workaround engine limitation, see SQL BU 373271
                              case
                                  when x.index_id > 1 then 3          -- Non-Clustered
                                  else 1                              -- Clustered index
                              end), sysconv(smallint,1)),
        SEQ_IN_INDEX        = convert(smallint,
                case
                  when x.type < 3
                    then s_ic.key_ordinal
                                    else 1			      -- use 1 as the column ordinal(application indexes are single column indexes)
                                  end),
        COLUMN_NAME         = case
                when x.type < 3
                  then index_col(@full_table_name, x.index_id, s_ic.key_ordinal)
                  else index_col(@full_table_name, x.index_id, 1)
                end,
        -- For indexes return whether ASC or DESC and for statistics return null
        [COLLATION]         = convert(char(1),
                                case
                                when s_ic.is_descending_key = 1
                                  then 'D' -- DB_COLLATION_DESC
                                  else 'A' -- DB_COLLATION_ASC
                                end),
        CARDINALITY         = convert(int,
                                case
                                when x.index_id > 1 then
                                    null -- Non-Clustered
                                else
                                    0x7FFFFFFF & p.rows -- Clustered index
                              end),
        PAGES               = convert(int,
                                case
                                when x.index_id > 1 then
                                    null -- Non-Clustered
               else
                                    p.data_pages -- Clustered index
                              end),
        FILTER_CONDITION    = convert(varchar(128),null)

    from
        sys.objects o inner join -- Yukon doesn't expose indexes on its system objects, so only user objects are useful.
        sys.indexes x on
            (
                x.object_id = o.object_id and
                (x.is_unique = 1 or @is_unique <> 'Y') and
                x.is_hypothetical = 0
            ) inner join
        sys.index_columns s_ic on
            (
                s_ic.object_id = o.object_id and
                s_ic.index_id = x.index_id and
                (s_ic.key_ordinal > 0 or x.type = 3 or x.type = 4) -- include spatial and xml indexes which don't have "key" columns
            ) left join
        sys.index_counts p on
            (
                p.object_id = x.object_id and
                p.index_id = x.index_id
            )

    where
        o.object_id = @table_id and
        (@index_name = '%' or x.name like @index_name) -- matching name

    union all

    -- Add row for table statistics.
    select
        TABLE_QUALIFIER     = db_name(),
        TABLE_OWNER         = schema_name(o.schema_id),
        TABLE_NAME          = o.name,
        NON_UNIQUE          = convert(smallint, null),
        INDEX_QUALIFIER     = convert(sysname, null),
        INDEX_NAME          = convert(sysname, null),
        TYPE                = isnull(convert(smallint, 0), sysconv(smallint,0)), -- Statistics for a table.
        SEQ_IN_INDEX        = convert(smallint, null),
        COLUMN_NAME         = convert(sysname, null),
        [COLLATION]         = convert(char(1), null),
        CARDINALITY         = convert (int, 0x7FFFFFFF & p.rows),
        PAGES               = p.data_pages,
        FILTER_CONDITION    = convert(varchar(128),null)

    from
        sys.objects o inner join -- Yukon doesn't expose indexes on its system objects, so only user objects are useful.
        sys.indexes x on
            (
                x.object_id = o.object_id and
                (x.index_id = 0 or x.index_id = 1) -- If there are no indexes then table stats are in a row with index_id =0
            ) left join
        sys.index_counts p on
            (
                p.object_id = x.object_id and
                p.index_id = x.index_id
            )

    where
        o.object_id = @table_id

    order by 4, 7, 6, 8

 
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