视图 依赖 关系
我现在写了个小工具。将视图导出成sql语句。但是视图的依赖关系没有解决。
谁有好的办法。
--------------
SELECT DISTINCT
dbo.sysobjects.name, sysobjects_1.name AS name2,
sysobjects_1.xtype,depnumber,depid,sysdepends.id,sysobjects.type
FROM dbo.sysobjects sysobjects_1 INNER JOIN
dbo.sysdepends ON sysobjects_1.id = dbo.sysdepends.id RIGHT OUTER JOIN
dbo.sysobjects ON dbo.sysdepends.depid = dbo.sysobjects.id
WHERE ((dbo.sysobjects.type = 'v 'or dbo.sysobjects.type = 'P 'or dbo.sysobjects.type = 'u ') and dbo.sysobjects.STATUS> =0 and sysobjects.name <> 'syssegments ' and sysobjects.name <> 'sysconstraints ')
order by depnumber desc
TABLE1 tttt P 2 1253579504 1653580929 U
TABLE1 VIEW1 V 2 1253579504 1621580815 U
TABLE2 VIEW2 V 2 1589580701 1637580872 U
VIEW1 VIEW3 V 2 1621580815 1685581043 V
VIEW2 VIEW3 V 2 1637580872 1685581043 V
TABLE1 tttt P 1 1253579504 1653580929 U
TABLE1 VIEW1 V 1 1253579504 1621580815 U
TABLE2 VIEW2 V 1 1589580701 1637580872 U
tttt ttt P 1 1653580929 1701581100 P
VIEW1 VIEW3 V 1 1621580815 1685581043 V
VIEW2 VIEW3 V 1 1637580872 1685581043 V
TABLE3 NULL NULL NULL NULL NULL U
ttt NULL NULL NULL NULL NULL P
VIEW3 NULL NULL NULL NULL NULL V
不知道这个有没有用。如果有用的话。该如果来程序来实现先后关系。可以用JAVA或。NET
------解决方案--------------------楼主不要那么复杂的。直接可以这样:
USE pubs
DECLARE @SQL_CREATE_VIEW varchar(8000)
SELECT @SQL_CREATE_VIEW=VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME= 'titleview '
PRINT @SQL_CREATE_VIEW
--自动生成建立视图的脚本,如:
/*
CREATE VIEW titleview
AS
select title, au_ord, au_lname, price, ytd_sales, pub_id
from authors, titles, titleauthor
where authors.au_id = titleauthor.au_id
AND titles.title_id = titleauthor.title_id
*/
------解决方案--------------------create view v_1
as
select top 10 name from sysobjects
go
create view v_1_1
as
select * from v_1
go
create view v_1_1_1
as
select * from v_1_1
go
create view v_1_2
as
select * from v_1
go
create view v_2
as
select top 10 name from sysobjects
go
select b.name,a.id,a.depid
from dbo.sysdepends a,dbo.sysobjects b
where a.id=b.id
and b.name in ( 'v_1 ', 'v_2 ', 'v_1_1 ', 'v_1_2 ', 'v_1_1_1 ')
and b.status> 0
/*
name id depid
------------------------------------
v_1 45243216 1
v_1_1 61243273 45243216
v_1_1_1 109243444 61243273
v_1_2 77243330 45243216
v_2 93243387 1
*/
先建立depid=1的视图,再按递归依次建