0

I would like to run below query:

SELECT 
    * 
FROM 
    TABLE1
WHERE 
    COL1 = :DynamicValue1
    AND  COL2 = :DynamicValue2
USING 
    USERENTEREDVALUE1, USERENTEREDVALUE2;

I don't want to use EXECUTE IMMEDIATE.

How can I use using keyword in select query? When I run this I get pop up to enter value but it gives error Ora-00933

1
  • 1
    try using DBMS_SQL Commented Oct 11, 2018 at 13:37

2 Answers 2

1

You may use substitution variables

DEFINE lname = 'Rogers'
DEFINE mgrid = 122

SELECT *
FROM employees
WHERE last_name = '&lname'
AND manager_id  = '&mgrid';

When you run this in SQL developer or SQL* Plus, you get

old:SELECT *
FROM employees
WHERE last_name = '&lname'
AND manager_id  = &mgrid
new:SELECT *
FROM employees
WHERE last_name = 'Rogers'
AND manager_id  = 122

EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DAT JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- -------------------- ------------------------- ------------------------- -------------------- -------- ---------- ---------- -------------- ---------- -------------
        134 Michael              Rogers                    MROGERS                   650.127.1834         26-08-06 ST_CLERK         2900                       122            50

Or use Bind variables

VARIABLE   lname VARCHAR2(40) 
VARIABLE  mgrid NUMBER 
EXEC :lname := 'Rogers'
EXEC :mgrid := 122
VARIABLE x REFCURSOR


BEGIN
     OPEN :x FOR SELECT *
                 FROM employees
                 WHERE last_name =:lname 
                 AND manager_id =:mgrid;
END;
/

    PRINT x

Result

PL/SQL procedure successfully completed.


PL/SQL procedure successfully completed.


PL/SQL procedure successfully completed.



EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DAT JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- -------------------- ------------------------- ------------------------- -------------------- -------- ---------- ---------- -------------- ---------- -------------
        134 Michael              Rogers                    MROGERS                   650.127.1834         26-08-06 ST_CLERK         2900                       122            50
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any way I can use in single statement? I am not using PL/SQL. Just want to run query from editor.
@AAD : if you run my first select query without defining the 2 variables, it does prompt you to input values, which substitutes the value u enter and gives u the query result
Yes but can't I combine that with 'using' keyword?
@AAD : well you can't. That's why you get the error as there is no such syntax.
0

I'm not sure if you're doing pl/sql, but if you do, you can simply add variables to you query:

declare
    USERENTEREDVALUE1 VARCHAR2(100) := 'value1';
    USERENTEREDVALUE2 VARCHAR2(100) := 'value2';
    RESULTVALUE VARCHAR2(100);
begin

    SELECT COL3
      INTO RESULTVALUE
      FROM  (
                SELECT 'value1' COL1, 'value2' COL2, 'Ok' COL3 FROM dual UNION ALL
                SELECT 'value3' COL1, 'value4' COL2, 'NOK' COL3 FROM dual
            ) TABLE1
    WHERE COL1 = USERENTEREDVALUE1
      AND COL2 = USERENTEREDVALUE2;

    dbms_output.put_line('RESULTVALUE: ' || RESULTVALUE); 
end;

2 Comments

No I am not doing PL/SQL. I just want to run SQL query from editor with dynamic binding and would like to use 'using' keyword.
Okay.. I did remove your pl/sql-Tag

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.