免责声明:作为一个只使用数据库的人,请耐心等待。(大多数时候,我会在工作中进行C ++编程,但是每个奇数月我都需要在Oracle数据库中搜索/修复/添加某些内容。)
我一再需要编写复杂的SQL查询,无论是针对临时查询还是针对应用程序内置的查询,其中大部分查询只是重复的“代码”。
用传统的编程语言编写此类可憎的代码会给您带来深重的麻烦,但是我(I)尚未找到任何体面的技术来防止SQL查询代码重复。
编辑: 1,我要感谢对我的原始示例进行了出色改进的回答者。但是,这个问题与我的示例无关。这是关于SQL查询中的重复性。这样,到目前为止的答案(JackP,Leigh)在显示可以通过编写更好的查询来减少重复性方面做得很好。但是即使如此,您仍然面临着一些重复性,这些重复性显然无法消除:这总是使SQL困扰我。在“传统”编程语言中,我可以进行大量重构以最大程度地减少代码中的重复性,但是对于SQL,似乎没有(?)工具允许这样做,除了以重复的方式编写较少的语句。
请注意,我再次删除了Oracle标记,因为如果没有数据库或脚本语言可以支持更多功能,我将非常感兴趣。
这是我今天拼凑而成的一颗宝石。它基本上报告单个表的一组列中的差异。请略读以下代码,特别是。最后是大型查询。我将在下面继续。
--
-- Create Table to test queries
--
CREATE TABLE TEST_ATTRIBS (
id NUMBER PRIMARY KEY,
name VARCHAR2(300) UNIQUE,
attr1 VARCHAR2(2000),
attr2 VARCHAR2(2000),
attr3 INTEGER,
attr4 NUMBER,
attr5 VARCHAR2(2000)
);
--
-- insert some test data
--
insert into TEST_ATTRIBS values ( 1, 'Alfred', 'a', 'Foobar', 33, 44, 'e');
insert into TEST_ATTRIBS values ( 2, 'Batman', 'b', 'Foobar', 66, 44, 'e');
insert into TEST_ATTRIBS values ( 3, 'Chris', 'c', 'Foobar', 99, 44, 'e');
insert into TEST_ATTRIBS values ( 4, 'Dorothee', 'd', 'Foobar', 33, 44, 'e');
insert into TEST_ATTRIBS values ( 5, 'Emilia', 'e', 'Barfoo', 66, 44, 'e');
insert into TEST_ATTRIBS values ( 6, 'Francis', 'f', 'Barfoo', 99, 44, 'e');
insert into TEST_ATTRIBS values ( 7, 'Gustav', 'g', 'Foobar', 33, 44, 'e');
insert into TEST_ATTRIBS values ( 8, 'Homer', 'h', 'Foobar', 66, 44, 'e');
insert into TEST_ATTRIBS values ( 9, 'Ingrid', 'i', 'Foobar', 99, 44, 'e');
insert into TEST_ATTRIBS values (10, 'Jason', 'j', 'Bob', 33, 44, 'e');
insert into TEST_ATTRIBS values (12, 'Konrad', 'k', 'Bob', 66, 44, 'e');
insert into TEST_ATTRIBS values (13, 'Lucas', 'l', 'Foobar', 99, 44, 'e');
insert into TEST_ATTRIBS values (14, 'DUP_Alfred', 'a', 'FOOBAR', 33, 44, 'e');
insert into TEST_ATTRIBS values (15, 'DUP_Chris', 'c', 'Foobar', 66, 44, 'e');
insert into TEST_ATTRIBS values (16, 'DUP_Dorothee', 'd', 'Foobar', 99, 44, 'e');
insert into TEST_ATTRIBS values (17, 'DUP_Gustav', 'X', 'Foobar', 33, 44, 'e');
insert into TEST_ATTRIBS values (18, 'DUP_Homer', 'h', 'Foobar', 66, 44, 'e');
insert into TEST_ATTRIBS values (19, 'DUP_Ingrid', 'Y', 'foo', 99, 44, 'e');
insert into TEST_ATTRIBS values (20, 'Martha', 'm', 'Bob', 33, 88, 'f');
-- Create comparison view
CREATE OR REPLACE VIEW TA_SELFCMP as
select
t1.id as id_1, t2.id as id_2, t1.name as name, t2.name as name_dup,
t1.attr1 as attr1_1, t1.attr2 as attr2_1, t1.attr3 as attr3_1, t1.attr4 as attr4_1, t1.attr5 as attr5_1,
t2.attr1 as attr1_2, t2.attr2 as attr2_2, t2.attr3 as attr3_2, t2.attr4 as attr4_2, t2.attr5 as attr5_2
from TEST_ATTRIBS t1, TEST_ATTRIBS t2
where t1.id <> t2.id
and t1.name <> t2.name
and t1.name = REPLACE(t2.name, 'DUP_', '')
;
-- NOTE THIS PIECE OF HORRIBLE CODE REPETITION --
-- Create comparison report
-- compare 1st attribute
select 'attr1' as Different,
id_1, id_2, name, name_dup,
CAST(attr1_1 AS VARCHAR2(2000)) as Val1, CAST(attr1_2 AS VARCHAR2(2000)) as Val2
from TA_SELFCMP
where attr1_1 <> attr1_2
or (attr1_1 is null and attr1_2 is not null)
or (attr1_1 is not null and attr1_2 is null)
union
-- compare 2nd attribute
select 'attr2' as Different,
id_1, id_2, name, name_dup,
CAST(attr2_1 AS VARCHAR2(2000)) as Val1, CAST(attr2_2 AS VARCHAR2(2000)) as Val2
from TA_SELFCMP
where attr2_1 <> attr2_2
or (attr2_1 is null and attr2_2 is not null)
or (attr2_1 is not null and attr2_2 is null)
union
-- compare 3rd attribute
select 'attr3' as Different,
id_1, id_2, name, name_dup,
CAST(attr3_1 AS VARCHAR2(2000)) as Val1, CAST(attr3_2 AS VARCHAR2(2000)) as Val2
from TA_SELFCMP
where attr3_1 <> attr3_2
or (attr3_1 is null and attr3_2 is not null)
or (attr3_1 is not null and attr3_2 is null)
union
-- compare 4th attribute
select 'attr4' as Different,
id_1, id_2, name, name_dup,
CAST(attr4_1 AS VARCHAR2(2000)) as Val1, CAST(attr4_2 AS VARCHAR2(2000)) as Val2
from TA_SELFCMP
where attr4_1 <> attr4_2
or (attr4_1 is null and attr4_2 is not null)
or (attr4_1 is not null and attr4_2 is null)
union
-- compare 5th attribute
select 'attr5' as Different,
id_1, id_2, name, name_dup,
CAST(attr5_1 AS VARCHAR2(2000)) as Val1, CAST(attr5_2 AS VARCHAR2(2000)) as Val2
from TA_SELFCMP
where attr5_1 <> attr5_2
or (attr5_1 is null and attr5_2 is not null)
or (attr5_1 is not null and attr5_2 is null)
;
如您所见,生成“差异报告”的查询使用相同的SQL SELECT块5次(很可能是42次!)。这让我感到十分震惊(毕竟,我写了代码之后,我可以这么说了),但是我还没有找到任何好的解决方案。
如果这将是一些实际应用程序代码中的查询,我可以编写一个函数将此查询拼凑在一起作为字符串,然后将查询作为字符串执行。
- ->构建字符串非常可怕,而且测试和维护都很可怕。如果“应用程序代码”是用PL / SQL之类的语言编写的,那会感到很不舒服,很伤人。
另外,如果从PL / SQL或类似工具中使用,我猜想有一些程序上的方法可以使此查询更易于维护。
- ->将可以在单个查询中表达的内容展开到程序步骤中,只是为了防止代码重复,这也是错误的。
如果需要将此查询作为数据库中的视图,那么据我所知,除了我上面发布的内容,实际上除了维护视图定义外别无其他方法。(!!?)
- ->实际上,一旦与上述声明相距不远,我实际上必须对2页视图定义进行一些维护。显然,要更改此视图中的任何内容,都需要在视图定义上进行正则表达式文本搜索,以确定是否在另一行中使用了相同的子语句,是否需要在此处更改。
那么,随着标题的发展,有什么技术可以防止不得不写这种可憎的东西?
UNION ALL
。通常,UNION
如果没有这种处理,ALL
通常会导致为所需的排序操作假脱机到临时存储区(因为有效地UNION ALL
跟随了“ UNION” ,DISTINCT
这意味着要进行排序),因此在某些情况下,性能差异可能很大。