如何在整个读取行为空的情况下使用SQL合并

时间:2018-10-05 11:03:33

标签: sql db2-400 rpgle

我要解决的情况是这样的:对于表中的每一行,可能存在第二个表中的另一行,因此我需要第一个表的行中的所有数据和第二个表的行中的数据如果有的话。

我知道我可以使用数据结构作为宿主变量来从表中的一行收集所有数据。所以,我的选择是这样:

select
    t1.*
   ,t2.*
into
    :dst1
   ,:dst2
from table1 t1
  left join table2 t2 on t2.key=t1.key
;

其中dst1和dst2分别是数据结构,如table1和table2记录的格式。很简单。

现在,重点是当第二张表中不存在该键的行时如何捕获空结果。在那种情况下,我想初始化对应的数据结构,但是合并一次只能在一个字段上进行,而我无法找到另一种解决方案。

有没有办法得到这个结果?

任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:5)

一种解决方法是使用指标变量。看起来像这样:

fetch('api/public/libraries/sign-out-discourse', {
    method: 'POST',
    headers: new Headers([
        // I get the idToken from CognitoUser.getSession => getIdToken()
        ['Authorization', idToken],
    ]),
})

注意::hs和:hsind之间没有逗号(,),因为这是同一变量分配的一部分。 :hsind是一个指示符变量,在这种情况下是{"message":"'Object]' not a valid key=value pair (missing equal-sign) in Authorization header: '[object Object]'."} 的数组,其元素数量与主机数据结构相同:hs具有字段。如果:hs的关联字段中的值很好,那么指示符变量将包含0;如果为null,则该变量将包含-1。因此,在上面的示例中:如果hs.field1好,并且hs.field2为null,则hsind(1)= 0,hsind(1)= -1。其他值表示其他情况,例如数据映射错误(-2)或字符串截断(带有原始字符串长度的正数)。

因此在您的示例中,使用类似以下的内容:

fetch('api/public/libraries/sign-out-discourse', {
    method: 'POST',
    headers: new Headers([
        // I get the idToken from CognitoUser.getSession => getIdToken().getJwtToken()
        ['Authorization', jwtToken],
    ]),
})

如果Int(5)的元素数与{"message":"Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=eyJraWQiOiJOemhFe..."} 相同,则dcl-ds hs Qualified; field1 ... field2 ... endds; dcl-s hsind Int(5) Dim(2); exec sql select * into :hs:hsind from table fetch first row only; 是一个数组,其中子字段与Int(5)类似。然后,在选择之后,只需选中select t1.* ,t2.* into :dst1:dst1ind ,:dst2:dst2ind from table1 t1 left join table2 t2 on t2.key=t1.key ; ,就可以很好地进行选择。请注意,您需要确保dst1ind仅返回一行,否则您将得到错误提示。