Oracle Query - &&# variables from SQLPlus - sql

I have successfully used &&1, &&2, &&3, etc. in my SQLPlus query, and was wondering if it is possible to use this with any Oracle query, not just through SQLPlus?
What are these "variables" properly called?

These are called, Substitution Variables. They are only for sqlplus. See reference documentation If you are referring to "other queries" such as withing PL/SQL, those use bind parameters.

Related

Variable in Snowflake Task

I tried to use variable in snowflake task, but I received an error.
I read that task can contain only one statement and I think it can be a reason of failure - after variable setting the must be ';' - wchich means that there are at least two statements: variable declaration and main sql script.
Or maybe I am missing somethin important in documentation?
Is there any way to place variable in Snowflake task?
If not, what is the best workauround for that?
If you would like to use multiple statements in the snowflake task, I would suggest using a stored procedure in the task.
Currently, a task can execute a single SQL statement, including a call to a stored procedure.
https://docs.snowflake.com/en/user-guide/tasks-intro.html

nested sql scripts and variables in Postgres

I need to migrate from Oracle to Postgres.
In Oracle I have .sql files calling nested .sql files (with input and output parameters each one) in this way: nested sql scripts and variables
Looking for a similar solution in Postgres I found this:
How to run postgres sql script from another script?
But what happens with the solution: \i other_script.sql
Can I pass input parameters to it? (and if yes, how?) Can I return parameters from it? (and if yes, how?)
Or the other_script.sql and the calling.script share variables?
Or there isn't an equivalent solution for input/output parameters. And variables are not shared between the two .sql files?
psql session variables are session based, so you can set any psql variable inside script, and you can read this variable outside script.
\set myvar some value
select :'myvar';
You can read more in psql documentation.

Oracle SQL Developer: possible to pass parameters to SQL script?

I have the following SQL script (all names replaced with metasyntactic variables):
DELETE FROM FOO
WHERE UPPER(BAR)=? AND UPPER(BAZ)=? AND UPPER(QUX)=? AND UPPER(QUUX)=? AND UPPER(CORGE)=?;
When I run it in SQL Developer, I get the following error, as expected: SQL Error: Missing IN or OUT parameter at index:: 1
Is there a way in SQL Developer to pass test parameters to these values (in this case, BAR, BAZ, QUX, QUUX, and CORGE) or do I have to manually replace the question marks? Thanks!
UPDATE: I've been looking on the internet to try to find the answer with no luck; none of the articles I found deal with the FOO=? syntax. I know that that is the proper syntax to access (and in this case, delete from) the database from a Java program, but I would like to test the SQL script before embedding it in the program.
A query that way is called a prepared statement. It is used as a way to send SQL statements from a program to the database.
First you have to prepare the prepared statement object and later you supply the parameters and finally execute the statement. When setting the parameters, you refer to each question mark in the order they apear in the statement. So in you case, BAR would be parameter 1 and CORGE will be parameter 5.
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
For testing a query like this is quite useless in my experience. You could change the paramters to variables or even substitution variables if that is more convenient while testing.
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch5.htm#i1211130

What is this Oracle SQL Syntax ${}?

I have an oracle query which has a select statement
select table.columnname = ${sometext_sometext_sometext}
I would like to know what is the purpose of ${}.
Also this throws an error in Oracle SQL developer. Kindly advise what is the work around.
This isn't Oracle syntax, this is a common syntax for interpolating variables into a string found in Perl, Groovy, and a bunch of other languages.
You don't say what the context is here, but what is probably going on is something modifies the file, probably with environment-related properties, before the SQL gets run, the ${} is there to identify to the modifying script what value to substitute here. This is a common thing to do when you have environment-specific properties that need to be injected into a SQL script.
Can you give some background as to where you got this SQL statement? It appears that you are working with a query that requires pre-processing via Java, PHP, a Bash shell script, etc. Standard Oracle SQL or PL/SQL does not know what to do with the "${}" syntax.
I have used this syntax in a standard SQL template that I then process in Java or a bash shell script to generate the final SQL statement.

Can I prepare a statement in plain Oracle SQL?

3GLs provide mechanisms to prepare statements before executing them. E.g.
SELECT name
FROM people
WHERE age=:AGE
The same query can then be executed for different ages. But can such a statement also be prepared in a "plain" Oracle SQL client? Can the same be done in e.g. SQL Plus or dbForge Studio for Oracle as in Java or C# or any other programming language that supports prepared statements?
In dbForge Studio for Oracle, named parameters can be used, preceded by a colon :
SELECT *
FROM people
WHERE name=:name
The parameters can then be filled in with the "Edit parameters dialog box", available from the SQL toolbar.
I know you didn't ask about PostgreSQL but about Oracle. However, of note, PostgreSQL has this feature right in its SQL language.
The SQL standard includes a PREPARE statement, but it is only for use in embedded SQL. The PostgreSQL version of the PREPARE statement works like this:
PREPARE nameByAge(number) AS
SELECT name
FROM People
WHERE age=$1;
and you use it like this:
EXECUTE nameByAge(18);
EXECUTE nameByAge(50);
So unfortunately for Oracle SQLPlus the answer seems to be no, not bind variables. But SQLPlus has substitution variables, similar to shell scripts. You use them as &1, &2, &3, ... and they get their parameters from the way you call the SQLPlus script.
sqlplus user/password #script.sql 18
sqlplus user/password #script.sql 50
with the script.sql being
SELECT name
FROM People
WHERE age=&1;
this would work, even though it is not bind. But then, do you really care about the slight savings in repeat parse time? In fact Oracle hashes SQL statements and already replaces constants with bind variables to be able to better reuse query plans. So the savings you would get with PREPARE and BIND are really minuscule.