PL SQL using variable in SQL statement

I am trying to execute several sql statements, that have the same format but different values. Does this
mean I need a bind variable?
ie
select TO_CHAR ( (SYSDATE - 2), 'YYYY_MM_DD') from dual
select TO_CHAR ( (SYSDATE - 4), 'YYYY_MM_DD') from dual
select to_char(add_months(sysdate,-2*1) from dual
When I try to put the values into a varaiable (date, varchar2 or number) I am getting a conversion
error.
Can somebody show me an example of how to do something like this? Or at least point me to the correct
part of the documentation that provides and example. Pardon my syntax as I know it is incorrect
val :=add_months(sysdate,-2*1
select to_char(val) from dual
Thanks in advance to all who answer

Hi,
840386 wrote:
I am trying to execute several sql statements, that have the same format but different values. Does this
mean I need a bind variable?No, you don't need a bind variable, though bind variables may be more efficient than using PL/SQL variables. I don't see where you're trying to use any varibables at all in your examples. Is it in place of the literals, such as 2 or 'YYYY_MM_DD'? You can use either bind varibales or PL/SQL variables in those places.
ie
select TO_CHAR ( (SYSDATE - 2), 'YYYY_MM_DD') from dual
select TO_CHAR ( (SYSDATE - 4), 'YYYY_MM_DD') from dual
select to_char(add_months(sysdate,-2*1) from dualIn PL/SQL, when you have a SELECT statement, you must specify what you want to do with the results. For example, a SELECT ... INTO statement:
SELECT  AVG (sal)
INTO    avg_salary
FROM    scott.emp;There's usually no point in SELECTing from dual in PL/SQL. It's not an error, but it's simpler just to use an assignment statement.
When I try to put the values into a varaiable (date, varchar2 or number) I am getting a conversion
error.Post a complete procedure or anonymous block, including variable declarations, that shows exactly what you're trying to do.
>
Can somebody show me an example of how to do something like this? Or at least point me to the correct
part of the documentation that provides and example. Pardon my syntax as I know it is incorrect
val :=add_months(sysdate,-2*1Assuming val is a DATE, that's basically correct. You have unbalanced parentheses (there's a '(', but no matching ')' ), and you need a semicolon (';') at the end of the statement. Perhaps ');' just got cut off when you were posting this.
select to_char(val) from dualAgain, SELECTing from dual is unnecessary, but if you had some way to catch the returned value, that would work.
Usually, the reason why you need to call TO_CHAR is that you want a value in a particular format, which is specified in the 2nd argument to TO_CHAR. Calling TO_CHAR with only one argument is a possible mistake, but not something that would raise an error.
Here's an example that works:
SET     SERVEROUTPUT     ON
DECLARE
     d     DATE;
     v     VARCHAR2 (30);
BEGIN
     d := ADD_MONTHS (SYSDATE, -2);          -- -2 = (-2*1)
     v := TO_CHAR (d, 'DD-Mon-YYYY');
     dbms_output.put_line (v || ' = v');
END;
/Output (when run on March 13, 2011):
13-Jan-2011 = v

Similar Messages

  • 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 me

    Hi,
    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 )

  • Using variables in SQL Loader

    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

  • Maxl - can you use variables in iferror statements

    hi guys,
    A quick one:
    Can you use variables when doing error handling? I assume the answer is yes, but just wanted to make sure.
    example:
    set app1 = Test;
    set app1_db1 = Test;
    alter database $app1.$app1_db1 force archive to file "C:\\EssbaseBackup\\$app1\\$app1_db1\\$app1_db1.arc";
    iferror 'Bk_$app1.$app1_db1_Error';
    define label 'Bk_$app1.$app1_db1_Error';
    exit 10;
    I am putting error handling into my script and want to double check before I go about making the alterations.
    Many thanks,
    Russell
    Edited by: RussellH-C on 26-Apr-2013 04:45

    You don't need to split the files. Create a Maxl with parameters and then call it in a bat file.
    In the bat you can then check for Error.
    essmsh -D "%HYP_MONTH_SCRIPTS_DIR%\Maxl_Files\HYP_ESS_Archive.msh" %DECRYPTKEY% Appname1 Dbname1
    call :archiveerrcheck
    essmsh -D "%HYP_MONTH_SCRIPTS_DIR%\Maxl_Files\HYP_ESS_Archive.msh" %DECRYPTKEY% Appname2 Dbname2
    call :archiveerrcheck
    archiveerrcheck:
         SET FILE=%MAXLLOG_DIR%\Archive.log
         findstr /R "\<ERROR" %FILE% > ESSErrCheck
          for /F %%A in ("ESSErrCheck") do If %%~zA NEQ 0 (
            call :datestamp
            echo  Error in Archive Process......................... > %ESSLOG%
            copy %FILE% %HYP_MONTH_ERR_DIR%\Essbase\MaxL\Maxl_Error.log
            DEL /Q ESSErrCheck
            call :exit_process
           ) else (
                   echo  %DTSTMP%  Log Out Process was successfull......................... >> %ESSLOG%
                   call :remove_file ESSErrCheck
    GOTO EOF
    :EOFRegards
    Celvin
    http://www.orahyplabs.com

  • Use variable in SQL statement

    HI guys:
    I need code three SQL statements.the returned field and selected table are all same,there is only a difference in their "where" statement.
      Sample code:
        select marcmatnr marcwerks
        into table it_data
        from MARC inner join MBEW on marcmatnr = mbewmatnr
        where marcmatnr like 'A%' and mbewzplp1 = '001'.
        second one........................ mbew~zplp2 = '001'
        third one......................... mbew~zplp3 = '001'
      Could I write a FORM gather them with transporting a parameter ZPLPX to determine which condiniton will be execute?
    thank you very much.

    Hi tianli,
    1. source text
       This concept of dynamic where
       condition is called source text.
    2. use like this.
       This is important in the code  --->  WHERE (mywhere).
    REPORT abc LINE-SIZE 80.
    DATA : it_data LIKE TABLE OF mara WITH HEADER LINE.
    QUOTES ARE IMPORTANT
    PERFORM mysql USING 'mbew~zplp2 = ''001'''.
    FORM mysql USING mywhere.
      SELECT marcmatnr marcwerks
      INTO TABLE it_data
      FROM marc INNER JOIN mbew ON marcmatnr = mbewmatnr
      WHERE (mywhere).
    ENDFORM.                    "mysql
    regards,
    amit m.

  • HELP - using variables in SQL statement

    Hi,
    I am sure that this is a small problems for the gurus out there.
    I am trying to execute the following statement usign JDBC:
    String myVariable = "XYZ";
    sSQL = "select COL1, COL2 from TABLE1 where COL3 = '" + myVariable + "'";
    CODE ABOVE DOES NOT WORK FOR ME!!!!!
    HOWEVER, if I were to directly substitute the value XYZ as shown below, then things work. But that does not solve my problem.
    sSQL = "select COL1, COL2 from TABLE1 where COL3 = '" + "XYZ" + "'";
    Any suggestions?
    null

    Use methods setXXXX of the PreparedStatement class.
    Connection conn =
    DriverManager.getConnection("jdbc:default:connection:");
    PreparedStatement stm=conn.prepareStatement(
    "select name from persons where inn=?");
    String inn="ASDDGF12345";
    String name=new String;
    stm.setString(1,inn);
    ResultSet rss = stm.executeQuery();
    if (rss.next())
    name = rss.getString(1);
    rss.close();
    stm.close();
    conn.close();

  • 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

  • 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.

  • Native SQL using variables?

    Hi folks,
    Is it possible to assign a SQL query to a variable, and then using an OPEN statement to read the variable?
    E.g.,
    query TYPE STRING VALUE 'SELECT * FROM mytable'.
    EXEC SQL.
          OPEN c1 FOR :query
    ENDEXEC.

    OK, I've tried to narrow this length of string down to the following:
    data mystring type string.
    "SELECT ... ... ... WHERE" ... and then "mystring" needs to be appended to the end of the SQL statement.
    E.g., mystring could be " field1 = '1234' AND field2 = 'XYZ' "
    Is this possible? Thanks in advance for reading.

  • 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
    kb

    one 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;

  • 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>

  • 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

  • Pl/sql bound variable and SQL statement

    Hi,
    I'm using the Additional pl/sql tab to display the outcome of an sql query in the report header as adding <#MYCOLUMN.FIELD#> in the header section does not work.
    The code I have is:
    loop
    if (l_arg_names(i) = 'carry_forumid') then
    htp.p('The Forum is '||l_arg_values(i));
    end if;
    end loop;
    However, I need to query the outcome of the loop:
    declare
    store_forum_name varchar2(32767);
    begin
    select name into store_forum_name from df.forum where forum.id = l_arg_names(i);
    htp.p(store_forum_name);
    end;
    Does anyone know how the combine the two? Just not sure how to arrange the code/order it should be written. Thanks for the help.

    Hi,
    I'm using the Additional pl/sql tab to display the outcome of an sql query in the report header as adding <#MYCOLUMN.FIELD#> in the header section does not work.
    The code I have is:
    loop
    if (l_arg_names(i) = 'carry_forumid') then
    htp.p('The Forum is '||l_arg_values(i));
    end if;
    end loop;
    However, I need to query the outcome of the loop:
    declare
    store_forum_name varchar2(32767);
    begin
    select name into store_forum_name from df.forum where forum.id = l_arg_names(i);
    htp.p(store_forum_name);
    end;
    Does anyone know how the combine the two? Just not sure how to arrange the code/order it should be written. Thanks for the help.

  • Using variable in select statement (php)

    I am having difficulty using a variable in a select
    statement.
    The following manual query (using a static date) works fine:
    $query_Recordset1 = "SELECT * FROM academyClasses WHERE
    classDate > '2006-06-01' ";
    However, If I use the following:
    $dateVar = date('Y-m-d');
    mysql_select_db($database_mw, $mw);
    $query_Recordset1 = "SELECT * FROM academyClasses WHERE
    classDate > $dateVar ";
    ALL records are returned, which means the $dateVar variable
    is not being recognized. I have compared the $dateVar values
    <?php echo $datetime; ?> against the actual value of my date
    field <?php echo $row_Recordset1['classDate']; ?> in my
    results table and it seems that the values are indeed accurate in
    terms of a date 2006-06-04 for example.
    My date field "classDate" is of a "date" type within the
    mysql database. I'm using Dreamweaver MX 2004 with Mac OSX 10.3.9.
    I'm sure this is just a syntax problem, at least I hope it
    is.
    Any help is greatly appreciated.

    On Wed, 7 Jun 2006 04:49:57 +0000 (UTC), "mgwaters"
    <[email protected]> wrote:
    >Thanks Gary. That did seem to get the select statement
    working, but I had to
    >enter a manual date as before $dateVar = '2006-06-01'; on
    the previous line of
    >code to actually get it to filter my records. So... it
    looks like my setting of
    >the date variable:
    > $dateVar = date('Y-m-d');
    > does not seem to be recognized within the SELECT
    statement.
    Try this:
    $query_Recordset1 = "SELECT * FROM academyClasses WHERE
    classDate>'$dateVar'";
    print $query_Recordset1;
    See what is in the SQL statement.
    Gary

  • Passing PL/SQL varchar2 variable to XML Query

    Hi guys,
    i'm having trouble passing a pl/sql varchar2 variable to an XMLQuery.
    This is the relevant part of my code:
    DECLARE
       lo_result   XMLTYPE;         --contains the XML which is being parsed below
       lo_return   XMLTYPE;             -- for the XML result returned by XMLQuery
       lo_start    VARCHAR2 (100) DEFAULT 'Toronto'; -- a PL/SQL varchar2 variable
       lo_end      VARCHAR2 (100) DEFAULT 'Ottawa';  -- a PL/SQL varchar2 variable
    BEGIN
       SELECT XMLQUERY (
                 'for $i in //leg[start_address[text()[contains(.,$lo_start)]] and end_address[text()[contains(.,$lo_end)]]]/distance/value/text() return ($i)'
                 PASSING lo_result, lo_start AS "lo_start", lo_end AS "lo_end"
                 RETURNING CONTENT)
         INTO lo_return
         FROM DUAL;
    END;The XPath expression is correct but it doesn't seem to accept my variables since lo_return is empty.
    I think the variables should be of type Xmltype but the compiler won't let me convert them because they do not contain any XML tags .
    Hope anyone can help.
    Thanks,
    Martina

    may be i missed anything but
    >
    <start_address>Toronto, Ontario, Kanada</start_address>
    <end_address>Huntsville, Alabama, Vereinigte Staaten</end_address>
    >
    and
    >
    lo_start VARCHAR2 (100) DEFAULT 'Toronto'; -- a PL/SQL varchar2 variable
    lo_end VARCHAR2 (100) DEFAULT 'Ottawa'; -- a PL/SQL varchar2 variable
    >
    so
    SQL> SELECT XMLQUERY (
      2              'for $i in //leg[start_address[text()[contains(.,$lo_start)]] and end_address[text()[contains(.,$lo_end)]]]/distance/value/text() return ($i)'
      3               PASSING
      4               xmltype('<DirectionsResponse>
      5    <status>OK</status>
      6    <route>
      7      <summary>I-75 N</summary>
      8      <leg>
      9        <duration>
    10          <value>48220</value>
    11          <text>13 Stunden, 24 Minuten</text>
    12        </duration>
    13        <distance>
    14          <value>1404935</value>
    15          <text>1.405 km</text>
    16        </distance>
    17        <start_location>
    18          <lat>43.6533100</lat>
    19          <lng>-79.3827700</lng>
    20        </start_location>
    21        <end_location>
    22          <lat>34.7303300</lat>
    23          <lng>-86.5860700</lng>
    24        </end_location>
    25        <start_address>Toronto, Ontario, Kanada</start_address>
    26        <end_address>Huntsville, Alabama, Vereinigte Staaten</end_address>
    27      </leg>
    28    </route>
    29  </DirectionsResponse>'),
    30               'Toronto' AS "lo_start", 'Ottawa' AS "lo_end"
    31               RETURNING CONTENT)
    32       FROM DUAL;
    XMLQUERY('FOR$IIN//LEG[START_A
    SQL>
    SQL>
    SQL>
    SQL> SELECT XMLQUERY (
      2              'for $i in //leg[start_address[text()[contains(.,$lo_start)]] and end_address[text()[contains(.,$lo_end)]]]/distance/value/text() return ($i)'
      3               PASSING
      4               xmltype('<DirectionsResponse>
      5    <status>OK</status>
      6    <route>
      7      <summary>I-75 N</summary>
      8      <leg>
      9        <duration>
    10          <value>48220</value>
    11          <text>13 Stunden, 24 Minuten</text>
    12        </duration>
    13        <distance>
    14          <value>1404935</value>
    15          <text>1.405 km</text>
    16        </distance>
    17        <start_location>
    18          <lat>43.6533100</lat>
    19          <lng>-79.3827700</lng>
    20        </start_location>
    21        <end_location>
    22          <lat>34.7303300</lat>
    23          <lng>-86.5860700</lng>
    24        </end_location>
    25        <start_address>Toronto, Ontario, Kanada</start_address>
    26        <end_address>Huntsville, Alabama, Vereinigte Staaten</end_address>
    27      </leg>
    28    </route>
    29  </DirectionsResponse>'),
    30               'Toronto' AS "lo_start", /*'Ottawa'*/'Huntsville' AS "lo_end"
    31               RETURNING CONTENT)
    32       FROM DUAL;
    XMLQUERY('FOR$IIN//LEG[START_A
    1404935
    SQL>

Maybe you are looking for

  • Error while opening the database.--urgent---pls help...

    Hi, Got the below error while opening the database. Database version is :10.1.0.4.0 database is in noarchive log mode. the database is coming in to no-mount state and mount state quite easily but not able to open the database. Got the below error whi

  • HT5361 How to move saved messages in outlook to mail

    Could someone tell me how I can move all my saved messages from my Outlook into my mail as I wish to only use mail in future. Many thanks

  • Clean Install Yosemite onto new SSD (Also, what is TRIM?)

    Hi, I've been researching how to replace the Hard Drive on my Late 2011 13inch Macbook Pro (MacBookPro8,1 / 2.4 GHz / Intel Core i5 / SATA III) with a new SSD (maybe a Samsung 840 EVO) and clean install Yosemite onto it. I've found a few ways to do t

  • Ora-0059: max no.of db_files exceeded

    hi. my db is in archivelog mode(24x7). i tried to add data file to my tablespace i got the following error ora-oo59: max no of db_files exceeded. plz tel me step by step how to change the db_files parameter in what state when i have to take backup of

  • Poor inventory control

    Several days ago I noticed that the Diablo 3 expansion, Reaper of Souls (PC), was on sale. BestBuy.com showed that none were available in my area yet. I checked yesterday evening and it showed that there was one available as of close of business on 8