Using Variables in SQL*PLUS
Hi,
I hope to get any insight possible. I am using External tables and the Default directory is set. Now the process is that I get an automatic downloads of files placed in that directory. They are all to have the same naming system.
Test_June_2006 or Test_July_2006 and so on. Basically the month will be one behind, so Since it is August, I am looking for Test_July_2006.
I was able to set the "location" parameter to accept a variable, but i am having some trouble with set the value to that variable.
My code:
define month = EXTRACT(MONTH FROM CURRENT_DATE)-1||'_'||EXTRACT(YEAR FROM CURRENT_DATE).txt
variable month varchar2(100)
drop table ext_Test;
create table ext_Test
(field1 varchar2(25),
field2 varchar2(10),
field3 varchar2(10),
field4 varchar2(15),
field5 Float,
field6 Float,
field7 Float
organization external
( default directory sourcedata
access parameters
( records delimited by newline
SKIP 1
fields terminated by ','
OPTIONALLY ENCLOSED BY '"'
missing field values are null
location ('Test_&month')
reject limit 0;
When I tried "location ('&month') and setting month to July, it worked fine, but now that I am adding functions, it is not setting the variable properly.
Any help would be appreciated. Thank you.
You cannot use & substitution variable to replace file name dynamically.
You will have to use the ALTER TABLE command at run time to alter the location of the file to what you want before you start processing.
SQL> create table ext_Test
2 (field1 varchar2(25),
3 field2 varchar2(10),
4 field3 varchar2(10),
5 field4 varchar2(15),
6 field5 Float,
7 field6 Float,
8 field7 Float
9 )
10 organization external
11 ( default directory sourcedata
12 access parameters
13 ( records delimited by newline
14 SKIP 1
15 fields terminated by ','
16 OPTIONALLY ENCLOSED BY '"'
17 missing field values are null
18 )
19 location ('Test_&month')
20 )
21 reject limit 0;
Enter value for month: july
old 19: location ('Test_&month')
new 19: location ('Test_july')
Table created.
SQL> alter table ext_test location('Test_August') ;
Table altered.
SQL>
Similar Messages
-
Using variables in SQL Plus Worksheet
I'm new to Oracle. I've developed in MS SQL for some time. I'm trying to debug a query we're using in an application. I paste it into SQL Plus Worksheet and get the message:
SP2-0552: Bind variable "COUNTYPK" not declared.
I could paste in a value, but I would like to define a variable before the query so that it will work the same way as it would in production.
SELECT rsiplate.OWNER.work_area_code || '-' || SUBSTR(rsiplate.OWNER.work_phone_number, 1, 3) || '-' || SUBSTR(rsiplate.OWNER.work_phone_number, 4, 4) AS BUSINESSPHONE, trim(rsiplate.OWNER.first_name||' '||rsiplate.OWNER.middle_name||' '||rsiplate.OWNER.last_name) AS BUSINESSNAME, rsiplate.ADDRESS.address_line1 AS ADDRESSLINE1, rsiplate.ADDRESS.address_line2 AS ADDRESSLINE2, rsiplate.ADDRESS.city_name AS CITY, rsiplate.ADDRESS.state_code AS STATE, SUBSTR(rsiplate.ADDRESS.zip_code, 1, 5) AS POSTALCODE FROM rsiplate.WATERCRAFT_MASTER, rsiplate.WATERCRAFT_CURRENT, rsiplate.WATERCRAFT_REGIST_CURRENT, rsiplate.OWNER, rsiplate.WATERCRAFT_OWNER_CURRENT, rsiplate.ADDRESS WHERE rsiplate.WATERCRAFT_MASTER.WATERCRAFT_ID = rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_ID AND rsiplate.WATERCRAFT_MASTER.LF_WATERCRAFT_REGIST_ORDER = rsiplate.WATERCRAFT_REGIST_CURRENT.WATERCRAFT_REGIST_ORDER AND rsiplate.WATERCRAFT_MASTER.LF_WATERCRAFT_ORDER = rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_ORDER AND rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_REGIST_CURRENT.watercraft_id and rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_id and rsiplate.WATERCRAFT_OWNER_CURRENT.owner_id = rsiplate.OWNER.owner_id and rsiplate.WATERCRAFT_MASTER.watercraft_owner_group = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_group and rsiplate.ADDRESS.address_set_id = rsiplate.OWNER.address_set_id and rsiplate.WATERCRAFT_MASTER.data_status_code = 1 and rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_order = 1 and rsiplate.WATERCRAFT_REGIST_CURRENT.REGIST_EXPIRATION_DATE >= SYSDATE AND rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_USE_CODE = 3 AND rsiplate.ADDRESS.COUNTY_NUMBER = :CountyPK GROUP BY rsiplate.OWNER.work_area_code, rsiplate.OWNER.work_phone_number, rsiplate.OWNER.first_name, rsiplate.OWNER.middle_name, rsiplate.OWNER.last_name, rsiplate.ADDRESS.address_line1, rsiplate.ADDRESS.address_line2, rsiplate.ADDRESS.city_name, rsiplate.ADDRESS.state_code, rsiplate.ADDRESS.zip_code;Okay, I made some adjustements:
SET DEFINE :
DEFINE :COUNTYPK = '01'
SELECT rsiplate.OWNER.work_area_code||'-'||SUBSTR(rsiplate.OWNER.work_phone_number, 1, 3)||'-'||SUBSTR(rsiplate.OWNER.work_phone_number, 4, 4) AS BUSINESSPHONE, trim(rsiplate.OWNER.first_name||' '||rsiplate.OWNER.middle_name||' '||rsiplate.OWNER.last_name) AS BUSINESSNAME, rsiplate.ADDRESS.address_line1 AS ADDRESSLINE1, rsiplate.ADDRESS.address_line2 AS ADDRESSLINE2, rsiplate.ADDRESS.city_name AS CITY, rsiplate.ADDRESS.state_code AS STATE, SUBSTR(rsiplate.ADDRESS.zip_code, 1, 5) AS POSTALCODE FROM rsiplate.WATERCRAFT_MASTER, rsiplate.WATERCRAFT_CURRENT, rsiplate.WATERCRAFT_REGIST_CURRENT, rsiplate.OWNER, rsiplate.WATERCRAFT_OWNER_CURRENT, rsiplate.ADDRESS WHERE rsiplate.WATERCRAFT_MASTER.WATERCRAFT_ID = rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_ID AND rsiplate.WATERCRAFT_MASTER.LF_WATERCRAFT_REGIST_ORDER = rsiplate.WATERCRAFT_REGIST_CURRENT.WATERCRAFT_REGIST_ORDER AND rsiplate.WATERCRAFT_MASTER.LF_WATERCRAFT_ORDER = rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_ORDER AND rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_REGIST_CURRENT.watercraft_id and rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_id and rsiplate.WATERCRAFT_OWNER_CURRENT.owner_id = rsiplate.OWNER.owner_id and rsiplate.WATERCRAFT_MASTER.watercraft_owner_group = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_group and rsiplate.ADDRESS.address_set_id = rsiplate.OWNER.address_set_id and rsiplate.WATERCRAFT_MASTER.data_status_code = 1 and rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_order = 1 and rsiplate.WATERCRAFT_REGIST_CURRENT.REGIST_EXPIRATION_DATE >= SYSDATE AND rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_USE_CODE = 3 AND rsiplate.ADDRESS.COUNTY_NUMBER = :CountyPK GROUP BY rsiplate.OWNER.work_area_code, rsiplate.OWNER.work_phone_number, rsiplate.OWNER.first_name, rsiplate.OWNER.middle_name, rsiplate.OWNER.last_name, rsiplate.ADDRESS.address_line1, rsiplate.ADDRESS.address_line2, rsiplate.ADDRESS.city_name, rsiplate.ADDRESS.state_code, rsiplate.ADDRESS.zip_code;
But I still get this:
SP2-0317: expected symbol name is missing
old 2: iplate.WATERCRAFT_REGIST_CURRENT.watercraft_id and rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_id and rsiplate.WATERCRAFT_OWNER_CURRENT.owner_id = rsiplate.OWNER.owner_id and rsiplate.WATERCRAFT_MASTER.watercraft_owner_group = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_group and rsiplate.ADDRESS.address_set_id = rsiplate.OWNER.address_set_id and rsiplate.WATERCRAFT_MASTER.data_status_code = 1 and rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_order = 1 and rsiplate.WATERCRAFT_REGIST_CURRENT.REGIST_EXPIRATION_DATE >= SYSDATE AND rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_USE_CODE = 3 AND rsiplate.ADDRESS.COUNTY_NUMBER = :CountyPK GROUP BY rsiplate.OWNER.work_area_code, rsiplate.OWNER.work_phone_number, rsiplate.OWNER.first_name, rsiplate.OWNER.middle_name, rsiplate.OWNER.last_name, rsiplate.ADDRESS.address_line1, rsiplate.ADDRESS.address_line2, rsiplate.ADDRESS.city_name, rsiplate.ADDRESS.state_code, rsiplate.ADDRESS.zip_code
new 2: iplate.WATERCRAFT_REGIST_CURRENT.watercraft_id and rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_id and rsiplate.WATERCRAFT_OWNER_CURRENT.owner_id = rsiplate.OWNER.owner_id and rsiplate.WATERCRAFT_MASTER.watercraft_owner_group = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_group and rsiplate.ADDRESS.address_set_id = rsiplate.OWNER.address_set_id and rsiplate.WATERCRAFT_MASTER.data_status_code = 1 and rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_order = 1 and rsiplate.WATERCRAFT_REGIST_CURRENT.REGIST_EXPIRATION_DATE >= SYSDATE AND rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_USE_CODE = 3 AND rsiplate.ADDRESS.COUNTY_NUMBER = 01 GROUP BY rsiplate.OWNER.work_area_code, rsiplate.OWNER.work_phone_number, rsiplate.OWNER.first_name, rsiplate.OWNER.middle_name, rsiplate.OWNER.last_name, rsiplate.ADDRESS.address_line1, rsiplate.ADDRESS.address_line2, rsiplate.ADDRESS.city_name, rsiplate.ADDRESS.state_code, rsiplate.ADDRESS.zip_code
iplate.WATERCRAFT_REGIST_CURRENT.watercraft_id and rsiplate.WATERCRAFT_MASTER.watercraft_id = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_id and rsiplate.WATERCRAFT_OWNER_CURRENT.owner_id = rsiplate.OWNER.owner_id and rsiplate.WATERCRAFT_MASTER.watercraft_owner_group = rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_group and rsiplate.ADDRESS.address_set_id = rsiplate.OWNER.address_set_id and rsiplate.WATERCRAFT_MASTER.data_status_code = 1 and rsiplate.WATERCRAFT_OWNER_CURRENT.watercraft_owner_order = 1 and rsiplate.WATERCRAFT_REGIST_CURRENT.REGIST_EXPIRATION_DATE >= SYSDATE AND rsiplate.WATERCRAFT_CURRENT.WATERCRAFT_USE_CODE = 3 AND rsiplate.ADDRESS.COUNTY_NUMBER = 01 GROUP BY rsiplate.OWNER.work_area_code, rsiplate.OWNER.work_phone_number, rsiplate.OWNER.first_name, rsiplate.OWNER.middle_name, rsiplate.OWNER.last_name, rsiplate.ADDRESS.address_line1, rsiplate.ADDRESS.address_line2, rsiplate.ADDRESS.city_name, rsiplate.ADDRESS.state_code, rsiplate.ADDRESS.zip_code
ERROR at line 2:
ORA-00933: SQL command not properly ended -
Using Bind variables in SQL PLUS Report
using Bind variables in SQL PLUS Report. This report gets the arguments from the application concurrent program. Now my need is to convert the start_date and end_date to bind Variables to improve the performance. I have commented the original code in 'prompt List of Unapproved Adjustments' and used my Bind Variable but it is giving an error
error: Bind Variable "ENDING_DATE" not declared
Report Date and Time:
26-OCT-2010 15:44:13
List of Unapproved Adjustments
Bind Variable 'ENDING_DATE" not declared
Please see below the code for the sql plus report:
define p_org_id = '&1'
define p_fy_begin_date = '&2'
define p_start_date = '&3'
define p_end_date = '&4'
define p_conversion = '&5'
declare
variable begin_date date;
exec :begin_date := p_start_date;
variable ending_date date;
exec :ending_date := p_end_date;
/* Begin
:begin_date := to_date('&p_start_date','YYYY/MM/DD HH24:MI:SS');
:ending_date := to_date('&p_end_date','YYYY/MM/DD HH24:MI:SS');
End; */
set newpage none
set termout off
set pagesize 55
set linesize 180
set heading on
set feedback off
set wrap off
set space 1
set heading on
begin
dbms_application_info.set_client_info('&p_org_id');
end;
prompt
prompt Report Date and Time:
prompt ----------------------
select to_char(sysdate,'DD-MON-YYYY HH24:MI:SS')
from dual ;
prompt
prompt List of Unapproved Adjustments
prompt -------------------------------
select b.trx_number,
a.adjustment_number,
f.user_name created_by
from apps.ar_adjustments a,
apps.ra_customer_trx b,
apps.fnd_user f
where a.customer_trx_id = b.customer_trx_id
and a.status <> 'A'
and a.created_by = f.user_id
and a.creation_date between :begin_date
and :ending_date
-- and a.creation_Date between to_date('&p_start_date','YYYY/MM/DD HH24:MI:SS')
-- and to_date('&p_end_date','YYYY/MM/DD HH24:MI:SS')
order by
b.trx_number ;Hi
Please go to customization part of the report and verify..You have set a default value out there ..And also verify your lov and look at the values ..If it is again giving you the problem ..pl delete the report and develop it again from the scratch it will be solved...
vishnu
null -
Windows System Environment Variables in "Sql * plus"
Can i use/reference Windows System Environment variables in "Sql * plus"?
For example, i want to create sql-script to run in database server computer that asks variable "ORA_HOME" and uses this value to execute some sql/plsql sentences.
Oracle 10g Personal, Windows 7.
Edited by: CharlesRoos on 12.11.2010 17:28CharlesRoos wrote:
Business problem:
I have created 2 databases in my computer. Both databases needs tablespaces created by a script. Tablespaces' datafiles (.dbf files) names are same for both database. Both database has it's own directory where it holds datafiles at the moment. At the moment the datafiles for Database1 are in folder something like "%ORACLE_HOME%"\oradata\%databasename1%\*.dbf, and second database has its datafiles in other folder, somewhere ""%ORACLE_HOME%"\oradata\%databasename1%\*.dbf". I want now the script to create tablespace called "INDX" with same datafile name "indx1.dbf" into both database. So into both mentioned folder the file "indx1.dbf" must be created by script. I think the script should do following:
1. get ORACLE_HOME.
2. connect to database "databasename1"
3. EXECUTE IMMEDIATE "Create TableSpace INDX....file=%ORACLE_HOME% || databasename1 || indx1.dbf"
4. connect to database "databasename2"
5. EXECUTE IMMEDIATE "Create TableSpace INDX....file=%ORACLE_HOME% || databasename2 || indx1.dbf"I don't have Oracle database near by anymore, so the code was pseudocode.
I don't understand how to use ?-shortcut.OK, my first impression is "why does this even NEED to be scripted? Creation of new tablespaces is usually a one-off operation.
But that aside how about this sqlplus command-line substitution variables. This example is in linux, but will work as well in Windows with the change of the way environment variables are referenced:
*nix - echo $myvariable
Windows - echo %myvariable%
First, the sqlscript to create the TS. Note the use of the substitution variable "&1"
{code}
[oracle@vmlnx01 ~]$ cat cat makets.sql
set echo on feedback on verify on trimsp on
prompt &1
CREATE SMALLFILE TABLESPACE EDSTEST
DATAFILE '/ora01/oradata/&1/edstest.dbf'
SIZE 5M
REUSE
AUTOEXTEND ON
NEXT 1280K
MAXSIZE 32767M
LOGGING
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO
drop tablespace edstest
including contents and datafiles
exit
{code}
So, at the OS prompt: Notice that the @ is separated by a space, makeing it a command line parm instead of part of the connect string
{code}
[oracle@vmlnx01 ~]$ export myparm=vlnxora1
[oracle@vmlnx01 ~]$ sqlplus system/pswd @makets $myparm
SQL*Plus: Release 10.2.0.4.0 - Production on Fri Nov 12 13:18:05 2010
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> prompt &1
vlnxora1
SQL> --
SQL> CREATE SMALLFILE TABLESPACE EDSTEST
2 DATAFILE '/ora01/oradata/&1/edstest.dbf'
3 SIZE 5M
4 REUSE
5 AUTOEXTEND ON
6 NEXT 1280K
7 MAXSIZE 32767M
8 LOGGING
9 EXTENT MANAGEMENT LOCAL
10 SEGMENT SPACE MANAGEMENT AUTO
11 ;
old 2: DATAFILE '/ora01/oradata/&1/edstest.dbf'
new 2: DATAFILE '/ora01/oradata/vlnxora1/edstest.dbf'
Tablespace created.
SQL> --
SQL> drop tablespace edstest
2 including contents and datafiles
3 ;
Tablespace dropped.
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@vmlnx01 ~]$
{code} -
How to bind DATE variables in SQL*Plus
I have a stored procedure in a package that has a DATE OUT parameter
myPackage.myProcedure( outDate OUT DATE )
I am trying to test this from SQL*Plus
How do I specify a Bind variable for the outDate? The VARIABLE command does not allow DATE types?
If this was a CHAR type I could have done
VARIABLE myChar CHAR
BEGIN
myPackage.myProcedure( :myChar )
END;
PRINT myChar
How do I do this with DATE data types instead? I am using Oracle 8.1.7.you should use VARCHR2 type with the minimum length that can store a value defined by your NLS_DATE_FORMAT
string. The returned date will be converted to a VARCAHR2 type and the size and format will depend on
your NLS_DATE_FORMAT setting.
=============================================================================
SQL> variable dt VARCHAR2(20)
SQL> create or replace procedure ret_date(dt OUT DATE) is
2 begin
3 dt := SYSDATE ;
4 end ;
5 /
Procedure created.
SQL> exec ret_date(:dt) ;
PL/SQL procedure successfully completed.
SQL> print dt
DT
13-OCT-02
SQL> select sysdate from dual ;
SYSDATE
13-OCT-02
SQL> alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS' ;
Session altered.
SQL> exec ret_date(:dt) ;
PL/SQL procedure successfully completed.
SQL> print dt
DT
13-OCT-2002 10:43:23
SQL>
================================================================ -
many times, i am confused by the way to assess a variable defined in sql*plus. can someone give a clear explanation on the difference and when to use which?
&var, &&var, :varnot sure if this will help
SQL> define vDepNo = 20;
SQL> variable vDepNo number;
SQL> execute :vDepNo := &vDepNo;
PL/SQL procedure successfully completed.
SQL> select * from emp
2 where deptno = :vDepNo;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 1000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
SQL> select * from emp
2 where deptno = &&vDepNo;
old 2: where deptno = &&vDepNo
new 2: where deptno = 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 1000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
SQL> -
Using Internet enabled SQL*Plus
I' d like to use this feature just as it is describe in the document from Luan Nim.
I wrote a simple test_sql.sh that work very well under unix.
# test utilisation sqlplus sous Web
#! /usr/bin/sh
# . $HOME/.profile
echo Content-type: text/html
# Mise en place des variables
ORACLE_HOME=/oracle/u01/app/oracle/product/9i/
export ORACLE_HOME
ORACLE_SID=oracle_sid
export ORACLE_SID
#export TNS_ADMIN
#NLS_LANG=FRENCH_FRANCE.WE8ISO8859P1
#export NLS_LANG
#echo $ORACLE_HOME
#echo $ORACLE_SID
# Execution de sqlplus
#echo $ORACLE_HOME/sqlplus -M \"html on\" user/pwd@$ORACLE_SID @./test_sql.sql
sqlplus -M "html on" -S user/pwd@$ORACLE_SID @./test_sql.sql
When calling it from Web Browser (IE5) I get the following error :
403 - Forbidden
You don't have permission to access cgi-bin directory ?
How can I do ?It looks like your web server is not configured to handle
CGI scripts. Does your web server documentation give steps
to configure CGI? Sometimes simple example CGI scripts
are included. Once these are working, then we can help
setting up a SQL*Plus CGI.
If you are using Apache, check the httpd.conf file for the
ExecCGI option. See if you can get the "printenv"
demonstration CGI script working. Then move your SQL*Plus
CGI shell script to the same directory, and try it there.
Don't forget to echo a blank line after the "Content-type:
text/html", else the CGI output will not be interpreted
correctly.
The SQL*Plus 9.2 UG&R has an example of a Perl CGI script
for calling SQL*Plus:
http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/server.920/a90842/ch8.htm#1005951
The iSQL*Plus web-based interface does most of the things
done by a CGI based call to command-line SQL*Plus. Its
"Dynamic Reports" feature is very similar to getting a CGI
script to return a report from a static .SQL file. If you
(i) have Oracle 9.2 (or 9.0 on Windows) installed (ii) don't
mind getting HTML pages back, then it may be a better
alternative to CGI. (Note the SET MARKUP HTML PREFORMAT OFF
command can be used to give traditional character aligned
text, but there will still be HTML page headers and
footers).
-- CJ -
Use file as sql*plus input
Can I use a file as sql*plus input?
I have a query like below:
select * from employees
where deptno = &1
and job = &2
It will prompt me for the values of course.
I can execute it at the command line like below:
sql>@script.sql 10 salesrep
Is is possible to put the values in a file and call the script like below:
sql>@script.sql <filename>
Then this file could be in a directory in the OS and I can add any values to it.
thanks.Hi,
The following script reads a 1-line file and puts its contents into the substitution variable &file_contents:
COLUMN file_contents_col NEW_VALUE file_contents
SELECT TRANSLATE ( '
@&1
, 'A' || CHR (10) || CHR (13)
, 'A'
) AS file_contents_col
FROM dual;So, if my_params.txt contains
10 salesrepthen you can say:
@read_file my_params.txt
@script.sql &file_contentsYou can generalize this by putting the two lines above into a file called caswf.sql:
@read_file &2
@&1 &file_contentsand call it like this
@caswf script.sql my_params.txt"Caswf" is a Welsh word that means "call any script with file". -
Substitution variables in Sql*Plus
Hi all,
How to make Sql*Plus ask for the substitution variable value one time only for a substitution variable even if it appears multiple times inside a query ? Because so far each time I launched the query below then I have been asked two times by Sql*Plus for the variable value. For example : select ref_site from t_comsis where longitude_lamb + latitude_lamb < &distance + power(&distance,2).
Thanksof course accept is more friendly, you can check for valid number, you can have a prompt or a hidden input.
you can also undef your variable if you prefer the && solution
SQL> select &&x+&&x from dual;
Enter value for x: 1
old 1: select &&x+&&x from dual
new 1: select 1+1 from dual
1+1
2
SQL> undef x
SQL> r
1* select &&x+&&x from dual
Enter value for x: 2
old 1: select &&x+&&x from dual
new 1: select 2+2 from dual
2+2
4 -
Hide new/old value of bind variable in SQL*Plus
Hi,
I have a SQL*Plus script to create a new user. It asks for username & password and then passes these values to a SP to do some checks and finally create the user.
The output is currently the following :
New user will be created in ORCL LAB
Enter the requested username : fleet2
Enter the password for fleet2 : *****
Re-enter the password for fleet2 : *****
old 2: labadmin_cr_user('&username', '&pswd', '&pswd2');
new 2: labadmin_cr_user('fleet2', 'fleet', 'fleet');
The requested username is already in use in the database. Please choose another username.
As you can see the passwords are now exposed when the procedure is called. This is not what I want off course. I cannot do termout off as the serveroutput is not shown as well in that case. I use dbms_output to show error messages to users. (see example above in case of an existing user).
Can anybody tell me how to hide these 2 lines (old 2, new 2) without hiding my dbms_output.
Thanks!
Krisset verify offhttp://download-uk.oracle.com/docs/cd/B10501_01/server.920/a90842/ch13.htm#1013382
-
Using variables in sql scripts
I am trying to pass a variable into a simple
sql script - but each time I run it, it still asks for the variable.
Here is my example:
test.sql contains:
select count(*) from &tbl
I try to run it by typing the following
at the sqlplus prompt:
@test the_tbl
Even though I provide a value for &tbl,
sqlplus still asks me for the value:
Enter value for tbl:
I am sure that the answer is very simple.
what am I doing wrong ?
Thanks !<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by KR:
I am trying to pass a variable into a simple
sql script - but each time I run it, it still asks for the variable.
Here is my example:
test.sql contains:
select count(*) from &tbl
I try to run it by typing the following
at the sqlplus prompt:
@test the_tbl
Even though I provide a value for &tbl,
sqlplus still asks me for the value:
Enter value for tbl:
I am sure that the answer is very simple.
what am I doing wrong ?
Thanks !<HR></BLOCKQUOTE>
In SQL Plus 8.0 this command goes correctly:
select count(*) from &tb ;
null -
How to swap column values using variable in sql?
Hi,
I have a table and i want to swap two column values using variable
please help meHi,
Let us assume that the DeptNAME and DeptNo columns are of type VARCHAR2. However, DeptNO column is VARCHAR2(10) and DeptNAMe is VARCHAR2(100).
First of all DeptNo column needs to be modified to be 100.
i) ALTER TABLE DEPT MODIFY DEPTNO VARCHAR2(100);
Secondly, you will swap DEPTNAME and DEPTNO values using the SQL as follows:
ii) update dept t1
set deptname = (select deptno from dept2 t2 where t1.deptno = t2.deptno),
deptno = (select deptname from dept2 t2 where t1.deptno = t2.deptno);
Now, finally you want the deptname columns to be VARCHAR2(10)
iii) ALTER TABLE DEPT MODIFY DEPTNAME VARCHAR2(10).
If DEPTNO is a NUMBER column, you cannot alter the datatype unless the table is empty; in that case the whole swapping requirement would be moot.
Trinath Somanchi,
( http://www.myospages.com ) -
Is it possible to pass or set a variable in SQL LOADER? In this case I want the file name (eg $data) that is getting passed from the command line to load into my table into the extract_date field.
For example. The command line:
sqlldr user/password control=deposit.ctl data=080322.txt
Control file:
Load data
infile '$data'
Append into table deposit
, id position (1-10)
, extract_date date "YYMMDD" $data
Any thoughts?user567866 wrote:
Is it possible to pass or set a variable in SQL LOADER? In this case I want the file name (eg $data) that is getting passed from the command line to load into my table into the extract_date field.
For example. The command line:
sqlldr user/password control=deposit.ctl data=080322.txt
Control file:
Load data
infile '$data'
Append into table deposit
, id position (1-10)
, extract_date date "YYMMDD" $data
Any thoughts?Just wonder, why do you need a variable, if you are passing the filename on the command line? The sqlldr is perfectly capable to read the data from the file given as argument with parameter data. Just remove the line with infile from your controlfile and leave your commandline as is.
Best regards
Maxim -
Using variables in sql session
HI
i want to use lv_file value to spool the file how can i use it
set define off
set serveroutput on size 100000;
variable lv_file varchar2(200)
begin
:lv_file := 'amt_test_config'||to_char(sysdate,'HH24:MI:SS')||'.err';
end;
prompt &&lv_file
spool &&lv_file
thanx
kbone workaround could be something like this:
I changed the date format because windows doesn't accept colon in file name.
SQL> undefine file_nm
SQL> col file_nm new_value file_nm
SQL> select 'amt_test_config'||to_char(sysdate,'HH24_MI_SS')||'.err' file_nm from dual;
FILE_NM
amt_test_config11_58_48.err
SQL> prompt &&file_nm;
amt_test_config11_58_48.err
SQL> host dir amt_test_config11_58_48.err
Volume in drive C has no label.
Volume Serial Number is F881-DE11
Directory of C:\
File Not Found
SQL> spool &&file_nm
SQL> rem NEW FILE CREATED
SQL> spool off;
SQL> host dir amt_test_config11_58_48.err
Volume in drive C has no label.
Volume Serial Number is F881-DE11
Directory of C:\
2007/08/31 11:59 AM 44 amt_test_config11_58_48.err
1 File(s) 44 bytes
0 Dir(s) 22,329,233,408 bytes free
SQL> host type amt_test_config11_58_48.err
SQL> rem NEW FILE CREATED
SQL> spool off;
SQL> undefine file_nm
SQL> col file_nm new_value file_nm
SQL> select 'amt_test_config'||to_char(sysdate,'HH24_MI_SS')||'.err' file_nm from dual;
FILE_NM
amt_test_config11_58_48.err
SQL> prompt &&file_nm;
amt_test_config11_58_48.err
SQL> host dir amt_test_config11_58_48.err
Volume in drive C has no label.
Volume Serial Number is F881-DE11
Directory of C:\
File Not Found
SQL> spool &&file_nm
SQL> rem NEW FILE CREATED
SQL> spool off;
SQL> host dir amt_test_config11_58_48.err
Volume in drive C has no label.
Volume Serial Number is F881-DE11
Directory of C:\
2007/08/31 11:59 AM 44 amt_test_config11_58_48.err
1 File(s) 44 bytes
0 Dir(s) 22,329,233,408 bytes free
SQL> host type amt_test_config11_58_48.err
SQL> rem NEW FILE CREATED
SQL> spool off; -
Use variable in SQL for column name
Hi All,
We want to use a user input as a column name in APEX.
For e.g user will enter "ALLOWABLE_AMOUNT" then the query will be as follows :
select Rule,rule_name,rule_desc,"User Input" from rule_dim
where "User_input" > 100
So here the User_input will be substitued with Allowable_amount. Is this doable using any bind/substitution variables ? I tried ":P2_USER_VARIABLE" and "&P2_USER_VARIABLE." but did not work.
Please advice.Hi Andy,
You do that with an Interactive Report and a Dynamic Action.
I'll assume that you're using APEX 4.2
Here's how:
Create Page 2 with an Interactive Report
Create New Page > Report > Interactive Report > Next > Next
Enter a SQL Select statement: select Rule,rule_name,rule_desc from rule_dim
Next > Create > Edit Page
Create the item P2_USER_VARIABLE
Add Item > Number Field > Next
Item Name: P2_USER_VARIABLE
Next > Next > Next
Source Used: Always, replacing any existing value in session state
Source Type: Static Assignment (value equals source attribute)
Create Item
Create a Dynamic Action to refresh the Interactive Report when P2_USER_VARIABLE is changed
Add Dynamic Action
Name: Refresh IRR
Next >
Event: Lose Focus
Selection Type: Item(s)
Item(s): P2_USER_VARIABLE
Next >
Action: Refresh
Next >
Selection Type: Region
Region: Report 1 (10)
Create Dynamic Action
Add the ALLOWABLE_AMOUNT to the Interactive Report
Report 1
Region Source: SELECT * FROM (select Rule,rule_name,rule_desc, :P2_USER_VARIABLE AS ALLOWABLE_AMOUNT from rule_dim) WHERE ALLOWABLE_AMOUNT > 100
Apply Changes > Apply Changes
Get the Interactive Report to submit P2_USER_VARIABLE
Report 1
Page Items to Submit: P2_USER_VARIABLE
Apply Changes
Change the Heading for ALLOWABLE_AMOUNT in the Interactive Report
Interactive Report
Change the Heading of ALLOWABLE_AMOUNT to &P2_USER_VARIABLE.
Apply Changes
Run
Enter something into the USER VARIABLE field and select something else on the page. Watch the last column update to that value.
Tim.
Maybe you are looking for
-
HT5312 I forgot my answers to my security questions? I need help ASAP
I forgot my answers to my security questions? I need help ASAP
-
Hi, I have moved to aonther organisation , I want the change the E mail ID in SDN .how to go about G.Ganesh Kumar
-
Casting an int to a string to work indexOf method
I'm trying to see what interger value occurs in a specific token. For examplethe token: "6-" the integer 6 occurs. But I have to pass the indexOf method a string. if (tokenCountdown == 0) while(gotGoals != 1) String(varGoal);
-
What is it with the onboard devices on Neo2 Plat.
What is it with the onboard devices on the Neo2 Plat. The sound really really sucks. Bass is like not there at all, other sounds is of LOW quality - Even with the Nforce driver the sound sucks, infact this is the WORST onboard sound I have ever tried
-
We have Version 11.0.2 of Adobe Reader. We are working on a project where we use the post its to tag certain available lots on a map. When we print the doc, the post its do not print. What's the best way to do this? Or, can we even do this with th