0

I just want to define and then use a variable in Oracle SQL in SQL Developer (Version 18.1.0.095) (Background: We have some legacy production SQL script that are run periodically through jBPM).

In SQL Developer, I want to avoid the 'Enter Binds' prompt window.

Surprisingly, I found this is not that straight-forward to do. Am I missing something obvious?

CREATE TABLE Test_Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255)
);

INSERT INTO Test_Persons
    (PersonID,LastName,FirstName)
    values(1,'LN_1','FN_1');

INSERT INTO Test_Persons
    (PersonID,LastName,FirstName)
    values(2,'LN_2','FN_2');

commit;

// --- method 1: not working and shows the prompt window (which I try to avoid)--------
var last_name_input varchar2(20);
select 'LN_2' into :last_name_input from dual;
select * from Test_Persons tp where tp.LASTNAME = :last_name_input;

// --- method 2: not working and shows the prompt window (which I try to avoid) --------
declare
    var last_name_input varchar2(20);
    var first_name_output varchar2(20);
begin
    select 'LN_2' into :last_name_input from dual;
    select tp.FIRSTNAME into first_name_output from Test_Persons tp where tp.LASTNAME = :last_name_input;
--    dbms_output.Put_line('here: ' || :first_name_output);
end;

// --- method 3: working and shows the prompt window (which I try to avoid) --------
var last_name_input varchar2(20);
exec :last_name_input := 'LN_2';
select * from Test_Persons tp where tp.LASTNAME = :last_name_input;

2 Answers 2

1

You may use either of these two methods to avoid prompts in SQL developer.

Set the value of the variable from exec and run as script (F5)

var   last_name_input varchar2(20);
EXEC  :last_name_input := 'LN_2'
select * from Test_Persons tp where tp.LASTNAME = :last_name_input;

Another method is to use a substitution variable( & by default, you may also change it using SET DEFINE )

define last_name_input = 'LN_2'
select * from Test_Persons tp where tp.LASTNAME = '&last_name_input';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Kaushik, this works. to be perhaps over-precise for anyone who wants to know: 'set define &' or 'set define on' will work here.
1

Your method2 works after removing ":" on Oracle 12c -

CREATE TABLE Test_Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255)
);

INSERT INTO Test_Persons
    (PersonID,LastName,FirstName)
    values(1,'LN_1','FN_1');

INSERT INTO Test_Persons
    (PersonID,LastName,FirstName)
    values(2,'LN_2','FN_2');

commit;


set serverout on;

declare
    last_name_input varchar2(20);
    first_name_output varchar2(20);
begin
    select 'LN_2' into last_name_input from dual;
    select tp.FIRSTNAME into first_name_output from Test_Persons tp where tp.LASTNAME = last_name_input;
    dbms_output.Put_line('here: ' || first_name_output);
end;

Output -

Table TEST_PERSONS created.


1 row inserted.


1 row inserted.


Commit complete.

here: FN_2


PL/SQL procedure successfully completed.

4 Comments

thanks! this does work. However, can it work without the Begin End block? and still avoiding the 'Enter Bind' prompt window in SqlDeveloper?
Variables are allowed only within PL/SQL block.
You can write your query directly as - select tp.FIRSTNAME from Test_Persons tp where tp.LASTNAME = 'LN_2';
But this does not work anywhere outside SQL Developer or SQL*Plus .... to capture the data within your application you need provision (like a variable) to hold it and then the value can be used.

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.