日期:2014-05-17  浏览次数:20435 次

数据库结构对比 更新成最新库
求大侠们给个思路:
               小弟想写一个公共函数实现,两个数据库(一个标准库一个非标准库)的比对找出差异,并且把非标准库更新成标准库的结构。包括键,索引和约束
                       感激不尽!!!
数据库 结构 标准 索引

------解决方案--------------------
有现成的

http://www.codeproject.com/Articles/145979/SQL-Server-Schema-Comparison-Tool-in-Visual-Studio

 http://stackoverflow.com/questions/685053/what-is-best-tool-to-compare-two-sql-server-databases-schema-and-data 
------解决方案--------------------
--查询外键
select b.name FK_Name,c.name ForeignTable,d.name ForeignCol,e.name refTable,f.name refCol
from sysforeignkeys a
inner join sysobjects b  on a.constid=b.id
inner join sysobjects c  on a.fkeyid=c.id
inner join syscolumns d on a.fkeyid=d.id and a.fkey=d.colid
inner join sysobjects e  on a.rkeyid=e.id
inner join syscolumns f on a.rkeyid=f.id and a.rkey=f.colid


--查询表名与字段名
select a.name as 表名,b.name as 字段名
from sysobjects a
left join syscolumns b on a.id=b.id
where a.xtype='U'
order by a.name,b.name