IF statement syntax in SQL script view

I need to include a "IF" condition in the "SELECT" section of my SQL script view.
I tried the following syntax's but I get the error 'Incorrect SQL syntax near 'IF'
1.  IF(Revenue <> '0' AND Quantity <> '0', Revenue/Quantity, '0') AS Gross Price
2.  IF(Revenue != '0' AND Quantity != '0', Revenue/Quantity, '0') AS Gross Price
3.  IF(Revenue <> '0' AND Quantity <> '0' THEN Revenue/Quantity ELSE  '0' END) AS Gross Price
4.  IF(Revenue != '0' AND Quantity != '0' THEN Revenue/Quantity ELSE  '0' END) AS Gross Price
My final SQL would read like follows:
SELECT field1, field2, IF(......) AS field3
FROM table1
Can anybody please help with the correct IF statement syntax to be used in the SQL script based view?

Hi Lakshmi,
below is the syntax for IF statement.
IF <bool_expr1> THEN
<then_stmts1>
ELSEIF <bool_expr2>
THEN <then_stmts2>
[ELSE <else_stmts3>]
END IF
eg :
BEGIN
DECLARE found INT := 1;
SELECT count(*) INTO found FROM books WHERE isbn = :v_isbn;
IF :found = 0 THEN
INSERT INTO books VALUES (:v_isbn, 'In-Memory Data Management', 1, 1, '2011', 42.75, 'EUR');
ELSE
UPDATE books SET price = 42.75 WHERE isbn =:v_isbn;
END IF;
END;
Sreehari

Similar Messages

  • Running sql script from pl/sql

    Is there any standard way to run an external sql script from pl/sql
    I really appreciate any assistance.

    If you want, I did start writing a function reading and executing statements out of sql script with utl_file.
    can I issue this command in PL/SQL: EXECUTE IMMEDIATE '@filename.sql';
    the function could be extended for DDL, session setting, etc...
    Regards
    Laurent

  • Where to running SQL Scripts after install of ODAC for Oracle Client?

    I just installed ODAC for Oracle Client on a windows server, that runs a sql server database, for developers to connect a web app to an Oracle/Unix database. The install states to run SQL scripts located in the $ORACLE_HOME\ASP.NET\SQL directory after the install. I'm not sure where to run them? On the ORACLE DB on the Unix server? If someone could bring me up to speed I would appreciate it. Thanks.

    If I do run the scripts, do you know what user they should be run as? The developer are using ASP.NET for connectivity.
    Why there is no Oracle Expert answer this simple question? ?
    After ODAC 11.1.0.7.20 installation completed, there is a message in the last screen:
    *"Run the SQL script located in c:\app\Administrator\product\11.1.0\client_1\ASP.NET\SQL directory for a new installation or an upgrade of Oracle Providers for ASP.NET"*
    The question is: How to run these 9 SQL scripts ?
    In SQL*Plus, which oracle user should I connect to in order to run these 9 scrips, including:
    InstallAllOracleASPNETProviders.sql
    InstallOracleASPNETCommon.sql
    InstallOracleMembership.sql
    InstallOraclePersonalization.sql
    InstallOracleProfile.sql
    InstallOracleRoles.sql
    InstallOracleSessionState.sql
    InstallOracleSiteMap.sql
    InstallOracleWebEvents.sql

  • Underlying SQL or SQL Script for Calculated Views Create Graphically

    Hello,
    Is it possible to view the SQL or SQL script generated for calculated views that are created graphically?

    Hi Mike,
    For your calculation view you will find the corresponding column view created in schema '_SYS_BIC'-->Column Tables->
    <packagename>/<Calculation view> --> Open Definition of this and check the 'create statement'.
    Note: This is a information modeler generated SQL script which will show the generated Calculation Scenario and the respective Column View.
    Regards, Rahul
    Seems the answer was already given by Murali while I typed
    Edited by: Rahul Pant on Mar 2, 2012 5:22 AM

  • View in which all sql scripts are stored

    Hello Guys,
    I am a Oracle DBA (Fresher in the field). I want to know in which view or table all the sql queries are stored as i have a performance tuning problem. I want to know which sql scripts are afffecting the most time to execute which in turn affecting the overall performance of the database. I am working on oracle 10g.

    You should also look at V$SQL_PLAN table for example:
    rem fts report
    rem based on V$SQL_PLAN table
    col operation format a13
    col object_name format a32
    col object_owner format a10
    col options format a15
    col executions format 999,999,999
    column dt new_value today noprint
    select to_char(sysdate,'ddmonyyyyhh24miss') dt from dual;
    set pages 55 lines 132 trims on
    ttitle 'Full Table/Index Scans'
    spool fts&&today
    select
    a.hash_value,a.object_owner,a.object_name, rtrim(a.operation) operation,
    a.options, sum(b.executions) executions, c.bytes, (sum(b.executions)*c.bytes)/(1024*1024) fts_meg
    from
    v$sql_Plan a, v$sqlarea b, dba_segments c
    where
    (a.object_owner=c.owner
    and a.object_name=c.segment_name)
    and a.address=b.address
    and a.operation IN ('TABLE ACCESS','INDEX')
    and nvl(a.options,'NULL') in ('FULL','NULL','FULL SCAN')
    and a.object_owner not in ('SYS','SYSTEM','PERFSTAT','SYSMAN','WKSYS','DBSNMP')
    and b.executions>1
    group by a.hash_value,a.object_owner, a.object_name, operation, options, c.bytes
    order by a.object_owner,a.object_name,operation, options, executions desc
    spool off
    set pages 20
    ttitle off
    It will show you for active SQL what the plans are and some other important stats.
    Mike

  • Out of memory Error while querying SQL Script based Calculation View

    Hi All,
    I wanted to test the performance of Graphical and SQL Script based Calculation views.
    Created Graphical (CA_GRPH) and SQL Script (CA_SQL) Calculation views.
    Analytic View (AN_GRPH) for both Calculation views are the same which is Graphical based (90 Attributes and 5 Measures)
    In Analytic View data foundation I have a Fact table which has 1.5 Billion records and 9 Dimension Tables –collectively 500 million records (7 Attribute Views). 9 Referential joins with cardinality N:1 and 1 Referential join with cardinality N:N.
    I wanted to keep (CA_GRPH) and (CA_SQL) as a base Calculation views and leverage those to create various calculation views (Will be creating different Calc views for respective Business segments)
    In order to test this I have created below calc views on top of base calc views.
    Graphical Based: Created (CA_GRAPH_XYZ) by having CA_GRPH in projection with 30 Calculated Columns. – This retrieves data in 13 secs
    SQL Script Based: Created (CA_GRPH_ABC) by having CA_SQL in projection view with 30 calculated columns – This errors out after 1.50 mins.
    Could not execute 'SELECT "COLUMN_A","COLUMN _B"," COLUMN _C"," COLUMN _D", SUM("COLUMN _REVENUE") AS ...' in 1:50.480 minutes .
    SAP DBTech JDBC: [2048]: column store error:  [2048] column store error: search table error: [1000002] Error executing physical plan: exception 1000002:
    ltt/impl/memory.cpp:63
    Out of memory ; $size$=1507711; $name$=ihm; $type$=pool; $inuse_count$=170104; $allocated_size$=219215007925
    exception 1000002:
    Any suggestion / help in fixing this issue will be greatly appreciated.
    Regards,
    Av

    Hi Raj,
    Thanks for your time, please find edited snap hot of Analytic View (AN_GRPH) below,
    Calculation view(CA_SQL)
            /********* Begin Procedure Script ************/
    BEGIN
           var_out =
         SELECT
                "COLUMN_1"
                "COLUMN_2",
                "COLUMN_84",
                "COLUMN_85;",
                SUM("REVN") AS "REVN",
                SUM("MGN") AS "MGN",
                SUM("ORD_QTY") AS "ORD_QTY",
                SUM("SYS_QTY1") AS "SYS_QTY1",
                SUM("SYS_QTY") AS "SYS_QTY"
    FROM
          "_SYS_BIC"."XYZ/AN_GRPH"
    GROUP BY
                "COLUMN_1"
                "COLUMN_2",
                "COLUMN_84",
                "COLUMN_85";
    END
    /********* End Procedure Script ************/
    Later i have built one more Calculation view(CA_GRPH_ABC) using (CA_SQL)in projection. i have 30 calculated measures in this final calc view. this final calc view is throwing above mentioned error.
    Not sure if i can use SQL script based calc view in graphical based calc views?
    Regards,
    AV

  • Improving SQL Insert statement syntax

    It's about insert statement syntax, in insert statement, unlike in update statement column names and corresponding values are separated in two different sets. With this separation debugging/writing of insert statement is going to be time taking activity. To identify what value is getting stored in any column, first column position needs to be identified and after that by counting commas in the values list value is located. If functions are included in insert statement then counting of commas doesn't help to locate the value.
    In any non trivial application column count in insert statement is going to be very big number and it is unmanageable.
    If column name and value are written next to each other as it is done in cause of UPDATE statement, it is going to drastically reduce the debugging efforts and there by improves the productivity of the developers.
    So I request SQL community please consider having a variant of insert statement in similar lines of update statement. This will simplify the life of millions of developers.
    Edited by: user9110509 on Feb 6, 2010 10:19 AM

    Hi,
    That's a good idea! An optional alternate way of specifying the columns would be handy.
    Perhaps the reason it hasn't been done yet is that most people, like those who have responded to this message, do not find the current syntax much of a problem. We can't be sure if your idea "is going to drastically reduce the debugging efforts" until it is available, but my guess is that it isn't.
    One thing I do to make sure the two lists match is to indent the values directly below the column names, like this:
    INSERT INTO emp (empno, ename,   hiredate,                job)
          VALUES (9876,  'OBAMA', TO_DATE ( '20-JAN-2009'
                              , 'DD-MON-YYYY), 'PRESIDENT');If the list is very long, or individual values are very complicated, then I might start with a list of the column names
    INSERT INTO emp          -- Step 1: not ready to run yet
    (     empno
    ,     ename
    ,     hiredate
    ,     job
    VALUESthen use the editor's copy and paste commands to duplicate that list:
    INSERT INTO emp          -- Step 2: not ready to run yet
    (     empno
    ,     ename
    ,     hiredate
    ,     job
    VALUES
    (     empno
    ,     ename
    ,     hiredate
    ,     job
    ;and then fill in the VALUES section, leaving the copied names as comments:
    INSERT INTO emp          -- Step 3: ready to run
    (     empno
    ,     ename
    ,     hiredate
    ,     job
    VALUES
    (     9876               -- empno
    ,     'OBAMA'               -- ename
    ,     TO_DATE ( '20-JAN-2009'
              , 'DD-MON-YYYY
              )          -- hiredate
    ,     'PRESIDENT'          -- job
    ;

  • SQL script into a CASE statement ?

    Hy,
    can someone tell me, please, if is it possible insert into a case statement (shell unix script) one or more SQL script ?
    For example :
    case aaa in
    1 )
    sqlplus blabla <<!
    update ...
    insert ...
    2 )
    esac
    Thank you very much !
    Andys

    /unix/sqlplus -s username/pwd @file_name.sql
    This will work on unix prompt. use this in shell script and implement. file_name.sql containts update logic.
    I hope it will solve ur problem.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Andys:
    Hy,
    can someone tell me, please, if is it possible insert into a case statement (shell unix script) one or more SQL script ?
    For example :
    case aaa in
    1 )
    sqlplus blabla <<!
    update ...
    insert ...
    2 )
    esac
    Thank you very much !
    Andys<HR></BLOCKQUOTE>
    null

  • SQL Script VS HANA views

    Hi Gurus,
    We are in the process of building the SAP HANA views. As we know there are multiple options
    1) Attribute , Analytic & Calc views
    2) Scripted Calculation views and CE functions.
    I have read many articles and as per SAP Documentation, they say build the information view with  Attribute, analytic and calculation View (Graphical) If it does not suffice they go for scripted calculation view.
    Just wanted to understand. Are there any limitations or any issues faced in the projects with scripted calc views. If we look at the John Appleby tips, it suggests that avoid SQL script unless it cannot be done with graphical views.
    http://scn.sap.com/community/hana-in-memory/blog/2013/12/29/6-golden-rules-for-new-sap-hana-developers
    If we build the views with graphical method, then it seems the parallelism can very well achieved ,means query is split into multiple sub queries and executed in parallel as in Visualize plan.
    If we write the Sql script, can this parallel processing  achieved or not ?  If the requirement can be achieved without writing script which method to choose.
    I have included the 10 to 12 attribute views in analytic view then Calculation view; it seems there may be some performance issue which I am going to check. As we know we can use base table directly in calculation view. What is the best method to use .Can we use these base  tables in calculation view directly or build the attribute views first, then analytic  view and after that build calculation view.
    As we know we use the attribute views from re usability perspective.  Is there any other reason that we need to use attribute views instead of joining the base tables in calc view .
    Regards
    Ram Ramanathaiah

    Thank you Krishna for the answers. I have gone through those links. But there is nowhere consensus opinion that the way we use the views. Some of the answers conflict each other .When we are starting the project we need to make decision about the best approach we wanted to take. If possible I would like to understand more about this from you and other experts.
    1) Build the views with Attribute analytic or calc views or
    2) Build the views only with base tables in calc views or
    3) Build everything using the SQL script /CE functions.
    Regards
    Ram

  • SQL script(column statement) understanding

    hi,
    i am ataching the code of one of the SQL scripts. I am having problem in understanding some of the lines of the code for which i have put a comment, the lines start with the "column" statement and the spool statements. Please help in solving my query.
    regards,
    jatin
    SET LINESIZE 200
    SET PAGESIZE 10000
    --Problem in understanding below 4 lines
    column spool_text_source new_value spool_text
    select 'c:\temp\DAR_' || to_char(sysdate,'YYYYMMDD')
    spool_text_source from dual;
    spool &spool_text..txt
    --query starts here 
    SELECT DISTINCT E.CITY_CODE,A.MOBILE_NO,A.ITEM_TYPE_ID,B.DOCUMENT_NUM,B.DOCUMENT_DT,E.DISTRIBUTOR_NAME
    FROM
    INV_SIM_SERIAL A,INVOICE_HEADER B,INVOICE_DETAIL C,INV_SIM_SERIAL D,DISTRIBUTOR_MASTER E WHERE
    A.DOC_LINE_ID=C.DOC_LINE_ID AND
    B.DOCUMENT_NUM=C.DOCUMENT_NUM AND
    A.SERIAL_ID=NVL(D.OLD_SERIAL_ID,D.SERIAL_ID) AND
    (trunc(A.FIRST_CALL_DT) = trunc(sysdate-1) OR trunc(D.FIRST_CALL_DT)= trunc(sysdate-1)) AND
    NVL(A.OLD_DISTRIBUTOR_ID,A.DISTRIBUTOR_ID)=E.DISTRIBUTOR_ID AND A.ACTIVATE_DT IS NULL
    order by e.distributor_name;
    --query ends here
    spool off
    exit

    Hi
    The part
    column spool_text_source new_value spool_text
    select 'c:\temp\DAR_' || to_char(sysdate,'YYYYMMDD')
    spool_text_source from dual;
    spool &spool_text..txt can be written as
    column spool_text_source new_value spool_text
    select 'c:\temp\DAR_' || to_char(sysdate,'YYYYMMDD') spool_text_source from dual;
    spool &spool_text..txt line 2 and 3 are just one sql-statement
    but back to what it does
    line 1 column ...
    it tells SQL*Plus to get the result from the column spool_test_source of next sql-statement and put into the spool_text variable.
    line 2 the SQL-statment it actuallt create an file name for the spool file and the result is put into the spool_text variable.
    line 3 open the spool file and start spooling output from the script.

  • How to use views in sql script report?

    All all
    Can any one tell how to use views in sql script report?

    Most of the views are based on tables (or other views which are based on tables).
    The view typically shows one org at a time based on the context that is set.
    If you need records for all orgs, you need to use the underlying tables. Oracle typically names the tables with a _all suffix.
    e.g. PO_HEADERS will show records for one org at a time. PO_HEADERS_ALL will show records for all orgs.
    Hope this answers your question,
    Sandeep Gandhi

  • Anyone got a SQL script that outputs Oracle create statements?

    Hi all,
    I've been using a data modeling tool (ER/Studio from Embarcadero Technologies) to design my database (which currently is 11g XE running on Win7). I need to ensure that the data model and database are 100% in sync. My lack of DBA abilities now haunts me. I need to accomplish one of two things.
    1. Run a SQL script that outputs the full compliment of Oracle CREATE statements that define my app's database (BTW, all of my tables are prefixed with "PT_").
    2. Connect ER/Studio with the local 11g XE database.
    Unless someone is familiar with ER/Studio reverse-engineering, I imagine #1 above is the more straightforward solution. Does anybody have such a script they can share with me?
    Thanks so much,
    Kim

    Hi,
    Maybe this blog post helps you.
    http://www.apex-at-work.com/2013/03/generate-ddl-source-code-with-sql.html
    BTW, you are posting in wrong forum as this is Application Express forum and your question do not have anything do with this product.
    Regards,
    Jari
    My Blog: http://dbswh.webhop.net/htmldb/f?p=BLOG:HOME:0
    Twitter: http://www.twitter.com/jariolai

  • Error when calling a stored procedure from a SQL Script

    Apologies if this is a really dumb question but I can't seem to call a procedure in package from a SQL script. I have a simple package.procedure containing a loop to populate a table. I would like to include a call to this procedure from my database install script, that also includes my CREATE and INSERT statements. I run the script using "@install_databae" and the CREATE and INSERT statements run fine. The script gives an error when it reaches the line below:
    exec lazarus.PopulateGridPositions;
    and gives the error.....
    BEGIN lazarus.PopulateGridPositions; END;
    ERROR at line 1:
    ORA-04063: package body "LAZARUS.LAZARUS" has errors
    ORA-06508: PL/SQL: could not find program unit being called: "LAZARUS.LAZARUS"
    ORA-06512: at line 1
    The procedure and package have both compiled without errors and the statement on its own works fine in SQL*Plus.
    I've obviously missed some fundamental concept with scripts and SQL. Please can anybody help me?

    Histon FTM wrote:
    ORA-04063: package body "LAZARUS.LAZARUS" has errors Above, obviously conflicts with the statement that follows:
    >
    The procedure and package have both compiled without errors and the statement on its own works fine in SQL*Plus.I suggest you take a look in the USER_ERRORS view to see, what the errors are.
    And just checking:
    You have schema called LAZARUS, which holds a package named LAZARUS, which holds a procedure called POPULATEGRIDPOSITIONS?
    Edited by: Toon Koppelaars on Oct 1, 2009 5:55 PM

  • SQL script: dynamic table creation

    Hello,
    I should write a sql script to do the following (simplified):
    - Create a table if it doesn't exist yet
    - Select something from the newly created table and do further conditional actions
    I've tried something like that:
    DECLARE
    lv_count INTEGER;
    BEGIN
    DBMS_OUTPUT.enable();
    SELECT COUNT(*) INTO lv_count FROM user_tables WHERE table_name = 'DATABASEINFO';
    IF(lv_count = 0)
    THEN
         DBMS_OUTPUT.PUT_LINE('Create DatabaseInfo');
    EXECUTE IMMEDIATE
         'CREATE TABLE DATABASEINFO(
              ZDatabaseInfoId RAW(16) NOT NULL,
              ZInfo VARCHAR2(30) NOT NULL,
              ZValue VARCHAR2(100) NOT NULL,
    CONSTRAINT DatabaseInfo_U_NC_PK PRIMARY KEY
    ZDatabaseInfoId
    END IF;
    SELECT COUNT(*) INTO lv_count FROM DatabaseInfo WHERE ZInfo = 'anything';
    The select statement fails with error message "Table or view cannot be found". As far as I know, this is because the table is created dynamically and validation of the sql script fails because the table doesn't exist on compile time.
    How to solve that?
    Best Regards
    Andreas

    866121 wrote:
    Hello,
    I should write a sql script to do the following (simplified):
    - Create a table if it doesn't exist yet
    - Select something from the newly created table and do further conditional actions
    I've tried something like that:
    DECLARE
    lv_count INTEGER;
    BEGIN
    DBMS_OUTPUT.enable();
    SELECT COUNT(*) INTO lv_count FROM user_tables WHERE table_name = 'DATABASEINFO';
    IF(lv_count = 0)
    THEN
         DBMS_OUTPUT.PUT_LINE('Create DatabaseInfo');
    EXECUTE IMMEDIATE
         'CREATE TABLE DATABASEINFO(
              ZDatabaseInfoId RAW(16) NOT NULL,
              ZInfo VARCHAR2(30) NOT NULL,
              ZValue VARCHAR2(100) NOT NULL,
    CONSTRAINT DatabaseInfo_U_NC_PK PRIMARY KEY
    ZDatabaseInfoId
    END IF;
    SELECT COUNT(*) INTO lv_count FROM DatabaseInfo WHERE ZInfo = 'anything';
    The select statement fails with error message "Table or view cannot be found". As far as I know, this is because the table is created dynamically and validation of the sql script fails because the table doesn't exist on compile time.
    How to solve that?
    why, oh why, are you dynamically making table?
    DDL should be done only once via static SQL.
    dig a deeper hole & EXECUTE IMMEDIATE the SELECT.

  • Problem while executing script in Toad - How to use '&' in the sql script ?

    I have to execute sql script in toad. Sql script has one insert query in which one insert-value is 'USA & CAN'. When I executed the script in toad by pressing F5, I got a prompt window asking for the value if 'CAN' as it is after the &.
    I tried using[b] {escape '\' } .... but could not resolve the problem.
    Is there any solution or workaround to overcome this problem. I have thousands of records with such values and I have to use sql script only.

    There is an option in TOAD to change this behaviour.
    Look in VIEW/OPTIONS/SQL Editor/
    Uncheck the box for "Scan statements for bound variables before execution".
    In SQL*PLUS it would be SET SCAN OFF
    (desupported version is SET DEFINE OFF)
    Message was edited by:
    Sven Weller

Maybe you are looking for

  • Error while connecting to OIM via Jdeveloper

    Hi, I am getting the below error while executing simple search code. I am executing this code from Jdeveloper 11.1.2.2 and trying to connect to OIM 11gR1 BP04. SOPs shows, I am able to connecto to OIM, however while trying to search users below error

  • Can't use Adobe Media Encoder CS5. It won't encode

    I'm following lessons on Flash Professional CS5 Classroom in a Book and I've just got stuck on lesson 8 because AME won't work. The program opens fine, but when a drag the penguin.mov to it, I get this message: ""Adobe Media Encoder Debug Event. Adob

  • Bulk loading of Customer data into Application

    Hi Guys, I am going on with the development of Teleservice module on a new instance. Now i need to migrate the data on the old instance to the new instance. Please let me know if i have to use only APIs to create the customer into Apps or whether i c

  • How do i copy text added to a protected pdf and save in word.

    how do I copy text added to a protected pdf and save in word?

  • Programming Analog Input Registers for MSeries NI-6221

    I am trying to understand the Preliminary M Series Register Map in order to correctly setup and acquire data from the Analog Input channels of my PCX-6221 card. I have been able to figure out the DIO locations and use them but I am having trouble und