导出表结构脚本
来源:互联网 发布:玉米进出口国家数据 编辑:程序博客网 时间:2024/04/30 15:27
/*
--导出表结构脚本
--用法:
--exec sp_ExportTables 'tb'
--vivianfdlpw 2005.10 引用请保留此信息
*/
IF EXISTS(SELECT 1 FROM SYSOBJECTS WHERE ID=OBJECT_ID('sp_ExportTables') AND XTYPE='p')
DROP PROCEDURE sp_ExportTables
GO
CREATE PROCEDURE sp_ExportTables
@table_name varchar(32)
as
begin
Create Table #CreateStatements (uid int identity(1,1),Info text)
DECLARE @table_id int,
@CurrColumn int,
@MaxColumn int,
@CreateStatement varchar(8000),
@ColumnTypeName varchar(255),
@uid int,
@i int,
@primary_key_field varchar(50)
select @table_id=id from sysobjects where xtype='U' and [name] <> 'dtproperties' and [name] = @table_name
select @primary_key_field = convert(varchar(32),c.name)
from
sysindexes i, syscolumns c, sysobjects o, syscolumns c1
where
o.id = @table_id
and o.id = c.id
and o.id = i.id
and (i.status & 0x800) = 0x800
and c.name = index_col (@table_name, i.indid, c1.colid)
and c1.colid <= i.keycnt
and c1.id = @table_id
Select @CreateStatement = CHAR(13) + 'CREATE TABLE [' + [name] + '] ( ' from SYSOBJECTS WHERE ID=@TABLE_ID
--循环列
Select @CurrColumn=Min(colid),@MaxColumn = Max(colid) from syscolumns where id= @table_id
--Select * from syscolumns where id=1511676433
while @currColumn <= @MaxColumn
begin
--print @currColumn
Declare @UQIndex int, @DefaultValue nvarchar(4000)
set @DefaultValue = null
select @DefaultValue=text from syscomments where id=
(select constid from sysconstraints where id=@table_id and colid=@currColumn)
--处理不同的列类型
SELECT @CreateStatement = @CreateStatement + CHAR(13) + '[' + [name] + '] ' + type_name(xtype) +
case
--ie numeric(10,2)
WHEN type_name(xtype) IN ('decimal','numeric') THEN
' ('+ convert(varchar,prec) + ',' + convert(varchar,length) + ')'
+ case when autoval is null then '' else ' IDENTITY(1,1)' end
+ CASE when isnullable=0 THEN ' NOT NULL' ELSE ' NULL' END
--ie float(53)
WHEN type_name(xtype) IN ('float','real') THEN
' ('+ convert(varchar,prec) + ')'
+ case when autoval is null then '' else ' IDENTITY(1,1)' end
+ CASE when isnullable=0 THEN ' NOT NULL' ELSE ' NULL' END
--ie varchar(40)
WHEN type_name(xtype) IN ('char','varchar','nchar','nvarchar') THEN
' ('+ convert(varchar,length) + ')'
+ case when autoval is null then '' else ' IDENTITY(1,1)' end
+ CASE when isnullable=0 THEN ' NOT NULL' ELSE ' NULL' END
--ie int
ELSE
+ case when autoval is null then '' else ' IDENTITY(1,1)' end
+ CASE when isnullable=0 THEN ' NOT NULL' ELSE ' NULL' END
end
--检测 'PRIMARY KEY'
+ CASE when syscolumns.name = @primary_key_field THEN ' PRIMARY KEY' else '' END
+ CASE when @DefaultValue is null then ''
ELSE
CASE
WHEN type_name(xtype) IN ('decimal','numeric','float','real','bigint','int','smallint','tinyint','money','smallmoney') THEN
' DEFAULT ' + convert(varchar,@DefaultValue)
ELSE
' DEFAULT ' + convert(varchar,@DefaultValue)
END
END
+ ',' from syscolumns where id=@table_id and colid=@CurrColumn
Select @CurrColumn = @CurrColumn + 1
end
insert into #CreateStatements(Info) values(@CreateStatement)
SELECT @CreateStatement=''
select @uid=@@IDENTITY
--添加外键关系代码
declare @cursorID int
declare c1 cursor for SELECT fkeyid from sysforeignkeys where fkeyid=@table_id
open c1
fetch next from c1 into @cursorID
SELECT @CreateStatement=@CreateStatement +
(select + CHAR(13) +'FOREIGN KEY (' + [syscolumns].[name] + ') REFERENCES ' from syscolumns where id=fkeyid and colid =fkey) +
(select (SELECT distinct [sysobjects].[name] from sysobjects where id=rkeyid) + '(' + [syscolumns].[name] + '),' from syscolumns where id=rkeyid and colid =rkey)
from sysforeignkeys where fkeyid=@table_id
close c1
deallocate c1
--添加UNIQUE约束代码
declare c1 cursor for select id from sysobjects where xtype='UQ' and parent_obj=@table_id
open c1
fetch next from c1 into @cursorID
while @@fetch_status >= 0
begin
declare @indid smallint
SELECT @indid = indid,@CreateStatement=@CreateStatement + CHAR(13) + 'CONSTRAINT ' + object_name(@cursorID) + ' UNIQUE '
+ case when (status & 16)=16 then ' CLUSTERED' else ' NONCLUSTERED' end
from sysindexes
where name = object_name(@cursorID) and id = @table_ID
declare @thiskey nvarchar(131), -- 128+3
@keys nvarchar(2126) -- a specific size for MS for whatever reason
select @keys = index_col(@table_name, @indid, 1), @i = 2
if (indexkey_property(@table_id, @indid, 1, 'isdescending') = 1)
select @keys = @keys + '(-)'
select @thiskey = index_col(@table_name, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@table_ID, @indid, @i, 'isdescending') = 1))
select @thiskey = @thiskey + '(-)'
while (@thiskey is not null)
begin
select @keys = @keys + ', ' + @thiskey, @i = @i + 1
select @thiskey = index_col(@table_name, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@table_ID, @indid, @i, 'isdescending') = 1))
select @thiskey = @thiskey + '(-)'
end
Select @CreateStatement=@CreateStatement + '(' + @keys + '),'
fetch next from c1 into @cursorID
end
close c1
deallocate c1
--添加check约束代码
--添加索引代码
DECLARE @ptrval binary(16),@txtlen INT
if len(@CreateStatement) > 0
BEGIN
SELECT @ptrval = TEXTPTR(info) ,
@txtlen = DATALENGTH(info)
FROM #CreateStatements
WHERE uid=@uid
UPDATETEXT #CreateStatements.info @ptrval @txtlen 0 @CreateStatement
END
SELECT @ptrval = TEXTPTR(info) ,
@txtlen = DATALENGTH(info) - 1
FROM #CreateStatements
WHERE uid=@uid
SELECT @CreateStatement= ')'+ CHAR(13)
UPDATETEXT #CreateStatements.info @ptrval @txtlen 1 @CreateStatement
Select info from #CreateStatements
drop table #CreateStatements
end
- 导出表结构脚本
- 转:导出表结构脚本
- powerdesigner 数据库表结构导出excel脚本
- powerdesigner 数据库表结构导出excel脚本
- 快速导出hive表结构脚本:
- MSSQL如何用脚本导出创建表结构的脚本?
- pl /sql导入导出表结构,表数据,sql脚本
- 直接从数据库中导出表结构的脚本
- SQL SERVER 2008 一次性导出表结构脚本
- 导出MySql表结构及MySql执行外部SQL脚本
- ORACLE使用sql脚本手工导出表结构
- Powerdesigner中把表结构导出为Excel的脚本
- SqlServer导出表结构生成脚本及表中数据脚本
- EXP导出表脚本
- 导出表结构
- access导出表结构
- Oracle 导出表结构
- mysql导出表结构
- IN天空的思考
- 追MM“23式”—— GOF 23种设计模式
- 最近的一些打算
- 人间万象“23式” —— GOF 23种设计模式
- 程序员的十种级别
- 导出表结构脚本
- GNU make 指南
- 图片缩放
- 在Eclipse中开发struts应用程序
- java的32个问题
- 我也开始我的软件人生啦
- ARM下的对齐处理
- 用Eclipse Visual Editor 开发SWT GUI
- C++ 完全不完全资源导引