开发者

How do I insert data into a object relational table with multiple ref in the schema

I have a table with a schema of Table(number, ref, ref, varchar2, varchar2,...).

How would I insert a row of data into this table?

When I do:

insert into table 
values (1, select ref(p), ref(d), '239 F.3d 1343', '35 USC § 283', ... 
           from plaintiff p, defendant d
            where p.name='name1' and d.name='name2');

I get a "missing expressio开发者_运维技巧n" error.

If I do:

insert into table
1, select ref(p), ref(d), ... 
from plaintiff p, defendant 
where p.name=...;

I get a "missing keyword VALUES" error.


Your syntax on the insert is off. Try:

insert into table  
(select 1, ref(p), ref(d), '239 F.3d 1343', '35 USC § 283', ... 
   from plaintiff p, defendant d where p.name='name1' and d.name='name2');

In general, it's a good practice to explicitly mention the columns you're inserting into as well, to avoid problems later if the column order changes, as well as to self-document the code:

insert into table (col1, col2, col3, ...) 
(select 1, ref(p), ref(d), '239 F.3d 1343', '35 USC § 283', ... 
   from plaintiff p, defendant d where p.name='name1' and d.name='name2');


Given a table like this ...

SQL> create table cases
  2      (case_no number
  3       , plaintiff_ref   REF person_t SCOPE IS plaintiffs
  4       , defendant_ref   REF person_t SCOPE IS defendants
  5       , col1 varchar2(30)
  6       , col2 varchar2(30)
  7       )
  8  /

Table created.

SQL>

We can populate it like this ...

SQL> insert into cases
  2  select 1, ref(p), ref(d), '239 F.3d 1343', '35 USC § 283'
  3  from plaintiffs p, defendants d
  4  where p.id = 1000
  5  and d.id=2000
  6  /

1 row created.

SQL>

... or like this ...

SQL> declare
  2      p_ref REF person_t;
  3      d_ref REF person_t;
  4  begin
  5      select ref(p) into p_ref
  6      from plaintiffs p
  7      where p.id = 1000;
  8      select ref(d) into d_ref
  9      from defendants d
 10      where d.id = 2000;
 11
 12      insert into cases
 13      values
 14      (2, p_ref, d_ref, 'YYT A.2e 789', '26 FTW § 169');
 15  end;
 16  /

PL/SQL procedure successfully completed.

SQL>

The REFs are eye-wateringly long:

SQL> select * from cases 2 /

   CASE_NO
----------
PLAINTIFF_REF
--------------------------------------------------------------------------
DEFENDANT_REF
--------------------------------------------------------------------------
COL1                           COL2
------------------------------ ------------------------------
         1
0000220208771EFF0FAD71409F85A448C831C0C7B041CAA1874D514FDC9D18EF12DA22C12D
0000220208981D65F90A004146A1A390DC1048858777ECAC51136743B39A75F37D22DC1379
239 F.3d 1343                  35 USC § 283

         2
0000220208771EFF0FAD71409F85A448C831C0C7B041CAA1874D514FDC9D18EF12DA22C12D
0000220208981D65F90A004146A1A390DC1048858777ECAC51136743B39A75F37D22DC1379
YYT A.2e 789                   26 FTW § 169


SQL>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜