Home » SQL & PL/SQL » SQL & PL/SQL » Conditional Insert???
Conditional Insert??? [message #28821] Fri, 23 January 2004 11:19 Go to next message
rdx
Messages: 2
Registered: June 2003
Junior Member
Is there a way to have an INSERT statement with a where clause?  I need to insert  to a table based only when a certain condition is satisfied on another table and  I don't have the luxury of using multiple statement (otherwise would have been a piece of cake using PLSQL )  .. Need to do it in a single statement .. so what are my choices ??

 

Thanks in advance

rdx

 
Re: Conditional Insert??? [message #28822 is a reply to message #28821] Fri, 23 January 2004 11:26 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Is this a single-row insert? What is the condition? It would help if you could provide some pseudo-code.
Re: Conditional Insert??? [message #28827 is a reply to message #28821] Sat, 24 January 2004 07:38 Go to previous message
William Robertson
Messages: 1643
Registered: August 2003
Location: London, UK
Senior Member
You could try something like:
INSERT INTO sometable
( col1, col2, col3 )
SELECT a, b, c
FROM   dual
WHERE  EXISTS
       ( SELECT 1
         FROM   othertable
         WHERE  somecondition );
or
INSERT INTO sometable
( col1, col2, col3 )
SELECT a, b, c
FROM   othertable
WHERE  somecondition
AND    rownum = 1;

In 9i you also have a CASE-like syntax available with INSERT ALL/FIRST, though that doesn't sound like quite what you want here.
Previous Topic: How to create table with storage options
Next Topic: trigger problem
Goto Forum:
  


Current Time: Tue Aug 26 18:18:25 CDT 2025