IBM DB2版本9中的标量全查询问题

时间:2011-10-05 06:02:43

标签: sql db2

我已经构造了如下的查询,当输入有限的一组输入时,其结果集被准确地返回,即,当输入500条记录时,查询结果带有标量全查询错误消息下方。

 "Error: SQL0811N  The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO statement is more than one row.  SQLSTATE=21000 (State:21000, Native Code: FFFFFCD5)"

但是,当同一组记录被拆分并输入时,即通过输入前100个记录然后输入下一个100依此类推,直到所有500条记录都被输入,那么查询工作正常,没有任何记录错误信息。我制定的查询如下。

  select  d.pol_id,
    (select rtrim(a.CLI_INDV_TITL_TXT)||','||
            rtrim(a.ENTR_SUR_NM)||','||
            rtrim(a.CLI_INDV_MID_NM)||','||
            rtrim(a.ENTR_GIV_NM)
     from   uding604.tclnm a
     where  a.co_id = 'CP'
     and    a.cli_id = d.cli_id) "Client Name",
    (select RTRIM(a.CLI_ADDR_LN_1_TXT)||','||
            RTRIM(a.CLI_ADDR_LN_2_TXT)||','||
            RTRIM(a.CLI_ADDR_LN_3_TXT)||','||
            RTRIM(a.CLI_ADDR_MUN_CD)||','||
            RTRIM(a.CLI_ADDR_CNTY_CD)||','||
            RTRIM(a.CLI_CITY_NM_TXT)||','||(select  rtrim(etbl.etbl_desc_txt)
                                            from    uding604.tedit etbl
                                            where   etbl.co_id = 'CP'
                                            and     etbl.etbl_typ_id = 'STACD'
                                            and     etbl.etbl_valu_id = RTRIM(a.CLI_CRNT_LOC_CD))||','||
            (case when rtrim(a.CLI_CTRY_CD) = 'IN'
                  then 'India'
                  else '' end)||','||
            RTRIM(a.CLI_PSTL_CD)
     from   uding604.tclia a
     where  a.co_id = 'CP'
     and    a.cli_addr_typ_cd = 'PR'
     and    a.cli_id = d.cli_id) "Client Address",
     coalesce((select  RTRIM(a.CLI_CNTCT_ID_TXT)
               from    uding604.tclic a
               where   a.co_id = 'CP'
               and     a.CLI_CNTCT_ID_CD = 'CP'
               and     a.cli_id = d.cli_id),'') "Client Mobile",
     coalesce((select  RTRIM(a.CLI_CNTCT_ID_TXT)
               from    uding604.tclic a
               where   a.co_id = 'CP'
               and     a.CLI_CNTCT_ID_CD = 'EM'
               and     a.cli_id = d.cli_id),'') "Client Email",
    (select pol.serv_agt_id
     from   uding604.tpol pol
     where  pol.co_id = 'CP'
     and    pol.pol_id = d.pol_id
     and    pol.serv_agt_id in (select      agt_id
                                from        uding604.tag
                                where       co_id = 'CP'
                                and         agt_stat_cd = 'A')
     fetch first 1 row only) "Agent ID",
    (select case
            when    ag.agt_stat_cd = 'A'
            then    'ACTIVE'
            else    ''
            end
     from   uding604.tag ag
     where  ag.co_id = 'CP'
     and    ag.agt_id in (select    pol.serv_agt_id
                          from      uding604.tpol pol
                          where     pol.co_id = 'CP'
                          and       pol.pol_id = d.pol_id)) "Agent Status",
    (select rtrim(a.CLI_INDV_TITL_TXT)||' '||
            rtrim(a.ENTR_SUR_NM)||' '||
            rtrim(a.CLI_INDV_MID_NM)||' '||
            rtrim(a.ENTR_GIV_NM)
     from   uding604.tclnm a
     where  a.co_id = 'CP'
     and    a.cli_id in (select     pol.serv_agt_id
                         from       uding604.tpol pol
                         where      pol.co_id = 'CP'
                         and        pol.pol_id = d.pol_id
                         and        pol.serv_agt_id in (select      agt_id
                                                        from        uding604.tag
                                                        where       co_id = 'CP'
                                                        and         agt_stat_cd = 'A')
                                                        fetch first 1 row only)) "Advisor Name",
    (coalesce((select RTRIM(a.CLI_CNTCT_ID_TXT)
               from   uding604.tclic a
               where  a.co_id = 'CP'
               and    a.CLI_CNTCT_ID_CD = 'CP'
               and    a.cli_id = (select pol.serv_agt_id
                                  from   uding604.tpol pol
                                  where  pol.co_id = 'CP'
                                  and    pol.pol_id = d.pol_id
                                  and    pol.serv_agt_id in (select agt_id
                                                             from   uding604.tag
                                                             where  co_id = 'CP'
                                                             and    agt_stat_cd = 'A'))),'')) "Agent Mobile",
    (coalesce((select RTRIM(a.CLI_CNTCT_ID_TXT)
               from   uding604.tclic a
               where  a.co_id = 'CP'
               and    a.CLI_CNTCT_ID_CD = 'EM'
               and    a.cli_id = (select    pol.serv_agt_id
                                  from      uding604.tpol pol
                                  where     pol.co_id = 'CP'
                                  and       pol.pol_id = d.pol_id
                                  and       pol.serv_agt_id in (select  agt_id
                                                                from    uding604.tag
                                                                where   co_id = 'CP'
                                                                and     agt_stat_cd = 'A'))),''))"Agent Email",
    (select etbl.etbl_desc_txt
     from   uding604.tedit etbl
     where  etbl.co_id = 'CP'
     and    etbl.etbl_typ_id = 'BRIND'
     and    etbl.etbl_valu_id in (select br_id
                                  from   uding604.tag
                                  where  co_id = 'CP'
                                  and    agt_stat_cd = 'A'
                                  and    agt_id in (select       serv_agt_id
                                                    from         uding604.tpol m
                                                    where        m.co_id = 'CP'
                                                    and          m.pol_id = d.pol_id))) "Advisor Branch Id"
from    uding604.tpolc d
where   d.co_id = 'CP'
and     d.cli_id in (select pol_id from uding604.polid_tmp)  --this subquery holds the records to be provided as input and currently the 'uding604.polid_tmp' table in the subquery holds 500 records
and     d.pol_id in (select     pol_id
                     from       uding604.tpol
                     where      co_id = 'CP'
                     and        pol_cstat_Cd in ('1','4')
                     and        pol_iss_eff_dt between '2008-09-26' and '2010-09-26')
and     d.pol_id in (select max(a.pol_id)
                     from   uding604.tpolc a
                     where  a.co_id = 'CP'
                     and    a.cli_id = d.cli_id)
and     d.pol_cli_rel_typ_cd = 'O'

我觉得我非常接近解决方案,但它是什么?

1 个答案:

答案 0 :(得分:3)

因为当您执行a.cli_id = (select pol.serv_agt_id之类的操作时,等号右侧的部分只能返回一行。

因此,当您添加其他数据时,从100行到500行,附加数据中的某些内容会导致这些子查询返回多个记录。您可以执行in而不是=,或者检查您的数据是否有重复项,或在where子句中添加其他限定符以过滤掉不需要的记录。