11

I have a simple query that I can run in toad.

select * from my_table 
where my_id = 11111;

Why can't I run the same query from toad this time declaring a variable and using it in the where clause?

In sql server I would:

declare @testID int
set @testID = 11111
select * from my_table 
    where my_id = @testID;

How can I accomplish the same in Oracle 11g?

4
  • 3
    depends.. .if you're doing this for testing and you want to capture the value at runtime you could just select * from my_table where my_id = &testID; ampersand denotes variable requiring user input when executed. Be cautious of this in Oracle as if you have a name like Mike & Jon's Paint and body it may ask you to define Jon variable! (this can be disabled: see here) Commented Nov 15, 2016 at 13:30
  • For similar question, Answer already given earlier. Check it out here Commented Nov 15, 2016 at 13:49
  • @xQbert I have multiple "select * from tables where id = &id" queries. If i will add &id , it will prompt me everytime query is getting executed. Can I do something to make prompt once, & the use the same value again & again. Commented Oct 15, 2020 at 15:27
  • @ASharma7 Untested but this concept should work: use a selct statement and cross join to the the variable SELECT A.*, Z.MyTestValue FROM tableName A cross join (Select &myTestValue myTestValue from dual) Z but that only works if you can union the results of you multiple selects and do the cross join after the union. This is a different question: I recommend asking a new one. if the above doens't work Commented Oct 15, 2020 at 16:38

3 Answers 3

7

PLSQL is different than SQL SERVER. It has its own syntax. See how you can do it as below:

DECLARE
   var    NUMBER := 1;
   var2   my_table%ROWTYPE;
BEGIN
   SELECT *
     INTO var2
     FROM my_table
    WHERE my_id = var;
     
  --To display result you need to add dbsm_output.put_line function.

  dbms_output.put_line(var2.<columnname>);
END;
/

Note: Assumption is that the query wil return a single row only.

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

7 Comments

To see out you first need to display it using dbms_output.put_line function.
I'm still not seeing the results. (My output is enabled in toad) declare p_jobID number(6) := 111111; p_row JOBS%rowtype; begin select * into p_row from my_table where id = p_jobID; Dbms_Output.put_line(:p_row); end;
You need to mention p_row.columnname like p_row.x where x is a column of your table my_table
So if my table has 30 columns then I have to specify all 30 columns?
Thank you. Seems like I have to jump through hoops more in Oracle then sql server.
|
6

In Toad (or SQL Developer) you can do this:

select * from my_table 
where my_id = :testID;

When you run it, you will be prompted to enter a value for the testId bind variable.

2 Comments

I have multiple "select * from tables where id = &id" queries. If i will add &id , it will prompt me everytime query is getting executed. Can I do something to make prompt once, & the use the same value again & again.
@ASharma7 Yes, you can use @@id
0

You can do it with user variables (DEFINE) or bind variables (VAR):

With DEFINE (this basically replaces the values "as text" and it can be used in almost every part of SQL commands):

define var_col1 = 5
define var_col2 = 6

SELECT * FROM my_table1 WHERE column1=&var_col1 and column2=&var_col2;
SELECT * FROM my_table2 WHERE col1=&var_col1 and col2=&var_col2;
/* and more SELECTs with var_col1 and var_col2 */

With bind variables (they can be used only where a value is expected in SQL):

var var_col1 number
var var_col2 number

exec :var_col1 := 5
exec :var_col2 := 6

SELECT * FROM my_table1 WHERE column1=:var_col1 and column2=:var_col2;
SELECT * FROM my_table2 WHERE col1=:var_col1 and col2=:var_col2;
/* and more SELECTs with var_col1 and var_col2 */

The above both work in SQL*Plus and SqlDeveloper.

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.