0

I am a read only user for a database and I have a task of adding some data into the system and need to verify my progress at every step. Adding and modifying the data is a task done in a portal and is not done directly from my SQL file.

I have one SQL file with several, non-trivial SELECT statements, each of which I have to edit one or two values every time I go through this process. The values are the same between each of the statements. Each of these statements is run independently at different points of the process and combining the statements would make verification difficult and confusing.

To simply an example:

SELECT *
FROM cats
WHERE name = 'pepper';

SELECT *
FROM dogs
WHERE name = 'pepper';

SELECT *
FROM birds
WHERE name = 'pepper';

In the statements above, the name of all the pets is 'pepper', but I load data to these tables one table at a time and I cannot load dogs until I verify cats were loaded correctly.

Is there a way to declare the name I'm searching for at the beginning of the file that will propagate into each of the SELECT statements, similar to declaring a variable in virtually every other language? It needs to have at least the scope of one file but does not need (not should it) have any larger scope.

Metomorphose
  • 434
  • 3
  • 14
  • Will `:value` work? This will depend on how you are running the query (ie `where name = :value` – JohnHC Jun 27 '17 at 14:11
  • @JohnHC is that a SQL+ thing? – Metomorphose Jun 27 '17 at 14:13
  • I know @JohnHC 's suggestion works in Oracle SQL Developer... – Alvin Thompson Jun 27 '17 at 14:14
  • @Metomorphose have a look at this [StackOverFlow Question](https://stackoverflow.com/questions/3564283/how-to-declare-variable-and-use-it-in-the-same-sql-script-oracle-sql) – Sudipta Mondal Jun 27 '17 at 14:15
  • What do you mean by "SQL file"? If it is an SQL\*Plus script, you can use a substitution variable, like `&name`. In any case, the answer to your question lies in the scripting language you are using, not in SQL. –  Jun 27 '17 at 14:15
  • @SudiptaMondal I'm not using SQL+ – Metomorphose Jun 27 '17 at 14:26
  • @mathguy I literally have a .sql file that I load into SQLTools to run my SELECT statements. No SQL+ used. – Metomorphose Jun 27 '17 at 14:27
  • I am not familiar with SQLTools, but they must have a concept of "substitution variable" (perhaps identical to SQL\*Plus?) What happens if you `select &a from dual`? If you are prompted for a value for a, then that's your answer. –  Jun 27 '17 at 14:36
  • @mathguy just because I'm not familiar with them, how do you declare those? I tried just `define a = 'pepper'` and then your `SELECT &a FROM dual;` and it told me `not all variables bound`. – Metomorphose Jun 27 '17 at 14:42
  • Sorry, as I said, I am not familiar with SQLTools. Perhaps you should add the SQLTools tag to your question (if StackOverfow has one for that) and see if anyone who knows that tool can help. –  Jun 27 '17 at 14:49

1 Answers1

0

Oracle Bind Variables may help check sample code @

https://blogs.oracle.com/sql/improve-sql-query-performance-by-using-bind-variables