Chuyển tới nội dung chính
Trang được dịch

Trang này được dịch từ tiếng Anh với sự hỗ trợ của AI. Nếu hai bản có chỗ khác nhau, hãy lấy phiên bản tiếng Anh làm chuẩn.

JSON_REPLACE

Cập nhật các giá trị hiện có trong đối tượng JSON dựa trên đường dẫn của chúng. Hàm JSON_REPLACE không thêm khóa mới vào tài liệu; hàm chỉ thay thế giá trị của các khóa hiện có.

Xem thêm:
[SELECT] [JSON_EXTRACT] [JSON_SET] [JSON_INSERT]


Cú pháp​

SELECT JSON_REPLACE(json_column, path, new_value) FROM <multipartIdentifier>;

Đối số​

json_column
Cột hoặc biểu thức JSON có giá trị cần thay thế.

path
Biểu thức đường dẫn JSON trỏ đến vị trí trong tài liệu JSON nơi cần thực hiện thay thế. Đường dẫn phải trỏ đến một khóa hiện có.

new_value
Giá trị mới dùng để thay thế tại đường dẫn được chỉ định trong tài liệu JSON.

Giá trị trả về​

Trả về tài liệu JSON đã sửa đổi, trong đó giá trị được chỉ định đã được thay thế. Nếu đường dẫn được chỉ định không tồn tại, hàm trả về tài liệu JSON ban đầu không thay đổi.

Hành vi khi khóa đã tồn tại
HàmGhi đè nếu đã tồn tại?Tạo nếu chưa tồn tại?
json_insert()KhôngCó
json_replace()CóKhông
json_set()CóCó

Ví dụ​

Thay thế một giá trị trong đối tượng JSON​

SELECT sku as original,
JSON_REPLACE(sku, '$.name', 'Standard_EU') as updated
FROM azure.container_registry.registries
WHERE registry_name = 'exampleRegistryProd'
AND subscription_id = '12345678-abcd-1234-abcd-1234abcd5678'
AND resource_group_name = 'exampleResourceGroupProd';
/* expected output...
|---------------------------------------|------------------------------------------|
| original | updated |
|---------------------------------------|------------------------------------------|
| {"name":"Standard","tier":"Standard"} | {"name":"Standard_EU","tier":"Standard"} |
|---------------------------------------|------------------------------------------|
*/

Thay thế nhiều giá trị trong đối tượng JSON​

select
properties beforechange,
json_replace(json_remove(properties,
'$.allowPort25Out',
'$.auxiliarySku',
'$.provisioningState',
'$.resourceGuid',
'$.macAddress',
'$.vnetEncryptionSupported',
'$.enableIPForwarding',
'$.defaultOutboundAccess',
'$.primary',
'$.virtualMachine',
'$.hostedWorkloads',
'$.tapConfigurations',
'$.nicType',
'$.auxiliaryMode',
'$.ipConfigurations[0].id',
'$.ipConfigurations[0].etag',
'$.ipConfigurations[0].type',
'$.ipConfigurations[0].properties.provisioningState',
'$.ipConfigurations[0].properties.privateIPAddress',
'$.ipConfigurations[0].properties.privateIPAllocationMethod',
'$.ipConfigurations[0].properties.primary',
'$.ipConfigurations[0].properties.privateIPAddressVersion'
),
'$.ipConfigurations[0].name',
'vmss-flex-vnet-nic01-defaultIpConfiguration'
) afterchange
from
azure.network.network_interfaces
where subscription_id = '0123456789'
and resource_group_name = 'vm-rg'
and network_interface_name = 'vm1450_z1'