在Oracle中查询JSON数据会产生语法错误

时间:2017-10-20 08:41:00

标签: sql json oracle

使用Oracle文档(Link here)我尝试使用JSON数据查询表中是否存在某些值。

列中的示例JSON数据如下所示:

  

[{ “ID”: “14843”},{ “ID”: “14103”},{ “ID”: “16632”},{ “ID”: “14776”},{ “ID”:” 14658 “},{” ID “:” 14426 “},{” ID “:” 14838 “},{” ID “:” 14834 “},{” ID “:” 14804 “},{” ID “:” 14803 “},{” ID “:” 14802 “},{” ID “:” 14801 “},{” ID “:” 14779 “},{” ID “:” 14767 “},{” ID “:” 16013 “},{” ID “:” 15980 “},{” ID “:” 15979 “},{” ID “:” 15978 “},{” ID “:” 15977 “},{” ID “:” 15976 “},{” ID “:” 15975 “},{” ID “:” 15974 “},{” ID “:” 15973 “},{” ID “:” 15972 “},{” ID “:” 15971 “},{” ID “:” 15970 “},{” ID “:” 15969 “},{” ID “:” 15968 “},{” ID “:” 15967 “},{” ID “:” 15966 “},{” ID “:” 15965 “},{” ID “:” 15964 “},{” ID “:” 15963 “},{” ID “:” 15962 “},{” ID “:” 15961 “},{” ID “:” 15960 “},{” ID “:” 16012 “},{” ID “:” 15903" }]

我想得到这个JSON数据包含某些特定值的行。 我试过像这样的查询:

SELECT * FROM SOME_TABLE WHERE json_exists(JSON_COLUMN, '$.id?(@ == 16632)');

根据文档,语法是正确的,但Oracle SQL Developer给了我错误

ORA-40442: Błąd składni wyrażenia ścieżki JSON
40442. 00000 -  "JSON path expression syntax error"
*Cause:    The provided JavaScript Object Notation (JSON) path expression
           had an invalid syntax and could not be parsed.
*Action:   Provide JSON path expression with the correct syntax. Error at Line: 1 Column: 76

我甚至尝试从文档中复制'$.LineItems?(@.Part.UPCCode == 85391628927)',但仍然会出现同样的错误。 你能告诉我这种语法有什么问题吗?

编辑: 我尝试过的所有版本(都出现了同样的错误):

SELECT * FROM SOME_TABLE WHERE json_exists(JSON_COLUMN, '$.id?(@ == "16632")');
SELECT * FROM SOME_TABLE WHERE json_exists(JSON_COLUMN, '$?(@.id == "16632")');
SELECT * FROM SOME_TABLE WHERE json_exists(JSON_COLUMN, '$?(@.id == 16632)');

1 个答案:

答案 0 :(得分:1)

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "select user_id from users where user_name='" + Session["usern"] + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();

我猜@Alex Pool是对的,你没有运行12.2