Suppose I have a query:
SELECT * from table1 tr where tr.start_time < &&starttime;
First time i execute the query, SQL Developer prompts me to enter the value for the substitution variable 'starttime' and is stored. Next time I run the script, it reuses the same value again, how do I reset/clear the stored value?
UNDEFINE starttime;
Just use above command.
Just use one ampersand at the start. You will be prompted to enter the value fresh. See the difference between & and &&
You can try these commands:
select &&v_value from table_name;
undefine v_value;
Related
In SQL*Plus, I want to execute multiple SQL queries in single line like
create table emp(name varchar2(20)); desc emp;
I tried executing this one but didn't work for me.
BEGIN OPEN :1 FOR SELECT * FROM table1; OPEN :2 FOR SELECT * FROM table2; END;
is there any way to accomplish this?
Thanks in advance!
SQL*Plus expects either:
A single SQL command, terminated by either a ";" character or a "/" on a line by itself.
A PL/SQL block
A SQL*Plus command
What you have entered is 2 queries on a single line, which SQL*Plus will send to the RDBMS - Oracle will then try and parse the string sent as a single query and fail because it is not valid SQL.
A quick workaround would be to have all your commands in a sql file and run them using #file.sql
I have an SQL statement in Oracle SQL developer that has some variables:
DEFINE custom_date = "'22-JUL-2016'" --run1
DEFINE custom_date = "'25-JUL-2016'" --run2
SELECT * FROM TABLE WHERE date=&custom_date
The real query is much more complicated and has many more variables and new tables are created from the results of the query. How can I create a script so that the query is executed twice, the first time with the custom date set as in the first line and the second time as in the second line.
In Oracle, the &variable is a "substitution variable" and is not part of SQL; it is part of the SQL*Plus scripting language (understood by SQL Developer, Toad etc.)
The better option, and what you are asking about, is BIND VARIABLES. The notation is :variable (with a colon : instead of &), for example :custom_date.
The difference is that a substitution variable is replaced by its value in the front-end application (SQL Developer in your case) before the query is ever sent to the Oracle engine proper. A bind variable is substituted at runtime. This has several benefits; discussing them is outside the scope of your question.
When you execute a query with bind variables in SQL Developer, the program will open a window where you enter the desired values for the bind variables. You will have to experiment with that a little bit till you can make it work (for example I never remember if a date must be entered with the single quotes or without). Good luck!
Define is used in TRANSACT SQL. To do this Oracle way, You can create anonymus PL/SQL block, similar to this:
DECLARE
p_param1 DATE;
p_param2 NUMBER;
CURSOR c_cur1(cp_param1 DATE,cp_param2 NUMBER)
IS
SELECT * FROM table WHERE date = cp_param1
;
BEGIN
-- Execute it first time
p_param1 := TO_DATE('2016-09-01','YYYY-MM-DD');
FOR r IN c_cur1(p_param1)
LOOP
NULL;
END LOOP;
-- Execute it second time
p_param1 := TO_DATE('2015-10-11','YYYY-MM-DD');
FOR r IN c_cur1(p_param1)
LOOP
NULL;
END LOOP;
END;
And in it, You create cursor with parameters and execute it twice with different parameter.
I do not know why You want to execute this query twice, so the script abowe does nothing with results, but it certainly should execute Your query twice, with different params.
In SQL Developer, is it possible to define a variable as a construction based on another variable(s) to be used with a SPOOL?
For example:
define startdate='01-JAN-14'
define sdt=SUBSTR(&&startdate,4,3)
SPOOL &&sdt._File.csv
Giving me the output "JAN_File.csv"
I know that won't work because I've tested it and I've tried to research it, but is there a workaround to get to that same output?
Not pretty but if you really wanted to you could use a bind variable as an intermediate step:
define startdate='01-JAN-14'
var bind_sdt varchar2(3);
exec :bind_sdt := SUBSTR('&&startdate',4,3);
column sub_sdt new_value sdt;
select :bind_sdt as sub_sdt from dual;
SPOOL &&sdt._File.csv
The column ... new_value command is creating a substitution variable from the select-list item, which is the bind variable, which is the sub-string.
You could set termout off and back on around the exec and select, and set verify off if you aren't already doing so. You haven't said where the start date string is coming from so I've left that as a define.
I am transitioning from SQL Server to Vertica. Is there any comparable way to create a variable?
Usually I do something like:
Define #myVariable int
Set #myVariable = select MAX(Field1) from myTable
I do not think Vertica allows variables, except if you are using vsql directly, but then vsql variables are very limited and will not do what you expect:
-- this will work
\set a foo
\echo :a
foo
-- this is not what you expect:
\set a NOW()
\echo :a
NOW()
\set a select max(id) from s.items()
\echo :a
selectmax(id)froms.items()
See for more information the vertica doc at https://my.vertica.com/docs/6.1.x/HTML/index.htm#2732.htm
You do not "create variables" in Vertica the same way you do not "create variables" in SQL Server. What you're trying to convert is a T-SQL script.
You can do the same in Vertica by creating Perl or Python or Java ... scripts running outside the database or writing a user defined function in C++ or R or Java running inside Vertica.
You can use :variable_name in Vertica for a user input variable. For example:
select date_time from table_1 where date_time between :start and :end
In above start and end are the variables. When you run the query, a dialog box opens prompting you to enter the values for start and end.
If you are using vsql, you can create variables that contain results of queries, though it is a bit convoluted:
Lets assume you start vsql with vsql -v INARG=33;
SELECT :INARG+1; -- Set up the query
\pset format u
\pset t -- Update output format to bare
\g `echo /tmp/dyneval` -- Eval the query and write into file
\set DYNARG `cat /tmp/dyneval` -- Set var from shell command output
\echo :DYNARG
So basically we write the query result into a file and read the file's contents into a variable.
You can use /tmp/dyneval-${PPID}_id instead of /tmp/dyneval, so you can ensure, that parallel executions wont alter each other. (PPID being the parent process's process ID, that is the vsql process's PID.)
The solution has some limitations:
assumes a linux env (echo, cat)
it changes the output formatting settings (\pset)
When I do a query SELECT t.NAME, t.SOME_DATE FROM MY_TABLE t in toad I get dates as 14-FEB-13 with no time information! In the table browser it gives full date and time. How can this be changed?
That is the default setting for a session. If you want to change it,
ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';
Or, change your query to
SELECT t.NAME, to_char(t.SOME_DATE,'DD-MON-YYYY HH24:MI:SS') FROM MY_TABLE t
( the above is just an example, you can format whatever way you want in the format string, ie. 'MM/DD/YYYY, etc.)
If you want to see the same results as you see in the table browser when executing SQL directly (without changing your settings), use "Editor\Execute Statement (F9)" instead of "Editor\Execute as Script".
"Execute statement" displays data in a Toad grid, so the formatting will always be the same as the formatting you see in the table browser, while "Execute script" uses your session information, as explained in OldProgrammer's answer.
Execute statement:
Execute script: