2012-12-07 18 views
1

我需要從現有數據庫中創建某種模板(MS SQL 2012)。模板必須具有相同的模式(表,索引,角色等),但不得有源數據庫中的數據。如何使用密碼傳輸應用程序角色?

爲了達到這個目的,我使用SMO的Transfer類。我目前的傳輸設置爲:

 transfer.CopyAllObjects = true; 
     transfer.CopyAllSynonyms = true; 
     transfer.CopyData = false; 

     transfer.Options.WithDependencies = true; 
     transfer.Options.DriAll = true; 
     transfer.Options.Triggers = true; 
     transfer.Options.Permissions = true; 
     transfer.Options.Indexes = true; 
     transfer.Options.ContinueScriptingOnError = true; 
     transfer.Options.SchemaQualifyForeignKeysReferences = true; 
     transfer.Options.ExtendedProperties = true; 

源DB中的一個對象是應用程序角色。爲此角色生成的傳輸腳本如下所示:

/* To avoid disclosure of passwords, the password is generated in script. */ 
declare @idx as int 
declare @randomPwd as nvarchar(64) 
declare @rnd as float 
select @idx = 0 
select @randomPwd = N'' 
select @rnd = rand((@@CPU_BUSY % 100) + ((@@IDLE % 100) * 100) + 
     (DATEPART(ss, GETDATE()) * 10000) + ((cast(DATEPART(ms, GETDATE()) as int) % 100) * 1000000)) 
while @idx < 64 
begin 
    select @randomPwd = @randomPwd + char((cast((@rnd * 83) as int) + 43)) 
    select @idx = @idx + 1 
select @rnd = rand() 
end 
declare @statement nvarchar(4000) 
select @statement = N'CREATE APPLICATION ROLE [MyRole] WITH DEFAULT_SCHEMA = [dbo], ' + N'PASSWORD = N' + QUOTENAME(@randomPwd,'''') 
EXEC dbo.sp_executesql @statement 

問題:如何使用來自源數據庫的密碼傳輸此角色?這裏我不需要這個「安全」。我無法找到適當的轉移選項。我錯過了什麼?

+0

爲什麼不使用SQL的'生成腳本'功能爲您的數據庫生成腳本 – Boomer

+0

@Boomer:因爲a)這不是一次性任務,這是應用程序邏輯的一部分; b)SSMS使用相同的'Transfer'類來編寫腳本對象,嘗試創建應用程序角色並自己生成腳本。 – Dennis

回答

2

沒有辦法做到這一點,因爲SQL Server不允許在元數據中查看密碼。因此,在傳輸應用程序角色時,您需要爲它們指定一個新密碼。登錄和對稱和非對稱密鑰的各種屬性也是如此。

+0

是的,深入探討安全存儲過程使我可以做出同樣的決定。 – Dennis

相關問題