简体   繁体   English

向我们寻求正确方法的帮助 SQL 与 CTE 创建 JSON_OBJECT

[英]Asking for help on correct way to us SQL with CTE to create JSON_OBJECT

The requested JSON needs to be in this form:请求的 JSON 需要采用以下形式:

{
    "header": {
        "InstanceName": "US"
    },
    "erpReferenceData": {
        "erpReferences": [
            {
                "ServiceID": "fb16e421-792b-4e9c-935b-3cea04a84507",
                "ERPReferenceID": "J0000755"
            },
            {
                "ServiceID": "7d13d907-0932-44c0-ad81-600c9b97b6e5",
                "ERPReferenceID": "J0000756"
            }
        ]
    }
}

The program that I created looks like this:我创建的程序如下所示:

dcl-s OutFile sqltype(dbclob_file);  

exec sql                                                 
 With x as (                                             
 select  json_object(                                    
 'InstanceName' : trim(Cntry)   ) objHeader                     
 from xmlhdr                                   
 where cntry = 'US'),                                    
                                                         
 y as (                                                  
 select  json_object(                                    
      'ServiceID' VALUE S.ServiceID,                     
      'ERPReferenceID' VALUE I.RefCod) oOjRef      
 FROM IMH I                                           
   INNER JOIN GUIDS G ON G.REFCOD = I.REFCOD 
   INNER JOIN SERV S ON S.GUID = G.GUID               
  WHERE G.XMLTYPE = 'Service')                           
                                                         
   VALUES   (                                            
  select json_object('header'  : objHeader Format json , 
    'erpReferenceData' : json_object(                    
                     'erpReferences' VALUE               
      JSON_ARRAYAGG(               
          ObjRef Format json)))    
       from x                      
   LEFT OUTER JOIN  y ON 1=1       
  Group by objHeader)              
     INTO  :OutFile; 

This is the compile error I get:这是我得到的编译错误:

SQL0122: Position 41 Column OBJHEADER or expression in SELECT list not valid. SQL0122:Position 41 列 OBJHEADER 或 SELECT 列表中的表达式无效。

I am asking if this is the correct way to create this SQL statement, is there a better easier way?我在问这是否是创建此 SQL 语句的正确方法,有没有更好更简单的方法? Any idea how to rewrite the SQL statement to make it work correctly?知道如何重写 SQL 语句以使其正常工作吗?

The key with generating JSON or XML for that matter is to start from the inside and work your way out.生成 JSON 或 XML 的关键是从内部开始,走出去。

(I've simplified the raw data into just a test table...) (我已将原始数据简化为一个测试表......)

with elm as(select json_object 
                   ('ServiceID' VALUE ServiceID,                     
                   'ERPReferenceID' VALUE RefCod) as erpRef
            from jsontst)
select * from elm; 

Now add the next layer as a CTE the builds on the first CTE..现在将下一层添加为 CTE,构建在第一个 CTE 上。

with elm as(select json_object 
                   ('ServiceID' VALUE ServiceID,                     
                   'ERPReferenceID' VALUE RefCod) as erpRef
            from jsontst)
 , arr (arrDta) as (values json_array (select erpRef from elm)) 
select * from arr; 

And the next layer...而下一层...

with elm as(select json_object 
                   ('ServiceID' VALUE ServiceID,                     
                   'ERPReferenceID' VALUE RefCod) as erpRef
            from jsontst)
 , arr (arrDta) as (values json_array (select erpRef from elm)) 
 , erpReferences (refs) as ( select json_object 
                                 ('erpReferences' value arrDta )
                            from arr)
select * 
from erpReferences; 

Nice thing about building with CTE's is at each step, you can see the results so far...使用 CTE 构建的好处在于每一步,您可以看到到目前为止的结果......

You can actually always go back and stick a Select * from CTE;实际上,您始终可以返回 go 并粘贴Select * from CTE; in the middle to see what you have at some point.在中间看看你在某个时候有什么。

Note that I'm building this in Run SQL Scripts.请注意,我正在运行 SQL 脚本中构建它。 Once you have the statement complete, you can embed it in your RPG program.完成语句后,您可以将其嵌入到您的 RPG 程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM