Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in TOAD scripts

I have a SQL script that is being executed in TOAD. Currently, I have it laid out with just statement after statement, thusly:

select such-and-such from somewhere;

delete other-thing from somewhere-else;

And so on. Some of the where clauses end up being repetitive because I have complex inner queries to get particular IDs to operate on. I'd like to capture the ID in the beginning of the script in a variable, and then use that variable in subsequent where clauses. So something like this:

variable MY_ID = select the-ID from somewhere;

select such-and-such from somewhere where ID = @MY_ID;

Obviously, I'm making up that syntax, but that is what I'm looking for. But I'm not sure if that is possible in a TOAD script. I know I can convert the whole thing to a PL/SQL block but I'm trying to avoid having to do that for various reasons.

Any way to do this using TOAD without converting to a PL/SQL block?

like image 625
RationalGeek Avatar asked Dec 22 '09 15:12

RationalGeek


People also ask

Can you use a variable in in statement SQL?

The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement. A common situation in which SQL variables come in handy is when you need to issue successive queries on multiple tables that are related by a common key value.

How do you set a variable in SQL query?

Setting a Value in a Transact-SQL Variable To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.

How do you display results in Toad?

Right-click the data and select Compare To. Display the data in a grid or card view; grid view is useful for viewing summary information at a glance; card view is useful for viewing information in greater detail. Right-click a record and select Show |Grid View or Card View.

Can we connect Postgres using Toad?

According to the Quest, Toad Edge supports EDB Postgres Advanced Server, MySQL, MariaDB, Amazon Redshift (beta), and PostgreSQL. This makes it a very handy tool to have on one's system.


2 Answers

I think this will accomplish what you want. You can declare a bind variable, insert a value into it, and then use it in future statements.

variable l_var varchar2(1);

begin
  select dummy
    into :l_var
    from dual;
end;

select *
  from dual
 where dummy = :l_var;
like image 176
Daniel Emge Avatar answered Nov 08 '22 01:11

Daniel Emge


I use SQL*PLUS substitution variables. They are supported by TOAD. You can execute this code by pressing F5.

COLUMN VAR NEW_VALUE VAR_VALUE

SELECT 'SOMETHING' VAR FROM DUAL;  --this sets 'VAR_VALUE' = 'SOMETHING'

SELECT '&VAR_VALUE' FROM DUAL;  --this uses the value set by the previous stmt.
like image 1
Chris Ellison Avatar answered Nov 08 '22 01:11

Chris Ellison