There is a query that i use very often on a daily basis. I usually run this same query several times using a different condition in the where clause throughout the day.

Given the following table:

columnA columnB columnC columnD
1         2         3     4
5         6         7     8
9         9         9     9

When i run my query i usually modify the where clause to get the results based on a particular column. Sometimes i would say 'where columnA=1' or i would change it to where columnB=3'.

To avoid having to retype the query everytime, i decided to script this in an SQL plus script.

To run the script i want to run it as follows:

> @myScript columnA 3
or
> @myScript columnC 7

Here is what i have so far:

define searchColumn=&&1
define searchParam=&&2

Select columnA, columnB, columnC,columnD
from myTable t
where t.'&searchColumn' in ('&searchParam')

The above doesnt work yet as it is complaining about the t.'&searchColumn'. How can i build the table name in the where clause that includes the prefix.

Additionally, if you have similar experiences where you have a set of common queries that you run every day, i would love to know how you use them to make your life easier. If there are better solutions i please let me know.

Thanks

有帮助吗?

解决方案

Try:

Select columnA, columnB, columnC, columnD
from myTable t
where t.&searchColumn in ('&searchParam')

Also if they are going to be typing in the substitution values, you don't need to define them earlier.

And I would change "IN" to "="

Or if they need to type in multiple values to search on:

Select columnA, columnB, columnC,columnD
from myTable t
where t.&searchColumn in (&searchParam)

But they will have to have correct input, such as:

'string','string1'

2010,2011

If you want them to be able to type the substitution values into the file (at the top) using DEFINE, this is what you would do:

define searchColumn = column_name_here
define searchParam = search_term_here

Select columnA, columnB, columnC,columnD
from myTable t
where t.&searchColumn in ('&searchParam')

Again, you might want to change IN to =

On a side note, if the substiution variable is not defined, the user will be prompted to enter it. So it depends on whether you want them to be prompted to enter it each time it's run, or if you want them to be able to define the variables at the top of the script, before they run it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top