T-SQL Post-部署脚本如何引用一个变量
T-SQL Post-Deployment Script how to reference a variable
我正在使用以下 post-部署脚本 1 部署 DACPAC:
ALTER DATABASE [$(DatabaseName)]
MODIFY FILE (NAME = [$(DatabaseName)],
SIZE = 100MB, MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
但是,当我尝试引用此 post-部署脚本 2 时,部署失败:
ALTER DATABASE [$(DatabaseName)]
MODIFY FILE (NAME = [$(DatabaseName)],
SIZE = $(size), MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
那么,我的问题是传递大小的正确语法是什么?
我使用此命令启动脚本(第一个脚本 1 有效,脚本 2 无效)
$resourceGroup = "SQL1"
$vmName = "SQL1"
$storageName = "sql1artifactsstorage111"
$ConfigurationPath = "C:\DSC\Ext\deployDB.ps1"
$ConfigurationName = "deployDB"
$configurationArchive = "deployDB.ps1.zip"
#Publish the configuration script into user storage
Publish-AzureRmVMDscConfiguration -ConfigurationPath $ConfigurationPath -ResourceGroupName $resourceGroup -StorageAccountName $storageName -force
#Set the VM to run the DSC configuration
$configurationArguments =
@{
Credential = Get-Credential;
DatabaseName = 'Database1'
size = '100MB'
}
Set-AzureRmVmDscExtension -Version 2.22 -Name dscExtension -ResourceGroupName $resourceGroup -VMName $vmName -ArchiveStorageAccountName $storageName -ArchiveBlobName $configurationArchive -AutoUpdate:$true -ConfigurationName $ConfigurationName -ConfigurationArgument $configurationArguments
这是 DSC 显示的错误:
Dac Deploy Failed: 'Exception calling "Deploy"
with "3" argument(s): "An error occurred during deployment plan generation.
Deployment cannot continue."'
动态SQL是无赖最后的避难所:
EXEC ('ALTER DATABASE [$(DatabaseName)] MODIFY FILE (NAME = [$(DatabaseName)],SIZE = $(size), MAXSIZE = UNLIMITED, FILEGROWTH = 20%)');
GO
对我有用,尽管是在本地部署而不是通过 Azure DSC。
这是执行这些通常不支持 TSQL 变量替换的 "DBA-esque" 活动的通用技术。
未来读者请注意:这个技巧显然奏效了,也就是说 OP 已将其标记为答案。但是,由于工作站和构建服务器之间的 DacFX 版本不同,它 可能 是必需的,例如参见 [=11=]。
我正在使用以下 post-部署脚本 1 部署 DACPAC:
ALTER DATABASE [$(DatabaseName)]
MODIFY FILE (NAME = [$(DatabaseName)],
SIZE = 100MB, MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
但是,当我尝试引用此 post-部署脚本 2 时,部署失败:
ALTER DATABASE [$(DatabaseName)]
MODIFY FILE (NAME = [$(DatabaseName)],
SIZE = $(size), MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
那么,我的问题是传递大小的正确语法是什么?
我使用此命令启动脚本(第一个脚本 1 有效,脚本 2 无效)
$resourceGroup = "SQL1"
$vmName = "SQL1"
$storageName = "sql1artifactsstorage111"
$ConfigurationPath = "C:\DSC\Ext\deployDB.ps1"
$ConfigurationName = "deployDB"
$configurationArchive = "deployDB.ps1.zip"
#Publish the configuration script into user storage
Publish-AzureRmVMDscConfiguration -ConfigurationPath $ConfigurationPath -ResourceGroupName $resourceGroup -StorageAccountName $storageName -force
#Set the VM to run the DSC configuration
$configurationArguments =
@{
Credential = Get-Credential;
DatabaseName = 'Database1'
size = '100MB'
}
Set-AzureRmVmDscExtension -Version 2.22 -Name dscExtension -ResourceGroupName $resourceGroup -VMName $vmName -ArchiveStorageAccountName $storageName -ArchiveBlobName $configurationArchive -AutoUpdate:$true -ConfigurationName $ConfigurationName -ConfigurationArgument $configurationArguments
这是 DSC 显示的错误:
Dac Deploy Failed: 'Exception calling "Deploy"
with "3" argument(s): "An error occurred during deployment plan generation.
Deployment cannot continue."'
动态SQL是无赖最后的避难所:
EXEC ('ALTER DATABASE [$(DatabaseName)] MODIFY FILE (NAME = [$(DatabaseName)],SIZE = $(size), MAXSIZE = UNLIMITED, FILEGROWTH = 20%)');
GO
对我有用,尽管是在本地部署而不是通过 Azure DSC。
这是执行这些通常不支持 TSQL 变量替换的 "DBA-esque" 活动的通用技术。
未来读者请注意:这个技巧显然奏效了,也就是说 OP 已将其标记为答案。但是,由于工作站和构建服务器之间的 DacFX 版本不同,它 可能 是必需的,例如参见 [=11=]。