Big" union选择所有"构造 - 如何摆脱`关系 - 过程 - 视图的太多上下文。允许的最大值为255`

时间:2016-10-24 13:29:16

标签: sql firebird firebird2.5

我发现了一个类似于下面的SQL语句。

只要有超过128个条目与union all相结合,我就会收到此错误:Too many Contexts of Relation-Procedure-Views. Maximum allowed is 255

如何重新构造查询以消除错误?

无法将此数据添加到数据库中(它是在对数据库进行完整性检查的脚本中;脚本只有在数据库处于只读模式时才具有读访问权限)。

以下列:

  • ididentifier在每行中都有不同的值
  • id是数字ID,它有预先知道的空白
  • identifier是一个字母数字名称(与以下查询不同,您无法从identifier生成id
  • usage有两个字符串值,对某些id组有效(所以我可以将它们放在一个单独的union all构造中)
  • comment有大约20个不同的字符串值,这些字符串值对某些id组有效(因此我可以将它们放在单独的union all构造中)

重现此内容的查询:

-- 20161024 - Too many Contexts of Relation-Procedure-Views. Maximum allowed is 255
with parameters as
(
select 1001 as id
     , '1001' as usage
     , 'PP_1001' as identifier
     , '1001' as comment
from rdb$database 

union all select 1002,'1002','PP_1002','1002' from rdb$database 
union all select 1003,'1003','PP_1003','1003' from rdb$database 
union all select 1004,'1004','PP_1004','1004' from rdb$database 
union all select 1005,'1005','PP_1005','1005' from rdb$database 
union all select 1006,'1006','PP_1006','1006' from rdb$database 
union all select 1007,'1007','PP_1007','1007' from rdb$database 
union all select 1008,'1008','PP_1008','1008' from rdb$database 
union all select 1009,'1009','PP_1009','1009' from rdb$database 
union all select 1010,'1010','PP_1010','1010' from rdb$database 
union all select 1011,'1011','PP_1011','1011' from rdb$database 
union all select 1012,'1012','PP_1012','1012' from rdb$database 
union all select 1013,'1013','PP_1013','1013' from rdb$database 
union all select 1014,'1014','PP_1014','1014' from rdb$database 
union all select 1015,'1015','PP_1015','1015' from rdb$database 
union all select 1016,'1016','PP_1016','1016' from rdb$database 
union all select 1017,'1017','PP_1017','1017' from rdb$database 
union all select 1018,'1018','PP_1018','1018' from rdb$database 
union all select 1019,'1019','PP_1019','1019' from rdb$database 
union all select 1020,'1020','PP_1020','1020' from rdb$database 
union all select 1021,'1021','PP_1021','1021' from rdb$database 
union all select 1022,'1022','PP_1022','1022' from rdb$database 
union all select 1023,'1023','PP_1023','1023' from rdb$database 
union all select 1024,'1024','PP_1024','1024' from rdb$database 
union all select 1025,'1025','PP_1025','1025' from rdb$database 
union all select 1026,'1026','PP_1026','1026' from rdb$database 
union all select 1027,'1027','PP_1027','1027' from rdb$database 
union all select 1028,'1028','PP_1028','1028' from rdb$database 
union all select 1029,'1029','PP_1029','1029' from rdb$database 
union all select 1030,'1030','PP_1030','1030' from rdb$database 
union all select 1031,'1031','PP_1031','1031' from rdb$database 
union all select 1032,'1032','PP_1032','1032' from rdb$database 
union all select 1033,'1033','PP_1033','1033' from rdb$database 
union all select 1034,'1034','PP_1034','1034' from rdb$database 
union all select 1035,'1035','PP_1035','1035' from rdb$database 
union all select 1036,'1036','PP_1036','1036' from rdb$database 
union all select 1037,'1037','PP_1037','1037' from rdb$database 
union all select 1038,'1038','PP_1038','1038' from rdb$database 
union all select 1039,'1039','PP_1039','1039' from rdb$database 
union all select 1040,'1040','PP_1040','1040' from rdb$database 
union all select 1041,'1041','PP_1041','1041' from rdb$database 
union all select 1042,'1042','PP_1042','1042' from rdb$database 
union all select 1043,'1043','PP_1043','1043' from rdb$database 
union all select 1044,'1044','PP_1044','1044' from rdb$database 
union all select 1045,'1045','PP_1045','1045' from rdb$database 
union all select 1046,'1046','PP_1046','1046' from rdb$database 
union all select 1047,'1047','PP_1047','1047' from rdb$database 
union all select 1048,'1048','PP_1048','1048' from rdb$database 
union all select 1049,'1049','PP_1049','1049' from rdb$database 
union all select 1050,'1050','PP_1050','1050' from rdb$database 
union all select 1051,'1051','PP_1051','1051' from rdb$database 
union all select 1052,'1052','PP_1052','1052' from rdb$database 
union all select 1053,'1053','PP_1053','1053' from rdb$database 
union all select 1054,'1054','PP_1054','1054' from rdb$database 
union all select 1055,'1055','PP_1055','1055' from rdb$database 
union all select 1056,'1056','PP_1056','1056' from rdb$database 
union all select 1057,'1057','PP_1057','1057' from rdb$database 
union all select 1058,'1058','PP_1058','1058' from rdb$database 
union all select 1059,'1059','PP_1059','1059' from rdb$database 
union all select 1060,'1060','PP_1060','1060' from rdb$database 
union all select 1061,'1061','PP_1061','1061' from rdb$database 
union all select 1062,'1062','PP_1062','1062' from rdb$database 
union all select 1063,'1063','PP_1063','1063' from rdb$database 
union all select 1064,'1064','PP_1064','1064' from rdb$database 
union all select 1065,'1065','PP_1065','1065' from rdb$database 
union all select 1066,'1066','PP_1066','1066' from rdb$database 
union all select 1067,'1067','PP_1067','1067' from rdb$database 
union all select 1068,'1068','PP_1068','1068' from rdb$database 
union all select 1069,'1069','PP_1069','1069' from rdb$database 
union all select 1070,'1070','PP_1070','1070' from rdb$database 
union all select 1071,'1071','PP_1071','1071' from rdb$database 
union all select 1072,'1072','PP_1072','1072' from rdb$database 
union all select 1073,'1073','PP_1073','1073' from rdb$database 
union all select 1074,'1074','PP_1074','1074' from rdb$database 
union all select 1075,'1075','PP_1075','1075' from rdb$database 
union all select 1076,'1076','PP_1076','1076' from rdb$database 
union all select 1077,'1077','PP_1077','1077' from rdb$database 
union all select 1078,'1078','PP_1078','1078' from rdb$database 
union all select 1079,'1079','PP_1079','1079' from rdb$database 
union all select 1080,'1080','PP_1080','1080' from rdb$database 
union all select 1081,'1081','PP_1081','1081' from rdb$database 
union all select 1082,'1082','PP_1082','1082' from rdb$database 
union all select 1083,'1083','PP_1083','1083' from rdb$database 
union all select 1084,'1084','PP_1084','1084' from rdb$database 
union all select 1085,'1085','PP_1085','1085' from rdb$database 
union all select 1086,'1086','PP_1086','1086' from rdb$database 
union all select 1087,'1087','PP_1087','1087' from rdb$database 
union all select 1088,'1088','PP_1088','1088' from rdb$database 
union all select 1089,'1089','PP_1089','1089' from rdb$database 
union all select 1090,'1090','PP_1090','1090' from rdb$database 
union all select 1091,'1091','PP_1091','1091' from rdb$database 
union all select 1092,'1092','PP_1092','1092' from rdb$database 
union all select 1093,'1093','PP_1093','1093' from rdb$database 
union all select 1094,'1094','PP_1094','1094' from rdb$database 
union all select 1095,'1095','PP_1095','1095' from rdb$database 
union all select 1096,'1096','PP_1096','1096' from rdb$database 
union all select 1097,'1097','PP_1097','1097' from rdb$database 
union all select 1098,'1098','PP_1098','1098' from rdb$database 
union all select 1099,'1099','PP_1099','1099' from rdb$database 
union all select 1100,'1100','PP_1100','1100' from rdb$database 
union all select 1101,'1101','PP_1101','1101' from rdb$database 
union all select 1102,'1102','PP_1102','1102' from rdb$database 
union all select 1103,'1103','PP_1103','1103' from rdb$database 
union all select 1104,'1104','PP_1104','1104' from rdb$database 
union all select 1105,'1105','PP_1105','1105' from rdb$database 
union all select 1106,'1106','PP_1106','1106' from rdb$database 
union all select 1107,'1107','PP_1107','1107' from rdb$database 
union all select 1108,'1108','PP_1108','1108' from rdb$database 
union all select 1109,'1109','PP_1109','1109' from rdb$database 
union all select 1110,'1110','PP_1110','1110' from rdb$database 
union all select 1111,'1111','PP_1111','1111' from rdb$database 
union all select 1112,'1112','PP_1112','1112' from rdb$database 
union all select 1113,'1113','PP_1113','1113' from rdb$database 
union all select 1114,'1114','PP_1114','1114' from rdb$database 
union all select 1115,'1115','PP_1115','1115' from rdb$database 
union all select 1116,'1116','PP_1116','1116' from rdb$database 
union all select 1117,'1117','PP_1117','1117' from rdb$database 
union all select 1118,'1118','PP_1118','1118' from rdb$database 
union all select 1119,'1119','PP_1119','1119' from rdb$database 
union all select 1120,'1120','PP_1120','1120' from rdb$database 
union all select 1121,'1121','PP_1121','1121' from rdb$database 
union all select 1122,'1122','PP_1122','1122' from rdb$database 
union all select 1123,'1123','PP_1123','1123' from rdb$database 
union all select 1124,'1124','PP_1124','1124' from rdb$database 
union all select 1125,'1125','PP_1125','1125' from rdb$database 
union all select 1126,'1126','PP_1126','1126' from rdb$database 
union all select 1127,'1127','PP_1127','1127' from rdb$database 
union all select 1128,'1128','PP_1128','1128' from rdb$database 
--union all select 1129,'1129','PP_1129','1129' from rdb$database 
)
select parameters.id
     , parameters.usage
     , parameters.identifier
     , parameters.comment
from parameters
where parameters.id <> -1
order by parameters.id

1 个答案:

答案 0 :(得分:0)

如下所示的构造应该让我前进。

params是数据库中的一个表,需要与稀疏填充的人工parameters表进行比较。

使用的技术:

  • 递归查询以在ID
  • 中生成一系列IDs
  • case构造以填充列

在实际查询中,对于所有case值,when都会then validId,而对于需要较少值的列,会有较小的case构造

无错误查询:

select parameters.id
     , parameters.usage
     , parameters.identifier
     , parameters.comment
     , params.ID param_id
     , params.AVALUE
     , params.DESCRIPTION
from
(
  with recursive IDs as
  (
    select 0 as ID from rdb$database
    union all
    select 1 + ID  from IDs where ID < 1000
  )
select
  ID       
  , case 
      when ((ID = 999) or (ID between 0 and 130))
      then 1
      else 0
    end as validId
  , case 
      when ID in 
      ( 
                         3, 
                   12,  13,  14,  15,  16,  17,  18,  19, 
        20,   21,  22,  23,  24,  25,  26,  27,  28,  29, 
        30,   31,  32,  33,  34,  35,  36,  37,  38,  39, 
              41,  42,                  46,  47,  
        50,   51,  52,       54,  55,  56,  57,  58,  59, 
        60,   61,  62,  63,  64,  65,  66,  67,  68,  69, 
        70,   71,  72,                                 79, 
        80,             83,  84,  85,  86,  87, 88,   89, 
        90,   91,  92,  93,  94,  95,  96,  97, 98,   99, 
        100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 
        110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 
        120, 121, 122, 123, 124, 125, 126, 127,      129, 
        999
      )
      then 'used'
      else 'unused'
    end as usage
  , '' as identifier
  , '' as comment
from
  IDs
) parameters              
full outer join params
  on params.ID = parameters.id
where parameters.validId = 1
order by params.id, parameters.id