Use a database value in SQLPLUS as a parameter to a batch file - oracle

I need to run a batch file and pass in a database value. Depending on the day, a different value needs to be passed into the DOS command. This particular business rule is defined in a view in the database.
Our primary scheduling tool is datastage which runs scripts in SQLPLUS, so this is my preferred method for schedules activities. I could also run a batch file directly taht calls SQLPLUS and gets a value, but there is much more hassle setting it up so I'd prefer not to.
I'm a SQLPLUS amateur.
The SQL script I'm passing into SQLPLUS is below. The problem I'm having is it's passing in :sTriggerName, not the value of it.
set echo on
set feedback on
set termout on
VAR sTriggerName VARCHAR2(100)
SELECT TRIGGERNAME INTO :sTriggerName
FROM SCHEMA.VIEW
WHERE CALENDAR_DATE = TRUNC(SYSDATE) AND ROWNUM < 2;
/
HOST "E:\CallScheduleTrigger.CMD :sTriggerName."
quit

In the example above I am using a bind variable.
This link showed me hwo to load a substitution variable from the database and use that instead:
http://www.oracle.com/technetwork/testcontent/sub-var9-086145.html
To load a a database value into a substitution variable called sTriggerName
COLUMN TRIGGERNAME new_value sTriggerName
SELECT TRIGGERNAME FROM SCHEMA.VIEW WHERE CALENDAR_DATE = TRUNC(SYSDATE) AND ROWNUM < 2;
To use this substitution variable in a host command (i.e. as a parameter to a batch file):
HOST "E:\CallScheduleTrigger.CMD &sTriggerName"

Related

Where the NLS_TIMESTAMP_FORMAT is set at client side?

I want to know where does this parameter gets set?
I cannot make connection and then set this parameter as I have to use it in a script to execute procs.
$ORACLE_HOME/dbs or $ORACLE_HOME/databases does not exist and init.ora file is not present anywhere.
I have tried to first set this parameter at shell level and then make connection and execute proc, but that does not works.
Ultimately I want this parameter to be 'MM/DD/YYYY HH:MI:SSXFF AM'.
Please help determine where it is set or let me know if I can set some variables at shell level so that it is set in oracle session by default to
'MM/DD/YYYY HH:MI:SSXFF AM'.
It is oracle DB.
Thanks in advance!
first off, it's not NLS_TIMESTAMP_PARAMETER, but NLS_TIMESTAMP_FORMAT.
yes, it is an environment variable, set at the os session level. It doesn't go in any init files. Unfortunately, no one was looking over your should to see exactly what you did when you "tried to first set this parameter at shell level and then make connection and execute proc". But if I had a shell script that called sqlplus, I would:
NLS_TIMESTAMP_FORMAT=<whaterver format mask you want>
export NLS_TIMESTAMP_FORMAT
sqlplus ........

Parameterizing adhoc scripts

In SQL server I always had a set of diagnostic scripts and always made sure to declare variables with identifiers so that my other selects and updates would leverage them. I'm having trouble adopting this pattern in Oracle.
I might have 4 or 5 select queries, and then also some updates that I may uncomment once I've verified the results. I want to see the results of the select queries in output.
I am using SQL Developer.
First I tried using a DEFINE block, but it seems this must be paired with BEGIN/END block, and once a query is inside a block, it seems like it becomes cumbersome to view the results. The examples I've seen either involve setting a cursor then iterating over the cursor to print results, or you must print individual values which is even more cumbersome.
So instead I tried using variable's since I can reference them without declare/begin/end, but I am having trouble setting the value of the variable:
variable customerid number;
customerid := 1234;
But I get this error:
Error starting at line : 5 in command - customerid := 1234 Error
report - Unknown Command
I also tried
select t.customerid into :customerid
from customer t
where t.customerid = 1234
and get:
SQL Error: ORA-01006: bind variable does not exist
01006. 00000 - "bind variable does not exist"
My goal is to have my id declarations at the top where I set the values, and be able to run the script and all my adhoc selects appear in output.
You need to set the bind variable in a PL/SQL context, either with an execute syntactic wrapper:
variable customerid number;
exec :customerid := 1234;
or slightly more explicitly:
variable customerid number;
begin
:customerid := 1234;
end;
/
which is (almost) equivalent, but will probably be more convenient if you want to set multiple variables. You can populate the bind variabke from a query too, as you attempted, but that also needs to be in a PL/SQL context:
begin
select t.customerid into :customerid
from customer t
where t.customerid = 1234;
end;
/
Notice the colon before customerid, indicating it is a bind variable, in all of those. You need that when you reference it later, e.g. in a SQL query (which doesn't need to be in a PL/SQL block):
select * from customer where customerid = :customerid;
You can use the same mechanism in your updates later. The exception to using a colon is if you want to just see the value of the variable; you could select :customerid from dual, but there is also the ability to
print customerid
That's even more useful if your variable is a refcursor.
define is a completely different mechanism, for substitution variables rather than bind variables. You don't need to use PL/SQL blocks for this either:
define customerid=1234
select * from customer where customerid = &customerid;
Notice there is no colon this time. And also note that if your variable is a string, you need to enclose it in quotes when you use it:
define name=aaron
select * from users where first_name = '&name';
You can also use the result of a query to populate a substitution variable, using the new_value syntax.

How to pass parameters to sql scripts via command line

There a requirement where I am trying to automate a process in my project where in a sql need to be run for daily reporting.
sql looks like below: (This is simplest form but my sql is of 400 lines,below is just an example to get the results).
test.sql
select * from table
where create_date between &date1 and &date2;
I wanted to create a bat file that calls the sqlplus and passes the dates via command prompt.Date will be calculated automatically and will be passed in the commmmand prompt itself.
I've tried the below command line :
sqlplus userid/password#db_name #test.sql DATE1 DATE2
But that still prompts me to enter the dates for date 1 and date 2 which I want that to be picked up from arguments automatically.
Could you please help me in achieving the above ?
The parameters that are being passed from the command line are available in SQLPLUS as &1 and &2.
select * from table
where create_date between &1 and &2;
To prevent problems with date formatting you may want to consider changing that to
select * from table
where create_date between to_date('&1','DD-MM-YYYY') and to_date('&2','DD-MM-YYYY');
Or whatever date format you want to use.
If your date1 and date2 are being used for giving range for single day(today/yesterday), you can use Now() method of SQL.

Dynamic Schema name in SQL based on database-name Oracle

I have a DML statement "Simple update statement" that i need to execute in Different Oracle Environment (DEV,SIT,QA,PROD). As of Now im hard coding the Schema Name in Alter Session Command and Running the DML statement. So i need to maintain 4 different scripts across the environment.
Is there a way i can get the Database name on which the script is Running, from the database Name i can use If Else Condition to Choose between the Schema and assign it to the ALTER SESSION COMMAND?
You can query the name from the v$database view. How you translate that to an alter session command depends on your client and how you're connecting.
For example, in SQL*Plus you could do:
column x_schema new_value y_schema noprint
set termout off
select case name
when 'DEVDB' then 'DEVSCHEMA'
when 'PRODDB' then 'PRODSCHEMA'
...
end as x_schema
from v$database;
alter session set current_schema = &y_schema;
set termout on
The first query uses the DB name to determine a schema name; the column command makes that column alias x_schema available later as substitution variable &y_schema. Then that is used in the alter. The set termout is optional but will hide the query if it's run as part of a script (though then I guess the noprint is a bit pointless).
You could base your case on global_name.global_name if you prefer, or even sys_context('USERENV', 'SERVICE_NAME').
Of course, you could also just pass the schema name in as a positional parameter when you run the DML script instead. It's unusual, I think, for the schema name to be different for the same application in different databases, but this should work if that is the situation you have.
To get the current schema name:
select user from dual
Is this what you're after?

Using variables in Oracle SQL Developer 3.2

I am extremely new to SQL, and manage to extract from some other queries we use the following syntax:
--VARIABLES
undefine PF_PROD --product;
undefine PF_PSG --shop;
--QUERY
SELECT *
FROM ET1250
WHERE PRODUCT=&&PF_PROD
AND PRICE_SHOP_GROUP=&&PF_PSG
ORDER BY PERIOD_YEAR desc,PERIOD_WEEK desc;
This works fine as long as I run the undefine statements first, is there a way to make the query always ask for these variables without me having to undefine them first?
Use a single &. This is covered briefly in the SQL Developer documentation:
For substitution variables, the syntax &&variable assigns a permanent
variable value, and the syntax &variable assigns a temporary (not
stored) variable value.
... and in more detail in the SQL*Plus documentation, which is largely relevant for both clients.
Note that if you define or accept a variable then it won't be prompted for even with a single ampersand, but that doesn't seem to be relevant to you at the moment.
There are two types of variable in SQL-plus: substitution and bind.
Substitution variables can replace SQL*Plus command options or other hard-coded text:
define a = 1;
select &a from dual;
undefine a;
Bind variables store data values for SQL and PL/SQL statements executed in the RDBMS; they can hold single values or complete result setsb:
var x number;
exec :x := 10;
select :x from dual;
exec select count(*) into :x from from dual;
exec print x;
SQL Developer support substitution variables, but when you execute query with bind :var syntax you are prompted for binding (in dialog box).
Reference:
http://www.oracle.com/technetwork/testcontent/sub-var-087723.html SQL*Plus Substitution Variables, Christopher Jones, 2004
https://stackoverflow.com/a/22171706/173149

Resources