Toad World® Forums

Where statements

I have a situation where multiple variables can have the same value. I’m trying to set up a condition where I only have to enter the value once. Any suggestions?

To tpyane:

I humbly apologize. I regret that I do not understand the context of this request.

Please let us know which feature of Toad Data Point you are utilizing, or if this is general SQL Construction. Giving us an example of what you currently have implemented would also help us with providing a solution catered to you.

We look forward to your response,

-Joshua Liong

Maybe I am missing something here but can’t you just use the same variable multiple times in your query instead of multiple variables with the same value?

To tpayne:

I believe I understand your issue. There are ways of addressing your problem, though I could not tell at a glance which database platform you were inquiring about. The coding syntax I’m writing will be catered to SQL Server.

You can type out a series of SQL Statements where you only need to assign the variable just once, as you originally requested!

Declare a variable

/* IN SQL Server, declare with "DECLARE @var DATATYPE" */
DECLARE @myVariable varchar(50);

/* Assign it a value HERE! * /
SET @myVariable = 'I%';

/* Use a statement! */
SELECT * FROM ADDRESS
WHERE (CITY LIKE @myVariable) OR (STATE LIKE @myVariable);

3731.1.png

It sounds like you’re typing the same statements quite a bit. It may helpful to use SQL to simply prompt you while it is executing instead of typing in the value before execution. Below is an example of the syntax with two approaches of doing the same thing.

Use an unassigned unnamed variable

/* IN SQL Server, declare with "DECLARE @var DATATYPE" */
DECLARE @myVariable varchar(50);

/* Get prmopted for a value every execution */
SET @myVariable = ?;

/* Use a statement! */
SELECT * FROM ADDRESS
WHERE (CITY LIKE @myVariable) OR (STATE LIKE @myVariable);

6303.2.png

Use a unassigned named variable

/* Use a statement with named variable :[varName]*/
SELECT * FROM ADDRESS
WHERE (CITY LIKE :InputVar) OR (STATE LIKE :InputVar);

Again, syntax varies depending on the database platform you’re using. I hope this post is a good start to achieve your needs.

-Joshua Liong