SQL2000 企业管理器中自带了关系模型工具,可以方便做数据模型的设计。为了方便数据库的迁移,特别写了一个sql脚本,用来把sql2000里的关系图导脚本方式,配合上一篇导出表数据的功能,可以完整的以脚本方式备份数据库了。
代码如下:
/*
导出数据库关系图的创建脚本.
使用方法:
1.设置sql查询分析器
打开SQL 查询分析器,菜单 "工具"-->"选项..." 然后在对话框中选择 "结果"面板,
修改 "每列最多字符数" 为 8000,选择框项目均为不选取
2.用sql查询分析器打开此文档,选择要导出的的数据库,选择“文本显示结果( Ctrl + T )",执行(F5).
3.返回结果即是选定数据库的关系图的创建脚本.
4.如果要只返回特定的关系图创建脚本,请在下面设置变量 @ModelName
longxu 2007-01-03
*/
set nocount on
print'/*****************************************************'
print' 数据库关系图的创建脚本 '
print' '
print' 数据库名 : '+db_name()
print' 版本 : '
print' 内容 : 数据库关系模型图'
print' 创建时间 : '+ convert(varchar(20),getdate(),120)
print' 创建主机 : '+host_name()
print'*****************************************************/'
print' '
declare @ModelName nvarchar(2000)
set @ModelName=''
/*******************************/
--请在下面写入要生成脚本的关系图名称以逗号隔开,如果为空则建立所有模型的脚本
--例如:
--set @ModelName=''
set @ModelName='员工,建筑'
--set @ModelName='组织'
/*******************************/
declare @i int ,@j int, @ubound int
declare @updateSql nvarchar(4000)
declare @mID int ,@id int
declare @m_tmp table(id int , mName nvarchar(50))
declare @mName nvarchar(50)
declare @dtp_tmp table(id int)
if @ModelName is null set @ModelName=''
if @ModelName<>''
begin
insert into @m_tmp (ID,mName)
select objectid,value from dtproperties where ','+@ModelName+',' like '%,'+value+',%' and property='DtgSchemaNAME'
end
else
begin
insert into @m_tmp (ID,mName)
select objectid,value from dtproperties where property='DtgSchemaNAME'
end
print 'declare @mID int '
print 'declare @ID int '
print 'declare @ptrval binary(16) '
print ''
while exists(select * from @m_tmp)
begin
select top 1 @mID=id ,@mName=mName from @m_tmp
delete @dtp_tmp
insert into @dtp_tmp (ID)
select ID from dtproperties where objectID = @mID and ID<>ObjectID
print '/****************************************************'
print ' "'+@mName+'" 模型图创建脚本'
print' '+host_name()+' '+ convert(varchar(20),getdate(),120)
print '****************************************************/'
print '--删除原有 "'+@mName+'" 模型图'
print ' delete dtproperties where objectid=(select ObjectID from dtproperties where value='''+ @mName +''' and property=''DtgSchemaNAME'' )'
print ''
print '--重建 "'+@mName+'" 模型图'
select ' insert into dtproperties (property, value, uvalue, version) values ('+
''''+ property+'''',
',',
''''+value+'''',
',',
''''+uvalue+'''',
',',
version as [ ],
')'
from dtproperties where id=@mID
print ' set @mID=IDENT_CURRENT(''dtproperties'')'
print ''
print ' update dtproperties set objectid=id where id=@mid'
print ''
while exists(select * from @dtp_tmp)
begin
select top 1 @id=id from @dtp_tmp order by id
select ' insert into dtproperties (objectid, property, value, uvalue, version) values (' as [ ],
'@mid',
',',
''''+ property+'''',
',',
''''+value+'''',
',',
''''+uvalue+'''',
',',
version as [ ],
')'
from dtproperties where id=@id
print ' set @ID=IDENT_CURRENT(''dtproperties'')'
print ''
select @ubound=DATALENGTH(lvalue) from dtproperties where id=@id
if not (@ubound is null )
begin
select ' -- 开始 写入'+property+' 二进制数据' from dtproperties where id=@id
set @i = 1
set @j =256
if @j>@ubound set @j = @ubound
while @i<@ubound
begin
if @i=1
begin
print ' update dtproperties set lvalue=0x00 where id=@ID'
print ''
print ' select @ptrval = TEXTPTR(lvalue) from dtproperties where id= @ID'
print ''
select N' updatetext dtproperties.lvalue @ptrval 0 null ',substring(lvalue,@i,@j) from dtproperties where id=@id
end
else
begin
select N' updatetext dtproperties.lvalue @ptrval null null ',substring(lvalue,@i,@j) from dtproperties where id=@id
end
set @i=@i+@j
set @j=@j
if @j>@ubound-@i+1 set @j=@ubound-@i+1
end
select ' -- 完成 写入'+property+' 二进制数据' from dtproperties where id=@id
print ''
end
delete @dtp_tmp where id=@id
end
print '--完成 重建 "'+@mName+'" 模型图'
print ''
print ''
delete @m_tmp where id= @mID
end