You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure ARM模板创建SQL Server、数据库及表失败求助

问题原因与解决方案

核心错误点

  1. 资源类型层级错误:SQL表隶属于数据库的schema(如默认的dbo),ARM模板中对应的资源类型应为Microsoft.Sql/servers/databases/schemas/tables,而非你使用的Microsoft.Sql/servers/databases/tables。
  2. 资源名称格式错误:表的资源名称必须包含schema路径,格式应为{服务器名}/{数据库名}/{schema名}/{表名},缺少schema会导致ARM无法定位资源。
  3. 缺失管理员登录参数:你的模板中SQL Server资源引用了administratorLogin和administratorLoginPassword参数,但未在parameters节点定义,会导致服务器创建失败。

修正后的完整模板

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "serverName": {
            "defaultValue": "ServerTest",
            "type": "string",
            "metadata": {
                "description": "The name of the SQL logical server."
            }
        },
        "sqlDBName": {
            "defaultValue": "DatabaseTest",
            "type": "string",
            "metadata": {
                "description": "The name of the SQL Database."
            }
        },
        "location": {
            "defaultValue": "[resourceGroup().location]",
            "type": "string",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "tableName": {
            "defaultValue": "MyTable",
            "type": "string",
            "metadata": {
                "description": "Name of the table to be created."
            }
        },
        "administratorLogin": {
            "type": "string",
            "metadata": {
                "description": "SQL Server administrator login name."
            }
        },
        "administratorLoginPassword": {
            "type": "securestring",
            "metadata": {
                "description": "SQL Server administrator password."
            }
        }
    },
    "resources": [
        {
            "type": "Microsoft.Sql/servers",
            "apiVersion": "2022-05-01-preview",
            "name": "[parameters('serverName')]",
            "location": "[parameters('location')]",
            "properties": {
                "administratorLogin": "[parameters('administratorLogin')]",
                "administratorLoginPassword": "[parameters('administratorLoginPassword')]"
            }
        },
        {
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2022-05-01-preview",
            "name": "[format('{0}/{1}', parameters('serverName'), parameters('sqlDBName'))]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]"
            ],
            "sku": {
                "name": "Standard",
                "tier": "Standard"
            }
        },
        {
            "type": "Microsoft.Sql/servers/databases/schemas",
            "apiVersion": "2022-05-01-preview",
            "name": "[format('{0}/{1}/dbo', parameters('serverName'), parameters('sqlDBName'))]",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers/databases', parameters('serverName'), parameters('sqlDBName'))]"
            ]
        },
        {
            "type": "Microsoft.Sql/servers/databases/schemas/tables",
            "apiVersion": "2022-05-01-preview",
            "name": "[format('{0}/{1}/dbo/{2}', parameters('serverName'), parameters('sqlDBName'), parameters('tableName'))]",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers/databases/schemas', parameters('serverName'), parameters('sqlDBName'), 'dbo')]"
            ],
            "properties": {
                "columns": [
                    {
                        "name": "ID",
                        "type": "int"
                    },
                    {
                        "name": "Description",
                        "type": "nvarchar",
                        "maxLength": 255
                    }
                ]
            }
        }
    ],
    "outputs": {
        "hostname": {
            "type": "string",
            "value": "Template install complete"
        }
    }
}

关键修改说明

  • 补充了administratorLogin和administratorLoginPassword参数,确保SQL Server能正常创建。
  • 添加了Microsoft.Sql/servers/databases/schemas资源,显式创建默认的dboschema(避免依赖默认schema的延迟创建问题)。
  • 将表的资源类型修正为Microsoft.Sql/servers/databases/schemas/tables,并调整名称格式包含dboschema路径。
  • 表的dependsOn指向schema资源,确保依赖顺序正确。

内容的提问来源于stack exchange,提问作者Alex Gazzard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 03:33:09