首页 > 解决方案 > Japanese Characters are not recognized when using Invoke-SqlCmd -Query

问题描述

I'm inserting data from a json file to an SQL server table using Invoke-SqlCmd and using a stored procedure as following:

Invoke-SqlCmd -ServerInstance $servername -Database $database -Query "EXEC dbo.InsertDataFromJson @JSON='$json'

The json is obtained by getting it's raw content:

$json = Get-Content -Path "path\to.json" -Raw

$json # Content:
'{"Id": "2fe2353a-ddd7-479a-aa1a-9c2860680477", 
"RecordType": 20, 
"CreationTime": "2021-02-14T08:32:23Z", 
"Operation": "ViewDashboard", 
"UserKey": "10099", 
"Workload": "PowerBI", 
"UserId": "102273335@gmail.com", 
"ItemName": "テスト", 
"WorkSpaceName": "My Workspace", 
"DashboardName": "テスト", 
"ObjectId": "テスト" }'

All the column with strings, emails and japanese characters are NVARCHAR(MAX).

The problem is my json contains Japanese characters and they appear as ???? in the table.

When I try to insert a sample using SSMS directly it works fine.

Do you have any idea how to fix this ?

Thank you

标签: sqljsonpowershellinvoke-sqlcmd

解决方案


尝试将 -Encoding 标志设置为 Utf8。

{"test":"みんな"}
Get-Content -Path ".\test.json" -Encoding Utf8

推荐阅读