获取表的结构和索引的存储过程SP_GET_TABLE_INFO

       下面的存储过程是用来获取表的结构和表对应的索引,从网上搜索到的在运用的时候大多会出现问题,这个是经过修改后的存储过程,可以获取对应的表的创建语句和表对应的索引。

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

 

 

 

create procedure [dbo].[SP_GET_TABLE_INFO]
@ObjName varchar(128)       /* The table to generate sql script */
as

declare @Script varchar(1000)  --255 长度加长
declare @ColName varchar(100)   --30
declare @ColID   Int --TinyInt
declare @UserType int --TinyInt
declare @TypeName sysname
declare @Length   Int --TinyInt
declare @Prec     Int --TinyInt
declare @Scale    Int --TinyInt
declare @Status   Int --TinyInt
declare @cDefault int
declare @DefaultID Int --TinyInt
declare @Const_Key varchar(1000)--255
declare @IndID     int   --TinyInt
declare @IndStatus Int
declare @Index_Key varchar(1000)--255
declare @DBName    varchar(100) --30
declare @strPri_Key varchar (1000) --255

/*
**  Check to see the the table exists and initialize @objid.
*/
if not Exists(Select name from sysobjects where name = @ObjName)
begin
  select @DBName = db_name()
    raiserror(15009,-1,-1,@ObjName,@DBName)
    return (1)
end

create table #spscript
(
    id     int IDENTITY not null,
    Script Varchar(1000) NOT NULL, --255
    LastLine tinyint
)

declare Cursor_Column INSENSITIVE CURSOR
  for Select a.name,a.ColID,a.usertype,b.name,a.length,a.prec,a.scale,a.Status, a.cDefault,
        case a.cdefault when 0 then ' ' else (select c.Text from syscomments c where a.cdefault = c.id) end const_key
        from syscolumns a, systypes b where object_name(a.id) = @ObjName
        and a.xtype = b.xusertype order by a.ColID 
        --and a.usertype = b.usertype order by a.ColID  --修改了对多个列提示重复的问题,将a.usertype = b.usertype改为 a.xtype = b.xusertype

set nocount on
Select @Script = 'Create table ' + @ObjName + '('
Insert into #spscript values(@Script,0)

/* Get column information */
open Cursor_Column

fetch next from Cursor_Column into @ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,
      @Status,@cDefault,@Const_Key

 

Select @Script = ''
while (@@FETCH_STATUS <> -1)
begin
  if (@@FETCH_STATUS <> -2)
  begin
    Select @Script = '['+@ColName + '] ' + @TypeName
 
    if @UserType in (1,2,3,4)
      Select @Script = @Script + '(' + rtrim(Convert(char(4),@Length)) + ') '  --char(3)
    else if @UserType in (24)
      Select @Script = @Script + '(' + rtrim(Convert(char(4),@Prec)) + ','  --char(3)
                      + rtrim(Convert(char(4),@Scale)) + ') '  --char(3)
    else
      Select @Script = @Script + ' '
    if ( @Status & 0x80 ) > 0
      Select @Script = @Script + ' IDENTITY(1,1) '

    if ( @Status & 0x08 ) > 0
      Select @Script = @Script + ' NULL '
    else
      Select @Script = @Script + ' NOT NULL '
    if @cDefault > 0
      Select @Script = @Script + ' DEFAULT ' + @Const_Key
  end
  fetch next from Cursor_Column into @ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,
      @Status,@cDefault,@Const_Key
  if @@FETCH_STATUS = 0
  begin
    Select @Script = @Script + ','
    Insert into #spscript values(@Script,0)
  end
  else
  begin
    Insert into #spscript values(@Script,1)
    Insert into #spscript values(')',0)
  end
end


Close Cursor_Column
Deallocate Cursor_Column

/* Get index information */
Declare Cursor_Index INSENSITIVE CURSOR
  for Select name,IndID,status from sysindexes where object_name(id)=@ObjName
              and IndID > 0 and IndID<>255  order by IndID   /*增加了对InDid为255的判断*/
Open Cursor_Index
Fetch Next from Cursor_Index into @ColName, @IndID, @IndStatus
while (@@FETCH_STATUS <> -1)
begin
  if @@FETCH_STATUS <> -2
  begin

    declare @i TinyInt
    declare @thiskey varchar(50)
    declare @IndDesc varchar(68) /* string to build up index desc in */

    Select  @i = 1
    while (@i <= 16)
    begin
      select @thiskey = index_col(@ObjName, @IndID, @i)
      if @thiskey is null
        break

      if @i = 1
        select @Index_Key = index_col(@ObjName, @IndID, @i)
      else
        select @Index_Key = @Index_Key + ', ' + index_col(@ObjName, @IndID, @i)
      select @i = @i + 1
    end
    if (@IndStatus & 0x02) > 0
      Select @Script = 'Create unique '
    else
      Select @Script = 'Create '
    if @IndID = 1
      select @Script = @Script + ' clustered '


    if (@IndStatus & 0x800) > 0
     select @strPri_Key = ' PRIMARY KEY (' + @Index_Key + ')'
    else
     select @strPri_Key = ''
    
    if @IndID > 1
      select @Script = @Script + ' nonclustered '
    Select @Script = @Script + ' index ' + @ColName + ' ON '+ @ObjName
           + '(' + @Index_Key + ')'
    Select @IndDesc = ''
    /*
 **  See if the index is ignore_dupkey (0x01).
    */
    if @IndStatus & 0x01 = 0x01
      Select @IndDesc = @IndDesc + ' IGNORE_DUP_KEY' + ','
    /*
     **  See if the index is ignore_dup_row (0x04).
    */
   /* if @IndStatus & 0x04 = 0x04 */
   /*   Select @IndDesc = @IndDesc + ' IGNORE_DUP_ROW' + ',' */ /* 2000 不在支持*/
    /*
 **  See if the index is allow_dup_row (0x40).
    */
--    if @IndStatus & 0x40 = 0x40
--      Select @IndDesc = @IndDesc + ' ALLOW_DUP_ROW' + ','
    if @IndDesc <> ''
    begin
      Select @IndDesc = SubString( @IndDesc, 1, DataLength(@IndDesc) - 1 )
      Select @Script = @Script + ' WITH ' + @IndDesc
    end
    /*
 **  Add the location of the data.
    */
  end
  if (@strPri_Key = '')
    Insert into #spscript values(@Script,0)
  else
    update #spscript set Script = Script + @strPri_Key where LastLine = 1
 
  Fetch Next from Cursor_Index into @ColName, @IndID, @IndStatus
end
Close Cursor_Index
Deallocate Cursor_Index

Select Script from #spscript

set nocount off

return (0)

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mushroom_lb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值