I need to make a some sort of template from existing database (MS SQL 2012). Template must have identical schema (tables, indexes, roles, etc), but it must not have data from source DB.
To achieve this, I'm using Transfer
class from SMO. My current transfer settings are:
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;
One of the objects in source DB is an application role. Generated transfer script for this role looks like this:
/* 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
The question: how to transfer this role using password from source DB? I don't need this "security" here. I can't find appropriate transfer option. Am I missing somethig?
There isn't a way to do that, because SQL Server doesn't allow the password to be viewed in the meta-data. Therefore, when transferring application roles you need to specify a new password for them. The same is true for logins, and various properties of symmetric and asymmetric keys.