gpt4 book ai didi

sql - 如何在 Azure Runbook 中运行 SQL 批处理?

转载 作者:行者123 更新时间:2023-12-02 07:12:27 24 4
gpt4 key购买 nike

我需要执行批处理以在数据库中执行一些维护任务,但我看到的 Azure 自动化上的所有示例都在处理 a single SQL command .

如果无法创建 SP,我该怎么办?我想我需要以某种方式将我的 script.sql 文件嵌入到 Runbook 脚本中或引用它(例如 here )?

最佳答案

您可以将 .sql 文件存储在 Azure Blob 存储中,并在 Runbook 中下载 .sql 文件,读取其内容,并将其传递给 SqlCommand 对象。

类似于:

try {
# Connect to Azure using service principal auth
$ServicePrincipalConnection = Get-AutomationConnection -Name $AzureConnectionAssetName

Write-Output "Logging in to Azure..."

$Null = Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
}
catch {
if(!$ServicePrincipalConnection) {
throw "Connection $AzureConnectionAssetName not found."
}
else {
throw $_.Exception
}
}

$Path = "C:\abc.sql"
Set-AzureRmCurrentStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName

Get-AzureStorageBlobContent -Container $Container -Blob $Blob -Destination $Path
$Content = Get-Content $Path
$Cmd = New-Object System.Data.SqlClient.SqlCommand($Content, $Conn)

关于sql - 如何在 Azure Runbook 中运行 SQL 批处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com