Sql-Server

如何在 sql sever select 語句中使用 Json_value 函式在數據提取期間傳遞 Json 路徑中的局部變數

  • July 31, 2019

我的查詢是

declare @id INT
set @id = 69
select 
  (JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap."69".value')) CorrespondanceNumber,
  (JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap.***"@id"***.value')) Agency,
  (JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap."69".value')) Protocal
from actions_new a
 left join action_data ad on ad.id_ref = a.id
where baid = 12;

您應該將 @id 轉換為 varchar 並將其連接起來。像這樣。

(JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap.***"'+cast(@id as varchar)+'"***.value')) Agency,

完整的查詢是。希望這會幫助你。

declare @id INT
set @id = 69
select 
(JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap."69".value')) CorrespondanceNumber,
(JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap.***"'+cast(@id as varchar)+'"***.value')) Agency,
(JSON_VALUE(cast(ad.data_map as varchar(max)),'$.dataMap."69".value')) Protocal
from actions_new a
left join action_data ad on ad.id_ref = a.id
where baid = 12;

謝謝!

引用自:https://dba.stackexchange.com/questions/244184