插入0行 - 如何解决此问题?

时间:2012-03-09 14:03:49

标签: sql oracle insert

我试图通过SQL Developer v3.0.04

将多行插入到SQL,Oracle表中

数据库是由Uni设置的,所以我不知道它是什么版本。

在线查看之后,我已经提出了下面的代码,但它不会插入任何数据。我用一行测试了插件,这没问题。我错过了什么?

Insert all 
  Into Patient Values
    ('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456)
  Into Patient Values
    ('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456)
  Into Patient Values
    ('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456)

Select * from Patient;

3 个答案:

答案 0 :(得分:6)

INSERT ALL有两种不同的用途。一种是将所选列的不同子集插入表中。另一种是根据某些标准将行指向不同的行。在这两种情况下,数据都来自SELECT子句而不是来自VALUES。 See the examples in the documentation

通常,您只需在单个PL / SQL块中编写多个INSERT语句。像

这样的东西
begin
  Insert Into Patient Values
    ('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456);
  Insert Into Patient Values
    ('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456);
  Insert Into Patient Values
    ('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456);
end;
/

如果您真的想在单个SQL语句中执行此操作,则可以执行INSERT ... SELECT,但这通常比使用三个单独的语句更复杂。

insert into patient
  select *
    from (select '101' id, '1 house' addr, null col1, 'Kingston' city, ...
            from dual
          union all
          select '102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456
            from dual
          union all
          select '103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456
            from dual)

我还提醒您使用正确的数据类型并在INSERT语句中指定列名称。我猜测,例如,Patient表的第一列是某种PatientID,定义为NUMBER。如果是这样,您真的想要插入数字而不是字符串。类似地,第七列的值类似于' 15 / may / 1990'可能在表中定义为DATE。如果是这样,您的INSERT应该通过使用特定格式掩码显式调用DATE或使用ANSI日期格式(即TO_DATE)插入date '1980-01-10'而不是字符串。如果您希望最后一列保留前导0,则需要确保数据库中的列定义为VARCHAR2,并且您要插入字符串而不是数字。

答案 1 :(得分:1)

有趣。您可以在尝试时使用insert all插入多行。并不是说我会建议这样做而不是贾斯汀建议的解决方案。

多表插入的syntax diagram表示始终需要子查询。但是,您不必使用子查询的任何结果:

SQL> drop table t;

Table dropped.

SQL> create table t (c1 number, c2 varchar2(10));

Table created.

SQL> insert all into t (c1, c2) values (1, 'one')
  2      into t (c1, c2) values (2, 'two')
  3  select * from dual;

2 rows created.

SQL> select * from t;

        C1 C2
---------- ----------
         1 one
         2 two

两个into ... values(...)将导致在子查询中每行插入两行:

SQL> insert all into t (c1, c2) values (1, 'one')
  2      into t (c1, c2) values (2, 'two')
  3  select * from dual
  4  connect by level <= 10;

20 rows created.

答案 2 :(得分:0)

替换此查询[从Patient中选择*; ]与 从双选*;

双表是一个虚拟表,它在我们的模式中不存在,但可以作为Oracle透视图的一部分存在于RAM中而不是存储中

全部插入 融入患者价值观 (“ 101”,“ 1个房子”,空,“金斯顿”,“萨里”,“ KT1 1XX”,“ 10 / jan / 1980”,“ m”,01452987456) 融入患者价值观 (“ 102”,“ 2蛋rd”,“ vail”,“ guildford”,“ Surrey”,“ GU1 1LL”,“ 05 / dec / 1985”,“ m”,01452987456) 融入患者价值观 (“ 103”,“ 6站路”,空,“吉尔福德”,“萨里”,“ GU1 2XX”,“ 15 / may / 1990”,“ f”,01452987456)

从双选*;

相关问题