0
SQL Server 2005 Express中有沒有一種方法可以根據表主/外鍵關係生成一個可以創建VIEW的腳本?SQL Server 2005 Express - 生成基於關係表創建VIEW的腳本?
我有多個包含數千個表的數據庫,它非常耗時查看「表依賴關係」並嘗試在查詢窗口中加入數據。
感謝
SQL Server 2005 Express中有沒有一種方法可以根據表主/外鍵關係生成一個可以創建VIEW的腳本?SQL Server 2005 Express - 生成基於關係表創建VIEW的腳本?
我有多個包含數千個表的數據庫,它非常耗時查看「表依賴關係」並嘗試在查詢窗口中加入數據。
感謝
該查詢返回創建表的語句,但你必須首先注意的是:1。 只適用於1列外鍵引用 2. SQL Server Express的2005年尚未經過測試,但作品罰款對於SQL Server 2005
create function dbo.func59C217D64BC54EA0B841BF1AB43D9398(@table1 nvarchar(1000), @table2 nvarchar(1000))
returns nvarchar(max)
as
begin
declare @sql nvarchar(max)
set @sql = ''
select @sql = @sql + dr + '.[' +cc +'] AS ['+ cc+ ISNULL(rr,'') + '],'
from (
select column_name cc,
dr = case when table_schema + '.' +table_name = @table1 then 'a' else 'b' end,
cast(NULLIF(row_number() over (partition by column_name order by table_name),1) as nvarchar) rr
from INFORMATION_SCHEMA.COLUMNS where table_schema + '.' +table_name in
(@table1,@table2)
) i
return substring(@sql,1,len(@sql) - 1)
end
GO
select cast(
'CREATE VIEW ['+r.CONSTRAINT_SCHEMA+'].[vw' +cp.TABLE_NAME+cf.TABLE_NAME+ ']
AS SELECT '+dbo.func59C217D64BC54EA0B841BF1AB43D9398(cp.TABLE_SCHEMA+'.' +cp.TABLE_NAME,cf.TABLE_SCHEMA+ '.' +cf.TABLE_NAME)+' FROM ['+cp.TABLE_SCHEMA+'].['+cp.TABLE_NAME+'] AS a
JOIN ['+cf.TABLE_SCHEMA+'].['+cf.TABLE_NAME +'] AS b
ON a.['+cp.COLUMN_NAME+'] = b.['+cf.COLUMN_NAME +']' as ntext) as sql
from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS r
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cf ON r.CONSTRAINT_NAME = cf.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cp ON r.UNIQUE_CONSTRAINT_NAME = cP.CONSTRAINT_NAME
GO
DROP function dbo.func59C217D64BC54EA0B841BF1AB43D9398
希望我幫助,或者至少讓你開始
太好了!它確實!謝謝! – tdjfdjdj