0

在此处输入图像描述

我有四列分别命名为 start_time、end_time、class_time 和 class_duration 并在一行中插入整个记录,例如 start_time 应该是 0900AM,end_time 应该是 1000AM,class_time 应该是 0930AM:0930AM,class_duration 应该是 60,即 60 分钟

4

1 回答 1

1

Shuttle 项与任何选择列表项一样,使用其select语句必须恰好包含两列的值列表:

  • 显示值
  • 返回值

显示值是用户看到的,返回值是你存储到数据库中的,通常是某种 ID。

如果要显示所有这 4 个值,则必须将它们连接起来,例如

select 'Start time: ' || start_time ||
       'End time:   ' || end_time   ||
       'Class time: ' || class_time ||
       'Duration:   ' || duration       as display_value,
       --
       something  as return_value
from your_table

如果没有什么“特殊”要返回(即没有 ID),您可以对显示值和返回值使用相同的连接列。


[编辑:如何将数据插入表中?]

假设这是目标表:

SQL> create table schedule
  2    (id       number,
  3     cstart   varchar2(6),
  4     cend     varchar2(6),
  5     cclass   varchar2(6),
  6     duration varchar2(6));

Table created.

然后假设穿梭项目包含的值如下所示:

Start=0900AM,End=1000AM,Class=1030AM,Duration=60

值用逗号分隔(因为,如果您在穿梭项目中选择多个值,它们将用冒号分隔 - 您宁愿选择其他内容作为分隔符)。

这些多个值如下所示:1:2:4:8代表 4 个穿梭值。

现在,插入:查看代码中的注释:

SQL> insert into schedule (id, cstart, cend, cclass, duration)
  2  with shuttle (col) as
  3    (select 'Start=0900AM,End=1000AM,Class=1030AM,Duration=60' ||':'||
  4            'Start=1100AM,End=1130AM,Class=1015AM,Duration=30' from dual
  5    ),
  6  tsplit as
  7    -- split shuttle value into rows. For multiple selection, values
  8    -- are separated by colon. Therefore, you'll have to use something
  9    -- different than that - I used the "=" sign and "," to separate columns
 10    (select level lvl,
 11            regexp_substr(col, '[^:]+', 1, level) val
 12     from shuttle
 13     connect by level <= regexp_count(col, ':') + 1
 14    )
 15  -- now split each value to columns. They are separated by commas
 16  select lvl,
 17    regexp_substr(val, '\w+', 1, 2) cstart,
 18    regexp_substr(val, '\w+', 1, 4) cend,
 19    regexp_substr(val, '\w+', 1, 6) cclass,
 20    regexp_substr(val, '\w+', 1, 8) cduration
 21  from tsplit;

2 rows created.

SQL>
SQL> select * From schedule;

        ID CSTART CEND   CCLASS DURATI
---------- ------ ------ ------ ------
         1 0900AM 1000AM 1030AM 60
         2 1100AM 1130AM 1015AM 30

SQL>

WITH factoring 条款将 - 在你的情况下 - 是穿梭项目的价值。

就是这样,我想。

于 2019-12-08T21:21:06.433 回答