0

The example would be:

Declare
stringtest VARCHAR2(10):='yes';
Begin
INSERT WHEN (condition) THEN   -- the condition has to be like stringtest='yes';
INTO ...
Select ...
End;

But I can't do it like

Begin
INSERT WHEN TRUE THEN
INTO ...
Select ...
End;

What is the easiest way to let the condition be true?

2
  • 1
    move you condition to the Select... where clause. Commented May 2, 2015 at 9:54
  • insert into some table select ... where stringest = 'yes' Commented May 2, 2015 at 9:55

2 Answers 2

1

the easiest (and the right) way is to move condition into select 'where' clause. If select returns (selects) nothing, then nothing will be inserted. i.e.

insert into table(col1,col2)
select 'colval1', 'colval2'
from dual
where 2=3

will select nothing and insert nothing

Sign up to request clarification or add additional context in comments.

Comments

1

Oracle SQL (as opposed to PL/SQL) doesn't have a boolean type, so TRUE isn't recognised in the SQL statement (standalone, or if it happens to be within a PL/SQL block).

You can use a dummy expression that always evaluates to true, e.g.:

INSERT WHEN (1=1) THEN
INTO ...
Select ...

Quick SQL Fiddle demo.

Unless this is a 'fixed' part of a larger INSERT ALL construct, the clause seems rather redundant if you know it always has to be true; if you aren't evaluating something real, the WHEN clause isn't adding anything useful. If you want to use it as a flag to control the behaviour then your stringtest approach would probably be easier since you can change that setting in one place - in the declare section - rather than hunting through and changing all the individual WHEN clauses.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.