简体   繁体   中英

how to use insert statement with where clause from diff table in oracle without using plsql

I want to insert like below

If 
   select *from class where student_name='A' ;
then
   Insert into student (Studnetname varchar2,rollno integer) values('A',1);

without using PL/SQL

Wouldn't that be as simple as

insert into student (studentname, rollno)
select student_name, rollno
from class
where student_name = 'A'
  and rollno = 1;

If such a student (whose values satisfy the WHERE clause) exists, a row will be inserted. Otherwise, nothing will happen.

I want direct insert statement without select

The closest you will get to meeting this requirement is something like this:

Insert into student (Studnetname, rollno) 
select 'A',1
from dual 
where exists (select null from class where student_name = 'A' )
;

This will insert one row into STUDENT if there is at least one record in CLASS matching the given criterion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM