Interactive script in SQL*PLUS

Hi,
I am a student and we need to write a script to be executed in SQL*PLUS.
Script has to execute an insert statement but all values need to be provided during runtime and in the end there should be a question if you wan't to save data. When yes then commit when no then rollback.
Below is my code. Problem is that program asks for variables' values after beginning then it displays all dbms_output.put_line and then continue with pl/sql.
I want to for examle:
- display message 'provide date'
- covvert string date to date type
- when error then exit, when ok then continue
- provide other variables in the same way
- ask for saving.
My code:
SET SERVEROUTPUT ON
set verify off
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
declare
do_save varchar2(240) ;
v_ename varchar2(50);
v_job varchar2(50);
v_sal number;
v_hiredate date;
v_tmp varchar(50);
begin
DBMS_OUTPUT.put_line('Provide name:');
v_tmp := '&val_ename';
v_ename := v_tmp;
DBMS_OUTPUT.put_line('Provide salary:');
v_tmp := '&val_sal';
begin
v_sal := cast(v_tmp as number);
exception when others then
DBMS_OUTPUT.put_line('Incorrect salary format.');
end;
DBMS_OUTPUT.put_line('Provide data:');
v_tmp := '&val_hiredate';
begin
v_hiredate := to_date(v_tmp);
exception when others then
DBMS_OUTPUT.put_line('Incorrect date format.');
end;
INSERT INTO EMP(ENAME, SAL, HIREDATE)
VALUES (v_ename, v_sal, v_hiredate);
DBMS_OUTPUT.put_line('Do you want to save? [Y/N]');
do_save := '&val_save';
do_save := upper(ltrim(substr(nvl(do_save, 'N'), 1,1)));
IF do_save = 'Y' THEN
commit;
DBMS_OUTPUT.put_line('Data saved.');
ELSE
rollback;
DBMS_OUTPUT.put_line('Data rejected.');
END IF;
end;
/

user10613699 wrote:
it is not solving my problem,...Why not? Did you test and also check the documentation link?
SQL> accept DO_SAVE  prompt 'Do you want to save (Y/N)? ';
Do you want to save (Y/N)? Y
SQL> begin
  2    if '&do_save' in ('Y','y') then
  3      commit;
  4    else
  5      rollback;
  6    end if;
  7  end;
  8  /
old   2:   if '&do_save' in ('Y','y') then
new   2:   if 'Y' in ('Y','y') then
PL/SQL procedure successfully completed.
SQL> Edited by: Sven W. on Nov 17, 2008 4:53 PM

Similar Messages

  • How to run multiple scripts in sql*plus?

    I would like to run 2 scripts in sql*plus, how do I do this? I tried the following, but it won't run.
    create table student_new
    AS
    select *
    from student
    insert into student_new
    (last_name, first_name
    values
    (Crop, Jenny)
    The above are the two scripts I want to run, what am I doing wrong.
    Thanks

    Do you have a solution to run multiple scripts continuosly, one after the other as one script.
    Say I have file1.sql file2.sql, file3.sql.
    I want to create a script run.sql, where this will run file1.sql,file2.sql,file3.sql one after other without any one running one after the other like follows:
    run.sql should have
    begin
    @file1.sql
    @file2.sql
    @file3.sql
    dbms_output.put_line(select sysdate from dual);
    end;
    If I run run.sql all the three scripts should be executed successfully and then display the current time . All these files have update statements(50,000 updates each file).
    Very urgetn. Can some one hlpe, please.
    Thanks in advance.

  • Accept input from Shell script in sql*plus

    Hey! Guys..
    i need the following info.
    I am running a shell script from sql*plus. I need to accept a value from shell script into my .sql file.
    thanks..
    Harsh.

    prompt for input, pass to another shell
    # contract_status_prompt.sh
    read udate?"Enter week-ending date in format dd-mmm-yyyy: "
    contract_status_update.sh $udate >$FDWLOG/current/contractstatusupdate`date +%d%h%y`.log
    echo `date`
    # End contract_status_prompt.sh
    Read the variable passed and use in SQLPlus:
    # contract_status_update.sh
    echo "Running contract_status.sh"
    echo "create records for contract_status"
    echo `date`
    echo " "
    echo " date used is "; print $1
    echo " "
    sqlplus <<exit
    @$FDWSQL/sqlparms
    set time on
    prompt *** Set contract_status_period ***
    update contract_status_period
    set period_date = '$1';
    commit;
    exit
    etc.

  • "ORA-1722 Invalid number" error while runing a SQL script in SQL*Plus

    Hello,
    I created a SQL script that is intended to populate tables with
    baseline data. When I run the script in the SQL*Plus program
    that comes with the Oracle 8.1.6 (client), I receive an "ORA-
    1722 Invalid number".
    The error always occurs when SQL*Plus tries to execute the
    following SQL statement:
    insert into components(db_ind,module_id,ext_compid,active) values
    ('cm',modules_seq.currval,1046682,'y');
    The datatypes for the columns on the components table are as
    follows:
    db_ind varchar2(2);
    module_id number;
    ext_compid number;
    active varchar2(1);
    It seems that for some reason Oracle is having trouble
    recognizing 1046682 as a number.
    I also tried the following INSERT statements to see if that
    would work:
    insert into components(db_ind,module_id,ext_compid,active) values
    ('cm',modules_seq.currval,to_number('1046682'),'y');
    insert into components(db_ind,module_id,ext_compid,active) values
    ('cm',modules_seq.currval,'1046682','y');
    but I still receive the same error message
    Is there some NLS setting(s) I need to modify so that Oracle
    does not give me this error? Does anyone know the answer to
    this problem?
    Your help is greatly appreciated.
    Oscar
    (NOTE: I also receive this same error when trying run the
    script in SQL Navigator and Toad programs)

    Please see the following docs.
    R12: JBO-27122 Error Message Clicking On Supplier Accounting Link With Unexpected Error [ID 1218903.1]
    Supplier Management Accounting Link Gives Unexpected Error and JBO-27122 at SELECT * FROM (select pvsa.ADDRESS_STYLE [ID 1340655.1]
    Adding Accounting Information or Operating Unit Information fails with APP-FND-1564: ORACLE error 1722 in FDFGVD [ID 364265.1]
    Thanks,
    Hussein

  • Tracking the order of execution of sql scripts in SQL*Plus

    In our production environment we sometimes have to run some .sql scripts in a particular order. Since the order of execution is important , i have created another .sql file caller caller.sql(shown at the bottom) which will call all the scripts in the right order.
    i thought of putting a exec DBMS_LOCK.SLEEP (5); after the end of every execution of the script so that i can see the
    'Ending script1'message .
    The spooling within the caller script(execute_stack.log) has become meaningless because each script has a spool <filename.log> and spool off within it. These spool logs (for every script) is important for tracking purposes as each script belongs to a different development team and i have to send them the spooled log file after the execution.
    I don't want to see the entire scripts running by in my screen. Since these scripts have their own spooling, i can later check the logs if the scripts where executed properly.
    So i need two things.
    1.I just need to see the following and nothing else in the screen.
    Ending script1
    Ending script2
    Ending script3
    .2. I need to log the order of execution. ie. the execute_stack.log should look like the above.Since there is a spool off within each script, this wouldn't be possible.Right?
    Ending script1
    Ending script2
    Ending script3
    .The caller.sql script which calls all the scripts in the right order
    alter session set nls_date_format = 'DD-MON-YYYY hh24:MI:SS';
    set serveroutput on
    set echo on;
    set feedback on;
    spool execute_stack.log
    @script1.sql
    exec dbms_output.put_line ('Ending script1');
    dbms_output.put_line(chr(10)||chr(10)||'.'||chr(10)||'.'||chr(10));
    exec DBMS_LOCK.SLEEP (5);
    @script2.sql
    exec dbms_output.put_line ('Ending script2');
    dbms_output.put_line(chr(10)||chr(10)||'.'||chr(10)||'.'||chr(10));
    exec DBMS_LOCK.SLEEP (5);
    @script3.sql
    exec dbms_output.put_line ('Ending script3');
    dbms_output.put_line(chr(10)||chr(10)||'.'||chr(10)||'.'||chr(10));
    exec DBMS_LOCK.SLEEP (5);
    @script4.sql
    dbms_output.put_line(chr(10)||chr(10)||'.'||chr(10)||'.'||chr(10));
    exec dbms_output.put_line ('Ending script3');
    commit;
    spool off;Is this a professional way of tracking the execution of .sql scripts?

    Pete_Sg1 wrote:
    Is this a professional way of tracking the execution of .sql scripts?No. There is very little professional about using .sql scripts on a production system - when stored procedures are safer, more robust, easier managed and controlled and secure.. and where a log table can be used to properly log the runtimes (and other stats) of each processing step.
    Let's just take a look at the number of moving parts you need to schedule and run a .sql script. A cron job needs to be configured with the proper environment setting. It needs to run a shell script. That shell script needs to load SQL*Plus. SQL*Plus needs to connect to the database (starting a dedicated server process most likely). SQL*Plus then needs to read a .sql file, parse these commands and either execute these locally (SQL*Plus commands) or remotely (PL/SQL and SQL commands).
    How can this be considered professional when the very same can be achieved with a
    - stored procedure
    - using DBMS_JOB to schedule the procedure for execution
    There are so many things that can go wrong with the first method. And so few things that could go wrong with the last one. No contest as to which method is not only better, but also professional.
    PS. See that you use Windows to run these scripts. It is even worse as it introduces another hardware and software layer making the scenario even more insecure & unsafe with more moving parts that can go wrong or simply fail.

  • How to view full script in sql*plus

    Hi,
    i am having a user who want to view the script for trigger by querying user_triggers in SQL*PLUS. i just remembered that there is some DBMS_ package for doing this. can anyone please provide that package name.
    thanks in advance

    Were you thinking of DBMS_METADATA?
    Have a look at this: http://blogs.ittoolbox.com/database/solutions/archives/ddl-generationoracles-answer-to-save-you-time-and-money-7590

  • After running sql scripts in SQL plus, where are the results(tables) stored

    Hi ,
    I am using oracleDb10g . i have used SQL plus to create a database. I have run 2 sql scripts and constructed the tables , but i dont know where the data is stored and how to make the data into a database, so that i can use it for connection through some UI.
    for example: schema.sql, data.sql script files.
    SQL> start schema.sql
    SQL> start data.sql
    The tables are created.
    Now how can i group those table into a database and name it.(i mean i can create a database using SQL plus, but how to dump the tables into the database created). Because i want to use this database name for connecting to MS SQL, so i need the name.
    Thanks
    babu.

    when you are in Rome, sing with the romans !
    Oracle is different from SQL Server.
    Time to read some basic books.
    --> http://tahiti.oracle.com

  • Can we run a unix script from sql plus

    Hello,
    Is there any possibility to run unix command from sql plus? I would appreciate if you can provide with the syntax.
    Thank You
    KK

    user539616 wrote:
    Hello,
    Is there any possibility to run unix command from sql plus? I would appreciate if you can provide with the syntax.
    Thank You
    KKWithin sqlplus you can run
    1) SQL statements
    2) PL/SQL blocks
    3) any valid sqlplus command.
    SQLplus commands are documented in the SQLPlus reference manual. For 10g this is found at http://docs.oracle.com/cd/B19306_01/server.102/b14357/toc.htm. You might be interested in the HOST command, detailed at http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12026.htm#sthref2314
    Just beware that the HOST command (or the shortcut version of it alluded to by Warren Tolentino) spawns a child process. When control comes back to sqlplus, that child process is gone.
    What is the business problem you are trying to solve? If we knew that we could probably give better advice than blindly giving you the correct technical answer to a narrow question.

  • Problem with executing script in sql*plus

    I have Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production installed
    And I have a script for generating Packages with more than 1000 rows.
    when I try to execute I get this:
    SQL> @c:\gen_pkg\gen_pkg.sql
    Package created.
    977
    The Package Body is not created only Package spec, and
    what could be the problem?
    please, If somebody can help me
    thanks in advance.

    maybe you didn't include the body (maybe aliens took it) :D
    Message was edited by:
    user581409

  • New line in SQL plus

    Hi all,
    I would like to include content with a new line into an column of a table. I would like to run the script through SQL plus but because I want a new line in this content, SQL plus determines it as the end of the query.
    e.g. update <tablename>
    set <columnname> = ' bla bla
    bla bla'
    Is there a way I can get around this?

    Joyce,
    Try:
    SET SQLBL[ANKLINES] ON
    http://download-west.oracle.com/docs/cd/B13789_01/server.101/b12170/ch13.htm#sthref2829
    Alison

  • Cant't submit plsql scripts with sql developer

    good morning
    i try again , submitting the creation of table books alone , and it replies :
    Error starting at line 1 in command:
    CREATE TABLE books (
    book_id VARCHAR2(20),
    title VARCHAR2(50)
    CONSTRAINT title_not_null NOT NULL,
    author_last_name VARCHAR2(30)
    CONSTRAINT last_name_not_null NOT NULL,
    author_first_name VARCHAR2(30),
    rating NUMBER,
    CONSTRAINT books_pk PRIMARY KEY (book_id),
    CONSTRAINT rating_1_to_10 CHECK (rating IS NULL OR
    (rating >= 1 and rating <= 10)),
    CONSTRAINT author_title_unique UNIQUE (author_last_name, title))
    Error report:
    SQL Error: Connexion interrompue
    could you help me please ?
    thanks.

    Looks like a straight permissions issue. Can you select from tables? Do you have Connect access? Can you paste that script into SQL*Plus as the same user and create the table there?
    Sue

  • SQL*Plus vs web/browser admin interface for 10g Express

    hello, I'm a newbie in Oracle. Seem like I executed the setup script using SQL*Plus successfully (the scrpt CREATE TABLE then INSERT setup data) but seems like Oracle 10g Express web/browser interface tells me the setup data has never been inserted into the tables created?!
    SQL*Plus: Release 10.2.0.1.0 - Production on Mon Apr 6 14:10:05 2009
    +                         Copyright (c) 1982, 2005, Oracle. All rights reserved.+
    +                         SQL> connect dev/dev+
    +                         Connected.+
    +                         SQL> set serveroutput on+
    +                         SQL> @C:\dev\...sql\Oracle\SetupSchema\setup.oracle.tables.sql+
    +                         1125 /+
    +                         PL/SQL procedure successfully completed.+
    I verified this from SQL*Plus environmet:
    SQL> select count(1) from GenericCombolistItem
    +2 /+
    COUNT(1)
    +506+
    I then closed SQL*Plus and tried to browse via :
    http://127.0.0.1:8081/apex/f?p=4500:1003:480765345738397::NO:1003::
    (Start>All Programs>Oracle Database 10g Express Edition>Go to Database Home Page)
    I ran the count(1) SQL again just to check. The table is empty?! I logged into SQL*Plus AND 10g web interface using same credential dev/dev. Not sure what's happening why I can see data from SQL*Plus but not web/browser admin interface

    GOT IT! --- I needed COMMIT

  • Accepting User input in SQL*Plus

    I am writing a SQL script in SQL*Plus that accepts a value from the user and plugs that value into a variable that exist in several locations outside of the PL/SQL block.
    I am able to do this, yet everytime this variable is encountered, the user is prompted for input. I would like to have the user prompted only once at the beginning and then use that value throughout. This seems like a simple task, yet I cannot get it to work.
    Any help would be greatly appreciated.

    You can use &&<variable_name> and it will define the variable and use it throughout the SQL code.

  • Procedure works in sql plus, shows error in sql dev

    I create a package spec and body via a script in sql plus. Following is part of script:
         procedure updateMetricValue(p_metricValueId_in IN number,
         p_username_in IN metricvalue.entryBy%TYPE,
         p_value_in IN VARCHAR2)
         is
         BEGIN
              UPDATE MetricValue
              SET modifiedBy = p_username_in,
                   value = CAST(p_value_in as NUMBER(17,4))
              WHERE id = p_metricValueId_in;
         END updateMetricValue;
    No errors in sqlplus. Runs fine. When I look at the package in SQL Developer 1.0, it shows an error: "expecting identifier" just before the 4 in the line:
    value = CAST(p_value_in as NUMBER(17,4))
    What am I missing?
    Using 9.2.0.1.0 on windows XP as development database.

    I create a package spec and body via a script in sql plus. Following is part of script:
         procedure updateMetricValue(p_metricValueId_in IN number,
         p_username_in IN metricvalue.entryBy%TYPE,
         p_value_in IN VARCHAR2)
         is
         BEGIN
              UPDATE MetricValue
              SET modifiedBy = p_username_in,
                   value = CAST(p_value_in as NUMBER(17,4))
              WHERE id = p_metricValueId_in;
         END updateMetricValue;
    No errors in sqlplus. Runs fine. When I look at the package in SQL Developer 1.0, it shows an error: "expecting identifier" just before the 4 in the line:
    value = CAST(p_value_in as NUMBER(17,4))
    What am I missing?
    Using 9.2.0.1.0 on windows XP as development database.

  • Input truncated to 3 characters in Sql*Plus

    Hi All,
    I ran .sql script in SQL*PLUS but i got one error.
    error is Input truncated to 3 characters.
    pls help me..

    Hi,
    Read > this one.
    Twinkle

Maybe you are looking for

  • Publisher11g - no return data with a data model from logical query

    Hi, i am usign publisher11g with obiee bi server autenthication. On rpd i define a security model with a user variable, a initialization block and a where over one fact table. When i create a answer report, bi server automatically include on the wher

  • HTTP connection to ABAP Error

    What is this error"HTTP connection to ABAP Runtime failed. Error: 403 Forbidden URL: http://dst01pi:8001/sap/xi/simulation?sap-client=100 User: PIDIRUSER" ?

  • Photoshop Print Crash

    HI, I kow that some people have had this issue, but Photoshop is crashing when I CTRL+P print. I'm sure I have read somewhere that, in order to avoid this crash, you need to save a duplicate of the PSD file and then re-open it in Photoshop in order t

  • How to create a COOL entrance page like this using action Script 3

    Hey dudes! so i have a project where i will be making a website. I will be using flash to make it interactive and visually appealing. I found a website with a really cool entrance page, please view it here:http://www.dvf.com/dvf/ i don't really know

  • Adobe Reader XI & iFilter indexing

    In Adobe Reader X they removed the ability to do iFilter indexing from the Windows Indexing services of PDF files.  To get around this you had to install Adobe Reader 9.x on 32bit systems or the iFilter 9 on 64bit systems. Does anyone know if Adobe R