国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

MS SQLSERVER 中如何得到表的創建語句

2024-07-21 02:05:24
字體:
來源:轉載
供稿:網友

ms sqlserver 只能得到存儲過程的創建語句,方法如下:

sp_helptext procedurename

但是往往我們需要得到表的創建語句,比如說在數據庫升級的時候判斷某個表是否已經改變,或者已經有一個表存在,但不知道它的創建語句是什么,字段有沒有約束,有沒有主鍵,創建了哪些索引等等.下面我給出一個存儲過程,供讀者參考.

該存儲過程可以得到你想得到的所有的表的創建語句,包括和表有關的索引的創建語句.

sqlserver2000 下的代碼

create procedure sp_get_table_info
@objname varchar(128)       /* the table to generate sql script */
as

declare @script varchar(255)
declare @colname varchar(30)
declare @colid   tinyint
declare @usertype smallint
declare @typename sysname
declare @length   tinyint
declare @prec     tinyint
declare @scale    tinyint
declare @status   tinyint
declare @cdefault int
declare @defaultid tinyint
declare @const_key varchar(255)
declare @indid     smallint 
declare @indstatus int
declare @index_key varchar(255)
declare @dbname    varchar(30)
declare @strpri_key varchar (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(255) not null,
    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.usertype = b.usertype order by a.colid

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 + '(' + convert(char(3),@length) + ') '
    else if @usertype in (24)
      select @script = @script + '(' + convert(char(3),@prec) + ','
                      + convert(char(3),@scale) + ') '
    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)[email protected]
              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)


sqlserver6.5下的代碼

create procedure sp_get_table_info
@objname varchar(128)       /* the table to generate sql script */
as

declare @script varchar(255)
declare @colname varchar(30)
declare @colid   tinyint
declare @usertype smallint
declare @typename sysname
declare @length   tinyint
declare @prec     tinyint
declare @scale    tinyint
declare @status   tinyint
declare @cdefault int
declare @defaultid tinyint
declare @const_key varchar(255)
declare @indid     smallint
declare @indstatus smallint
declare @index_key varchar(255)
declare @segment   smallint
declare @dbname    varchar(30)
declare @strpri_key varchar (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(255) not null,
    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 case c.text when "(' ')" then "('')" else c.text end
        from syscomments c where a.cdefault = c.id) end const_key
        from syscolumns a, systypes b where object_name(a.id) = @objname
        and a.usertype = b.usertype order by a.colid

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 + '(' + convert(char(3),@length) + ') '
    else if @usertype in (24)
      select @script = @script + '(' + convert(char(3),@prec) + ','
                      + convert(char(3),@scale) + ') '
    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,segment from sysindexes where object_name(id)[email protected]
              and indid > 0 and indid<>255 order by indid
open cursor_index
fetch next from cursor_index into @colname, @indid, @indstatus, @segment
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' + ','
    /*
 **  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.
    */
    if @segment <> 1
      select @script = @script + ' on ' + name
  from syssegments
  where segment = @segment
  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, @segment
end
close cursor_index
deallocate cursor_index

select script from #spscript order by id

set nocount off

return (0)


 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 加查县| 梁河县| 朝阳市| 东海县| 同心县| 海城市| 开远市| 阿尔山市| 福清市| 阳原县| 东山县| 阿克陶县| 昌乐县| 天台县| 松原市| 嘉兴市| 寿阳县| 阿尔山市| 顺平县| 张家川| 常宁市| 大连市| 剑阁县| 屏东市| 吉安市| 彩票| 磐安县| 邻水| 莱西市| 涪陵区| 桓台县| 双城市| 囊谦县| 麻城市| 南安市| 普兰县| 铁力市| 普兰店市| 东莞市| 青铜峡市| 清苑县|