json - 如果列名称包含APOSTROPHE(),则无法在SQLSERVER中插入JSON数据

标签 json sql-server powershell

我正在尝试通过使用PowerShell调用存储过程将数据Json格式的数据加载到SQL Server中

以下是JSON数据

{
    "id":"2aa7ce44-2ac1-4efb-a5df-6c34086744b7",
    "isReadOnly":false,
    "isOnDedicatedCapacity":false,
    "capacityMigrationStatus":"",
    "type":"Group",
    "state":"Active",
    "name":"Wes' Test Team",
    "dashboards":[],
    "reports":[],
    "datasets":[],
    "users":[]
},
{
    "id":"cb876bcc-285a-40d7-a120-f23e73baafe8",
    "isReadOnly":false,
    "isOnDedicatedCapacity":false,
    "capacityMigrationStatus":"",
    "type":"Group",
    "state":"Active",
    "name":"Kehat's Corner",
    "dashboards":[],
    "reports":[],
    "datasets":[],
    "users":[]
}

无法插入数据,因为列名包含'(APOSTROPHE),并且脚本无法加载数据。

如果列名中的数据中包含APOSTROPHE,有人可以帮助我如何克服。

PowerShell脚本
Connect-PowerBIServiceAccount 
$PBIGroupsFile = "C:\TEMP\PBIGroupsExpanded.json"
$ActiveGroupsURLExPersonal = '/admin/groups?$top=5000&' + '$filter=type ne' + " 'PersonalGroup'" + '&$expand=dashboards,reports,datasets,users'
Invoke-PowerBIRestMethod -Url $ActiveGroupsURLExPersonal -Method Get | Out-File $PBIGroupsFile

存储过程
ALTER PROCEDURE [dbo].[InsertInventoryDataFromAPI_Temp]
    @JSON NVARCHAR(MAX) 
AS
BEGIN

DECLARE @PARSSEDJSON NVARCHAR(MAX)= SUBSTRING(@JSON,CHARINDEX('"value":',@JSON)+8,LEN(@JSON)-CHARINDEX('"value":[',@JSON)-10)

select  ROW_NUMBER () OVER (ORDER BY id) AS RowNum,* INTO #FLJSON
FROM OPENJSON(REPLACE(REPLACE(@PARSSEDJSON, CHAR(13)+CHAR(10), ''),'''',''))
WITH(
    id nvarchar(max) '$.id',
    isReadOnly nvarchar(max) '$.isReadOnly',
    isOnDedicatedCapacity nvarchar(max) '$.isOnDedicatedCapacity',
    capacityId nvarchar(max) '$.capacityId',
    capacityMigrationStatus nvarchar(max) '$.capacityMigrationStatus',
    type nvarchar(max) '$.type',
    state nvarchar(max) '$.state',
    name nvarchar(max) '$.name',
    dashboards nvarchar(max) '$.dashboards' AS JSON,
    reports nvarchar(max) '$.reports' AS JSON,
    datasets nvarchar(max) '$.datasets' AS JSON,
    users nvarchar(max) '$.users' AS JSON
    ) 

--Load Workspace Info
    INSERT INTO dbo.Workspace_temp
    Select id, isReadOnly,isOnDedicatedCapacity,capacityId,capacityMigrationStatus,type,state,name from #FLJSON

使用PowerShell调用存储过程
$JSON = Get-Content "C:\TEMP\PBIGroupsExpanded.json"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$SQLServer;Database=$SQLDBName;Integrated Security=True;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "EXEC [dbo].[InsertInventoryDataFromAPI_Temp] '$JSON'"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlConnection.Open()
$SQLReturn=$SQLCmd.ExecuteNonQuery()
$SqlConnection.Close()

最佳答案

正如@DanGuzman在评论中指出的那样,您需要使用SQL参数来避免此问题

$SqlCmd.CommandText = "EXEC [dbo].[InsertInventoryDataFromAPI_Temp] @JSON"
$SqlCmd.Parameters.AddWithValue("@JSON", $JSON)

关于json - 如果列名称包含APOSTROPHE(),则无法在SQLSERVER中插入JSON数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60003218/

相关文章:

php - 我想根据登录的用户名使用 php 更新数据库中的字段,但它不起作用

SQL 查询转置数据

sql-server - 是什么原因导致此 SqlException : A transport-level error has occurred when receiving results from the server

regex - netsh 结果到 PowerShell 对象

python - 将 JSON 中的变量替换为列表中的项目

javascript - 来自外部 json 文件的 D3js 条形图

sql - 使用变量过滤'IN'子句中​​的字段

powershell - 变量是数组而不是字符串?

git - 防止 PowerShell "boxing"程序输出

java - 使用 Gson 将 json 转换为 Map.Entry 对象