使用 unpivot 从远程 table 插入
Insert from remote table using unpivot
因此,在我们的组织中有一个 table 用于安排时间并从中报告数据。日程安排行在一周中的每一天都有一列,其值是 'Y' 或 'N',具体取决于当天是否有会议。我设法使用 UNPIVOT 以简单的方式完成此操作 select,但不幸的是,我们的需求比这更复杂。
我需要插入一个本地 table 并从远程 table 上的调度 table 查询 DB_Link,这是不可透视的,比如:
SELECT *
FROM (SELECT class_nbr,
TERM,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM schedule_tbl) sched
UNPIVOT (wkDay FOR DayWeek IN (MON, TUES, WED, THURS, FRI, SAT, SUN)) piv
WHERE piv.WkDay = 'Y'
同样,当我 运行 它在 Sql 开发人员中针对 schedule_tbl 所在的服务器时,这工作正常。我得到类似
的结果
Class_nbr TERM DayWeek
1234 Fall MON
1234 Fall Wed
....
然而,令人惊讶的是,当我尝试从
等不同的服务器插入结果时
INSERT INTO other_table (class_nbr, TERM, DayWeek)
SELECT *
FROM (SELECT class_nbr,
TERM,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM schedule_tbl@db_link) sched
UNPIVOT (wkDay FOR DayWeek IN (MON, TUES, WED, THURS, FRI, SAT, SUN)) piv
WHERE piv.WkDay = 'Y'
发生的情况是仅插入 DayWeek = 'MON' 的行。应插入的实际星期一行数是。我不确定这是在哪里崩溃的。如果我更改 UNPIVOT 列的顺序,使 TUES 成为第一个,则仅插入星期二的行(同样,正确的数字)。
编辑:我想我可以在这里分享我的实际代码。请记住,如果我将这个确切的查询带到远程数据库并删除 dblink,我得到的不仅仅是星期一的行:
SELECT '%processinstance',
0 ,
crse_id ,
crse_offer_nbr ,
strm ,
session_code ,
class_section ,
subject ,
institution ,
facility_id ,
' ' ,
' ' ,
'0001484' ,
'4' ,
PDB_DE0120_TRM_ID ,
subject || catalog_nbr ,
strm || class_nbr ,
to_char ( start_dt , 'YYYYMMDD' ) ,
to_char ( end_dt , 'YYYYMMDD' ) ,
DECODE(DAYWEEK,'MON', 'M', 'TUES','T', 'WED','W', 'THURS','R', 'FRI','F', 'SAT','S', 'SUN','X'),
nvl ( to_char ( meeting_time_start ,'HH24MI' ) ,'9999' ) ,
nvl ( to_char ( meeting_time_end ,'HH24MI' ) ,'9999' ) ,
' ' ,
' ' ,
' ' ,
0 ,
'S' ,
'C' ,
decode ( instruction_mode ,'HB' ,'X' ,'DL' ,'X' ,'N' ) ,
' ' ,
' ' ,
' ' ,
' ' ,
' ' ,
'E' ,
SYSDATE ,
' ' ,
SYSDATE,
' ' ,
' '
FROM (SELECT A.crse_id ,
A.crse_offer_nbr ,
A.strm ,
A.session_code ,
A.class_section ,
B.institution ,
A.facility_id ,
B.instruction_mode,
B.SUBJECT,
B.CATALOG_NBR,
A.meeting_time_start,
A.meeting_time_end,
B.START_DT,
B.END_DT,
B.CLASS_NBR,
C.PDB_DE0120_TRM_ID,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM PS_CLASS_MTG_PAT@CSDV1 A
INNER JOIN PS_CLASS_TBL@CSDV1 B
ON A.STRM = B.STRM AND
A.CRSE_ID = B.CRSE_ID AND
A.CLASS_SECTION = B.CLASS_SECTION
INNER JOIN PS_FSC_SRPT_TRM_VW@CSDV1 C
ON C.STRM = B.STRM AND
C.ACAD_CAREER = 'CRED'
WHERE A.STRM = '2182' AND
(B.CLASS_STAT <> 'X' OR
(B.ENRL_TOT > 0 AND
B.ENRL_STAT <> 'C')) AND
B.acad_group NOT IN ( '11','12','13','23','80','99' )) B
UNPIVOT EXCLUDE NULLS (wkDay FOR DayWeek IN ( MON AS 'MON', TUES AS 'TUES', WED AS 'WED', THURS AS 'THURS', FRI AS 'FRI', SAT AS 'SAT', SUN AS 'SUN')) piv
WHERE piv.wkDay = 'Y';
第二次更新:根据要求,我粘贴更正后的查询:
SELECT '%processinstance',
0 ,
crse_id ,
crse_offer_nbr ,
strm ,
session_code ,
class_section ,
subject ,
institution ,
facility_id ,
' ' ,
' ' ,
'0001484' ,
'4' ,
PDB_DE0120_TRM_ID ,
subject || catalog_nbr ,
strm || class_nbr ,
to_char ( start_dt , 'YYYYMMDD' ) ,
to_char ( end_dt , 'YYYYMMDD' ) ,
DECODE(DAYWEEK,'MON', 'M', 'TUES','T', 'WED','W', 'THURS','R', 'FRI','F', 'SAT','S', 'SUN','X'),
nvl ( to_char ( meeting_time_start ,'HH24MI' ) ,'9999' ) ,
nvl ( to_char ( meeting_time_end ,'HH24MI' ) ,'9999' ) ,
' ' ,
' ' ,
' ' ,
0 ,
'S' ,
'C' ,
decode ( instruction_mode ,'HB' ,'X' ,'DL' ,'X' ,'N' ) ,
' ' ,
' ' ,
' ' ,
' ' ,
' ' ,
'E' ,
' ' ,
' ' ,
' '
FROM (SELECT A.crse_id ,
A.crse_offer_nbr ,
A.strm ,
A.session_code ,
A.class_section ,
B.institution ,
A.facility_id ,
B.instruction_mode,
B.SUBJECT,
B.CATALOG_NBR,
A.meeting_time_start,
A.meeting_time_end,
B.START_DT,
B.END_DT,
B.CLASS_NBR,
C.PDB_DE0120_TRM_ID,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM PS_CLASS_MTG_PAT@CSDV1 A
INNER JOIN PS_CLASS_TBL@CSDV1 B
ON A.STRM = B.STRM AND
A.CRSE_ID = B.CRSE_ID AND
A.CLASS_SECTION = B.CLASS_SECTION
INNER JOIN PS_FSC_SRPT_TRM_VW@CSDV1 C
ON C.STRM = B.STRM AND
C.ACAD_CAREER = 'CRED'
WHERE A.STRM = '2182' AND
(B.CLASS_STAT <> 'X' OR
(B.ENRL_TOT > 0 AND
B.ENRL_STAT <> 'C')) AND
B.acad_group NOT IN ( '11','12','13','23','80','99' )) B
UNPIVOT EXCLUDE NULLS (wkDay FOR DayWeek IN ( MON AS 'MON', TUES AS 'TUES', WED AS 'WED', THURS AS 'THURS', FRI AS 'FRI', SAT AS 'SAT', SUN AS 'SUN')) piv
WHERE piv.wkDay = 'Y';
这不是一个完全令人满意的解决方案,但它对我有用。如果我从我的 select 子句中删除两个表达式(any 两列),这个问题就会消失。这显然是一个以前未知的涉及 DB Links 的 Oracle 问题,就好像我在表所在的远程服务器上进行了相同的查询并且 运行 它不存在,并且 Oracle 文档中没有任何关于 DB Links 的内容对这个。我会在某个时候尝试与他们一起提出。
因此,在我们的组织中有一个 table 用于安排时间并从中报告数据。日程安排行在一周中的每一天都有一列,其值是 'Y' 或 'N',具体取决于当天是否有会议。我设法使用 UNPIVOT 以简单的方式完成此操作 select,但不幸的是,我们的需求比这更复杂。
我需要插入一个本地 table 并从远程 table 上的调度 table 查询 DB_Link,这是不可透视的,比如:
SELECT *
FROM (SELECT class_nbr,
TERM,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM schedule_tbl) sched
UNPIVOT (wkDay FOR DayWeek IN (MON, TUES, WED, THURS, FRI, SAT, SUN)) piv
WHERE piv.WkDay = 'Y'
同样,当我 运行 它在 Sql 开发人员中针对 schedule_tbl 所在的服务器时,这工作正常。我得到类似
的结果Class_nbr TERM DayWeek
1234 Fall MON
1234 Fall Wed
....
然而,令人惊讶的是,当我尝试从
等不同的服务器插入结果时INSERT INTO other_table (class_nbr, TERM, DayWeek)
SELECT *
FROM (SELECT class_nbr,
TERM,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM schedule_tbl@db_link) sched
UNPIVOT (wkDay FOR DayWeek IN (MON, TUES, WED, THURS, FRI, SAT, SUN)) piv
WHERE piv.WkDay = 'Y'
发生的情况是仅插入 DayWeek = 'MON' 的行。应插入的实际星期一行数是。我不确定这是在哪里崩溃的。如果我更改 UNPIVOT 列的顺序,使 TUES 成为第一个,则仅插入星期二的行(同样,正确的数字)。
编辑:我想我可以在这里分享我的实际代码。请记住,如果我将这个确切的查询带到远程数据库并删除 dblink,我得到的不仅仅是星期一的行:
SELECT '%processinstance',
0 ,
crse_id ,
crse_offer_nbr ,
strm ,
session_code ,
class_section ,
subject ,
institution ,
facility_id ,
' ' ,
' ' ,
'0001484' ,
'4' ,
PDB_DE0120_TRM_ID ,
subject || catalog_nbr ,
strm || class_nbr ,
to_char ( start_dt , 'YYYYMMDD' ) ,
to_char ( end_dt , 'YYYYMMDD' ) ,
DECODE(DAYWEEK,'MON', 'M', 'TUES','T', 'WED','W', 'THURS','R', 'FRI','F', 'SAT','S', 'SUN','X'),
nvl ( to_char ( meeting_time_start ,'HH24MI' ) ,'9999' ) ,
nvl ( to_char ( meeting_time_end ,'HH24MI' ) ,'9999' ) ,
' ' ,
' ' ,
' ' ,
0 ,
'S' ,
'C' ,
decode ( instruction_mode ,'HB' ,'X' ,'DL' ,'X' ,'N' ) ,
' ' ,
' ' ,
' ' ,
' ' ,
' ' ,
'E' ,
SYSDATE ,
' ' ,
SYSDATE,
' ' ,
' '
FROM (SELECT A.crse_id ,
A.crse_offer_nbr ,
A.strm ,
A.session_code ,
A.class_section ,
B.institution ,
A.facility_id ,
B.instruction_mode,
B.SUBJECT,
B.CATALOG_NBR,
A.meeting_time_start,
A.meeting_time_end,
B.START_DT,
B.END_DT,
B.CLASS_NBR,
C.PDB_DE0120_TRM_ID,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM PS_CLASS_MTG_PAT@CSDV1 A
INNER JOIN PS_CLASS_TBL@CSDV1 B
ON A.STRM = B.STRM AND
A.CRSE_ID = B.CRSE_ID AND
A.CLASS_SECTION = B.CLASS_SECTION
INNER JOIN PS_FSC_SRPT_TRM_VW@CSDV1 C
ON C.STRM = B.STRM AND
C.ACAD_CAREER = 'CRED'
WHERE A.STRM = '2182' AND
(B.CLASS_STAT <> 'X' OR
(B.ENRL_TOT > 0 AND
B.ENRL_STAT <> 'C')) AND
B.acad_group NOT IN ( '11','12','13','23','80','99' )) B
UNPIVOT EXCLUDE NULLS (wkDay FOR DayWeek IN ( MON AS 'MON', TUES AS 'TUES', WED AS 'WED', THURS AS 'THURS', FRI AS 'FRI', SAT AS 'SAT', SUN AS 'SUN')) piv
WHERE piv.wkDay = 'Y';
第二次更新:根据要求,我粘贴更正后的查询:
SELECT '%processinstance',
0 ,
crse_id ,
crse_offer_nbr ,
strm ,
session_code ,
class_section ,
subject ,
institution ,
facility_id ,
' ' ,
' ' ,
'0001484' ,
'4' ,
PDB_DE0120_TRM_ID ,
subject || catalog_nbr ,
strm || class_nbr ,
to_char ( start_dt , 'YYYYMMDD' ) ,
to_char ( end_dt , 'YYYYMMDD' ) ,
DECODE(DAYWEEK,'MON', 'M', 'TUES','T', 'WED','W', 'THURS','R', 'FRI','F', 'SAT','S', 'SUN','X'),
nvl ( to_char ( meeting_time_start ,'HH24MI' ) ,'9999' ) ,
nvl ( to_char ( meeting_time_end ,'HH24MI' ) ,'9999' ) ,
' ' ,
' ' ,
' ' ,
0 ,
'S' ,
'C' ,
decode ( instruction_mode ,'HB' ,'X' ,'DL' ,'X' ,'N' ) ,
' ' ,
' ' ,
' ' ,
' ' ,
' ' ,
'E' ,
' ' ,
' ' ,
' '
FROM (SELECT A.crse_id ,
A.crse_offer_nbr ,
A.strm ,
A.session_code ,
A.class_section ,
B.institution ,
A.facility_id ,
B.instruction_mode,
B.SUBJECT,
B.CATALOG_NBR,
A.meeting_time_start,
A.meeting_time_end,
B.START_DT,
B.END_DT,
B.CLASS_NBR,
C.PDB_DE0120_TRM_ID,
MON,
TUES,
WED,
THURS,
FRI,
SAT,
SUN
FROM PS_CLASS_MTG_PAT@CSDV1 A
INNER JOIN PS_CLASS_TBL@CSDV1 B
ON A.STRM = B.STRM AND
A.CRSE_ID = B.CRSE_ID AND
A.CLASS_SECTION = B.CLASS_SECTION
INNER JOIN PS_FSC_SRPT_TRM_VW@CSDV1 C
ON C.STRM = B.STRM AND
C.ACAD_CAREER = 'CRED'
WHERE A.STRM = '2182' AND
(B.CLASS_STAT <> 'X' OR
(B.ENRL_TOT > 0 AND
B.ENRL_STAT <> 'C')) AND
B.acad_group NOT IN ( '11','12','13','23','80','99' )) B
UNPIVOT EXCLUDE NULLS (wkDay FOR DayWeek IN ( MON AS 'MON', TUES AS 'TUES', WED AS 'WED', THURS AS 'THURS', FRI AS 'FRI', SAT AS 'SAT', SUN AS 'SUN')) piv
WHERE piv.wkDay = 'Y';
这不是一个完全令人满意的解决方案,但它对我有用。如果我从我的 select 子句中删除两个表达式(any 两列),这个问题就会消失。这显然是一个以前未知的涉及 DB Links 的 Oracle 问题,就好像我在表所在的远程服务器上进行了相同的查询并且 运行 它不存在,并且 Oracle 文档中没有任何关于 DB Links 的内容对这个。我会在某个时候尝试与他们一起提出。