Conditional Stored Procedure

how can i run an update procedure only if 2 fields values
already exist, in which case i would run an insert procedure?
i am using asp vbscript
something like .....
if field1 = value1
and field2 = value2
then
update......
else
insert.....

Ouch, dude. That's total gibberish. Also, you don't write
mark-up to do an
insert; you write a server script (or "code" to be super
generic). Markup
is what you used to make the form.
Anyway:
<%
If Inv_total = 0 Then
set Insert = Server.CreateObject("ADODB.Command")
Insert.ActiveConnection = MM_Inv_STRING
Insert.CommandText = "INSERT INTO Inv (Date, ID, Count)
VALUES ( date,
" & urlparameter & " , 1) "
Insert.CommandType = 1
Insert.CommandTimeout = 0
Insert.Prepared = true
Insert.Execute()
ElseIf Inv_total > 1 Then
set Update = Server.CreateObject("ADODB.Command")
Update.ActiveConnection = MM_Inv_STRING
Update.CommandText = "UPDATE Inv SET Count = Count +1 WHERE
Inv.ID = "
& urlparameter
Update.CommandType = 1
Update.CommandTimeout = 0
Update.Prepared = true
Update.Execute()
End If
%>

Similar Messages

  • JDBC RECEIVER -- conditional stored procedure

    Hi All,
               I have a situation where I need to call one stored procedure which returns to me if for a particular employee a record exists or not depending on that result I need to call further stored procedure. Can it be handled in one interface call. My scenario is :
    1. Source system  checks xxx database server to see if employee exists.
    Call GetEmplExsts stored procedure from xxx.
    2. Call the appropriate stored procedure from xxx depending on the status of the
    employee (if they already exist in xxx or not).
    Call CreatEmp stored proc if the employee does NOT already exist in xxx
    OR
    Call UpdatEmp stored proc if the employee already exists in xxx.
    Thx in advance
    Ravijeet

    hi
    i recommend you that create a sp with 3 condicions so you only do one call and is more easy to applicate in pi.
    for ex
    @@Exist char(2)
    exist = select existEmp from EMPLOYEE
    IF exist = "X" {
       execute updateEmp
    else{
    execute createEmp
    i dont remember too much how to define it in SQL :P
    Edited by: Rodrigo Pertierra on Feb 26, 2008 1:12 PM

  • Problem with Parameter on stored procedure for in() condition

    Hello,
    I would like to pass a parameter to a stored procedure like :
    procedure sps_country(zones varchar2)
    is
    begin
    select * from country where zone in (zones);
    end sps_country;
    My problem is : when i have one value in "zones" parameter all is OK but when i have 2, 3 ... values in zones with this format : ##, ##, ## there is no error, but there is no results found.
    have you a solution please ?
    Thanks

    Also this document has some suggestions.

  • Call stored procedure with a few unusal conditions

    What is the best way for a stored procedure that has been passed an unknown number of parameters to call another stored procedure, (whose name is passed in as a parameter to procedure1), that returns a ref cursor or a dbms_sql cursor.
    For example:
    Procedure1 - passed unknown # of parameters in an array/table calls stored procedure2 that returns a ref cursor that you eventually want to pass all the way back down to the client.
    I am using Oracle 11g.
    1). I have gotten this to work using derivedparameters in .net - but this is considered bad practice due to the overhead, etc...
    2). I have gotten this to work using execute immediate and bind variables.
    EXECUTE IMMEDIATE v_sqlstmt using p_strarry(1), p_strarry(2), p_strarry(3), p_strarry(4), p_strarry(5), p_rc;
    The problem with this technique is that you need to create a case statement that will use a different execute immediate statement for each arraycnt, since the using clause cannot be created dynamically. Also, considered bad. If I increase the number of elements in the array beyond what I have coded for then I need to add more when clauses to my case statement.
    3). I have gotten this to work with execute immediate that contains no binding variables - but my understanding is that this causes the shared pool to populate and causes the server to have to be flushed, periodically.
    4). I have read that with an unknown number of bind variables you need to use dbms_sql - I have been unable to find any reference as to a way I can return a ref cursor to the dbms_sql.cursor in a list of bind variables or a dbms_sql.cursor that returns multiple rows from a table to the original dbms_sql_cursor.
    In case what I have said is not specific enough - a code snipplet from the execute immediate using bind variables, (1 above).:
    PROCEDURE eiroutebvsproc(p_sprocname in varchar2,
    p_strarry in StrArry,
    p_rc IN OUT SYS_REFCURSOR)
    IS
    v_sqlstmt varchar2(300);
    v_arrycnt int;
    BEGIN
    v_sqlstmt := 'begin ';
    v_arrycnt := p_strarry.count;
    CASE
    WHEN (v_arrycnt = 1)
    THEN v_sqlstmt := '';
    WHEN (v_arrycnt = 2)
    THEN v_sqlstmt := '';
    WHEN (v_arrycnt = 3)
    THEN v_sqlstmt := '';
    WHEN (v_arrycnt = 4)
    THEN v_sqlstmt := '';
    WHEN (v_arrycnt = 5)
    THEN v_sqlstmt := v_sqlstmt || p_sprocname || '(:2, :3, :4, :5, :6, :7);';
    v_sqlstmt := v_sqlstmt || ' end;';
    EXECUTE IMMEDIATE v_sqlstmt using p_strarry(1), p_strarry(2), p_strarry(3), p_strarry(4), p_strarry(5), p_rc;
    ELSE v_sqlstmt :='ERROR';
    END CASE;
    this creates an anonymous block - that calls the 2nd procedure - The 2nd procedure name is passed into procedure1 as p_sprocname.
    p_strarry contains an unknown # of parameters to be passed to the 2nd procedure. The 2nd procedure returns a ref cursor that is also an out parameter for procedure1 allowing for the ref cursor to be passed all the way back to the client.
    Thank You,
    Keith

    user10651723 wrote:
    What is the best way for a stored procedure that has been passed an unknown number of parameters to call another stored procedure, (whose name is passed in as a parameter to procedure1), that returns a ref cursor or a dbms_sql cursor.There would be no good way to do that, like there is no real good way to bang your head against a brick wall.
    If the application can tell the first procedure than name of the procedure to call, why can it not just call the required procedure instead?

  • Execute Immediate with SQL in Stored Procedure

    Dear Experts, Please help / guide on below statement which i want to execute in stored procedure,
    EXECUTE IMMEDIATE 'INSERT INTO TABLE
    SELECT COUNT(AGENTID) FROM TABLE_1 A,
    TABLE_2 B, TABLE_3 C
    WHERE A.COL = B.COL AND B.COL = C.COL AND
    DT BETWEEN TRUNC(SYSDATE-1) + 1/24 + 0/12/60/60 AND TRUNC(SYSDATE-1) + 25/24 - 0/12/60/60';
    Edited by: DBA on Dec 3, 2011 3:57 PM

    So what's the problem? The only thing I can see is:
    INSERT INTO TABLETABLE is reserved word, so name your table differently.
    Also, I am assuming the above statement is actually generated from some variables based on some conditions, otherwise why would you need dynamic SQL, just issue:
    INSERT INTO TABLE
    SELECT COUNT(AGENTID) FROM TABLE_1 A,
    TABLE_2 B, TABLE_3 C
    WHERE A.COL = B.COL AND B.COL = C.COL AND
    DT BETWEEN TRUNC(SYSDATE-1) + 1/24 + 0/12/60/60 AND TRUNC(SYSDATE-1) + 25/24 - 0/12/60/60;

  • Call a pl/sql stored procedure from a report link

    Hello everyone:
    I have a report with a link in a column, which takes you to another page and passes values to that page, but also need the link, run a stored procedure before going to the other page.
    In the Forum I found cases similar to this, but none of the examples is clear to me how I do this. It seems that the target of the link should be URL, and also I have to call the stored procedure with a javascript function.
    My questions are:
    - Is this the best method to make what I need?
    - How should be the syntax of the link that takes me to another page and run the "stored procedure"
    - How and where I put the function "javascript" to run the "stored procedure"
    BDD: Oracle 11 g
    Apex version 4.1
    Best regards
    Gerard

    Hi,
    Change the link to call an apex.submit process. (javascript).
    Edit:
    I've set up a basic example.
    Region 1 (just so you can see the value getting set):
    select :P27_HIDDEN item_Value
    from dual
    Region 2:
    select rownum id, dbms_random.string('U', 12) sss
    from dual
    connect by level <= 20
    item attributes for ID:
    Target: URL
    URL: javascript:apex.submit({request:'NAVIGATE', set:{'P27_HIDDEN':#ID#}});
    (P27_HIDDEN is just a hidden item i created)
    http://apex.oracle.com/pls/apex/f?p=54920:27
    Edit2:
    In your case, then you can just have a page process based on conditions request = expression1, expression1: NAVIGATE..... then a page branch to go to the desired page, also with the same conditions
    Edit3:
    Im not sure what examples you are referring to , as you didn't supply any links; but you mention calling the stored procedure from javascript, so would likely involve some AJAX, but imo not necessary.

  • Help....on stored procedure (or package)

    Hi,
    I have any problem to create stored procedure in Oracle, Can you help me?
    I have to create a stored procedure (or package) in order to reserve the free rooms to the students in the period comprised between the DATE_START and DATE_END.
    Table of the present rooms in building BL1 (RM): SEX 1 = M - SEX 2 = F SEX = 0 (ROOM WITHOUT SEX)
    BL_ID.....FL_ID.......RM_ID.........SEX......RM_STD.....RM_CAT
    BL1.........1..........101..............1........S........ROOM
    BL1.........1..........102..............0........D........ROOM
    BL1.........1..........103..............2........T........ROOM
    BL1.........2..........201..............2........S........ROOM
    BL1.........2..........202..............1........D........ROOM
    BL1.........2..........203..............1........T........ROOM
    BL1.........3..........301..............2........S........APARTMENT
    BL1.........3..........302..............2........D........APARTMENT
    BL1.........3..........303..............1........T........APARTMENT
    BL1.........3..........304..............1........D........APARTMENT
    BL1.........3..........305..............0........D........APARTMENT
    Table of the students (EM):
    EM_ID...........BL_ID.......FL_ID........RM_ID........COD_STUD
    SABRINA..........BL1..........1............102.........524505
    TAKEM............BL1..........1............103.........569673
    SERAFINO.........BL1..........1............103.........589920
    STELLA...........BL1..........1............102.........574659
    CHIARA...........BL1..........1............101.........587845
    VIDAL............BL1..........1............102.........602877
    ROSARIA..........BL1..........2............202.........517070
    LUCA.............BL1..........2............201.........602743
    DANIELA..........BL1..........2............203.........602865
    ANNAMARIA........BL1..........3............305.........588721
    LUIGI............BL1..........3............304.........546517
    Type of rooms (RM_STD):
    RM_STD.......STD_EM........DESCRIPTION
    D.............4..............DOUBLE
    T.............6..............TRIPLE
    S.............2..............SINGLE
    Tables of the reservations carried out from the students (RMPCT):
    EM_ID......BL_ID........FL_ID......RM_ID......DATE_START.......DATE_END.......COD_STUD
    CHIARA......BL1.........1..........101.......11/02/2004.......12/02/2004.......587845
    CHIARA......BL1.........1..........101.......03/02/2005.......16/02/2005.......587845
    SERAFINO....BL1.........1..........102.......12/02/2004.......19/02/2004.......589920
    VIDAL.......BL1.........1..........102.......16/02/2004.......01/03/2004.......602877
    SERAFINO....BL1.........1..........103.......01/02/2004.......15/02/2004.......589920
    TAKEM.......BL1.........1..........103.......04/02/2005.......10/02/2005.......569673
    LUCA........BL1.........2..........201.......03/02/2005.......23/02/2005.......602743
    ROSARIA.....BL1.........2..........202.......03/02/2005.......16/02/2005.......517070
    DANIELA.....BL1.........2..........203.......03/02/2005.......04/02/2005.......602865
    LUIGI.......BL1.........3..........301.......03/02/2005.......23/02/2005.......546517
    VALERIA.....BL1.........3..........302.......12/02/2004.......16/02/2004.......515348
    CHIARA......BL1.........3..........302.......05/02/2004.......15/02/2004.......587845
    CHIARA......BL1.........3..........304.......10/02/2004.......12/02/2004.......587845
    CHIARA......BL1.........3..........305.......20/01/2004.......04/02/2004.......587845
    ANNAMARIA...BL1.........3..........305.......03/02/2005.......16/02/2005.......588721
    INPUT PARAMETERS:
    CREATE OR REPLACE Procedure RESERVE_ROOMS (stud_name varchar2,
    cod_stud varchar2,
    bl_in varchar2,
    fl_in varchar2,
    rm_in in varchar2,
    sex_in varchar2,
    date_start_in varchar2,
    date_end_in varchar2)
    CONDITIONS:
    verify if there are students in table EM:
    select count (1)
    into v_appo
    from em
    where em_id = stud_name
    and cod_stud = cod_stud;
    if v_appo = 0 then
    insert new student:
              insert into em (em_id,cod_sud,sex)
    values (stud_name,cod_stud,sex_in);
    Now I must verify the free rooms in the period comprised between the DATE_START_IN and DATE_END_IN.
    I tried this query: (seem correct...have you any idea?)
    select bl_id,fl_id,rm_id,RM_STD
    from rm
    where (bl_id,fl_id,rm_id,RM_STD) not in (select a.bl_id,a.fl_id,a.rm_id,A.RM_STD
                             from rm a, rmpct b
                             where a.bl_id=b.bl_id
                             and a.fl_id=b.fl_id
                             and a.rm_id=b.rm_id
                             AND((b.date_start <= TO_DATE(date_start_in, 'dd-mm-YYYY')
                             AND b.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY'))
                             OR ( b.date_end <= TO_DATE(date_end_in, 'dd-mm-YYYY'))
                             AND b.date_end >= TO_DATE(date_start_in, 'dd-mm-YYYY')
                             OR ( b.date_start >= TO_DATE(date_start_in, 'dd-mm-YYYY')
                             and b.date_start <= TO_DATE(date_end_in, 'dd-mm-YYYY')
                             AND b.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY'))))
    with this query I get all free rooms in period date_start_in - date_end_in, but I must,also,verify if there are double or triple rooms (reserved) with minus of 2 (double) or minus of 3 (triple) students. If there are I can reserved these rooms.
    I tried to verify with these steps:
    CREATE OR REPLACE VIEW COUNT_EM ( BL_ID,
    FL_ID, RM_ID, NUMBER_EM ) AS
    (SELECT rm.bl_id, rm.fl_id, rm.rm_id, COUNT(*) as numero_em
    FROM em, rm
    WHERE em.bl_id(+) = rm.bl_id
    AND em.fl_id(+) = rm.fl_id
    AND em.rm_id(+) = rm.rm_id
    and rm.rm_std in ('S', 'D', 'T')
    group by rm.bl_id, rm.fl_id, rm.rm_id)
    CREATE OR REPLACE VIEW COUNT_RMPCT ( BL_ID,
    FL_ID, RM_ID, STD_EM, NUMBER_RMPCT
    ) AS
    SELECT rm.bl_id, rm.fl_id, rm.rm_id, rmstd.std_em, COUNT(*) as numero_rmpct
    FROM rm, rmpct,rmstd
    WHERE rmpct.bl_id(+) = rm.bl_id
    AND rmpct.fl_id(+) = rm.fl_id
    AND rmpct.rm_id(+) = rm.rm_id
    and rm.rm_std=rmstd.rm_std
    and rm.rm_std in ('S', 'D', 'T')
    AND((rmpct.date_start <= TO_DATE(date_start_in', 'dd-mm-YYYY')
    AND rmpct.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY'))
    OR ( rmpct.date_end <= TO_DATE(date_end_in, 'dd-mm-YYYY'))
    AND rmpct.date_end >= TO_DATE(date_start_in, 'dd-mm-YYYY')
    OR ( rmpct.date_start >= TO_DATE(date_start_in, 'dd-mm-YYYY')
    and rmpct.date_start <= TO_DATE(date_end_in, 'dd-mm-YYYY')
    AND rmpct.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY')))
    group by rm.bl_id, rm.fl_id, rm.rm_id, rmstd.std_em
    AND FINALLY:
    select a.bl_id, a.fl_id, a.rm_id, a.NUMBER_RMPCT, B.NUMBER_EM, a.std_em, (a.std_em - (a.NUMBER_RMPCT+B.NUMBER_EM)) RM_FREE
    from COUNT_RMPCT a, COUNT_EM b
    where a.bl_id=b.bl_id
    and a.fl_id=b.fl_id
    and a.rm_id=b.rm_id
    if RM_FREE > 0 THEN there are free rooms (D or T) between those occupied in that period.
    Now If the room (bl_in,fl_in,rm_in) is free I can reserve inserting it in the table RMPCT:
    INSERT INTO rmpct (bl_id, fl_id, rm_id,em_id,cod_stud,date_start, date_end)
    values(bl_in,fl_in,rm_in,stud_name,cod_stud,date_start_in,date_end_in);
    If I haven't rm_in (can be null) I must reserve the first free room (random).
    after these controls: I update table of the students:
    UPDATE em
    Set bl_id = BL_IN,fl_id= FL_IN,rm_id=rm_in
         where em_id=stud_name
         and cod_stud=cod_stud;
    Finally I must make a control on the sex of the room, because there are rooms that have sex=0
    if RM.SEX <> 0 then
         null
    else
    if rm_cat = 'ROOM' then
    UPDATE rm
    set sex = sex_in
    where bl_id = in
    and fl_id = in
    and rm_id = in;
    if rm_cat = 'APARTMENT' then (update on all rooms)
    UPDATE rm
    set sex = sex_in
    where bl_id = in
    and fl_id = in;
    IF v_appo > 0 then
         Same controls except:      insert into em (em_id,cod_sud,sex)
                   values (stud_name,cod_stud,sex_in);
    How I can insert in one stored procedure (or package) all these instructions and controls?
    Thanks in advance!

    In the following demonstation, I have changed the names of some of your variables, in order to standardize them. I have prefaced input parameters with p_ and local variables with v_ and followed them with the name of the column that they are associated with when appropriate. This avoids conflicts with any column names and makes the code easier to read and follow. I have also used table_name.column_name%type to specify the data type associated with the variables instead of using varchar2. That way, if the data type of the columns changes, the code does not have to be updated. This is standard practice.
    In your first insert statement, you have attempted to insert sex into the em table, but there is no sex column in the em table data that you displayed, so I removed the sex.
    Instead of using a complicated mess of views and such to check whether there is a room available and then assign it, I have used just one select statement to select an available room and used an exception clause that handles a no_data_found exception to deal with when there is no room available.
    I have greatly simplified your checking of dates within that select statement.
    You were on the right track with the outer joins and grouping and checking for single, double, or triple rooms. Instead of count, I have used sum and nvl2, so that only the occupied rooms are counted and the rows generated by the outer join are zeroes.
    I have used the nvl function to allow for null values in the input parameters, so that any room can be selected. I used dbms_random.random to ensure that the rows are ordered randomly, but you can leave that out if it is really not that critical that the result be truly random and any row will do. I have used rownum=1 in an outer query to select just one row.
    When you select from a table in pl/sql, you have to select into something. You cannot just issue a select statement, then use an if statement to compare some value in the select statement. For example, you cannot use "if rm.sex ..." you have to select rm.sex into a variable, like v_sex, then use "if v_sex ...". So, I have selected the result of the select statement into local variables, then used those variables for comparisons. I have also used those variables for inserting and updating the appropriate tables, rather than just using the original input parameters, since some of them may have been null.
    In the example below, I have demonstrated how the procedure rejects an unavailable room, accepts a specific available room, and randomly assigns a room.
    -- procedure:
    scott@ORA92> CREATE OR REPLACE PROCEDURE reserve_rooms
      2    -- input parameters:
      3    (p_em_id      IN em.em_id%TYPE,
      4       p_cod_stud   IN em.cod_stud%TYPE,
      5       p_bl_id      IN rm.bl_id%TYPE,
      6       p_fl_id      IN rm.fl_id%TYPE,
      7       p_rm_id      IN rm.rm_id%TYPE,
      8       p_sex          IN rm.sex%TYPE,
      9       p_date_start IN VARCHAR2,
    10       p_date_end   IN VARCHAR2)
    11  AS
    12    -- local variables:
    13    v_appo          INTEGER;
    14    v_bl_id          rm.bl_id%TYPE;
    15    v_fl_id          rm.fl_id%TYPE;
    16    v_rm_id          rm.rm_id%TYPE;
    17    v_rm_cat      rm.rm_cat%TYPE;
    18    v_sex          rm.sex%TYPE;
    19  BEGIN
    20    -- verify if the student is in table em:
    21    SELECT COUNT (*)
    22    INTO   v_appo
    23    FROM   em
    24    WHERE  em_id = p_em_id
    25    AND    cod_stud = p_cod_stud;
    26    -- if the student is not in table em, then insert new student:
    27    IF v_appo = 0 THEN
    28        INSERT INTO em (em_id, cod_stud)
    29        VALUES (p_em_id, p_cod_stud);
    30    END IF;
    31    BEGIN
    32        -- find available room:
    33        SELECT bl_id, fl_id, rm_id, sex, rm_cat
    34        INTO     v_bl_id, v_fl_id, v_rm_id, v_sex, v_rm_cat
    35        FROM     (SELECT rm.bl_id, rm.fl_id, rm.rm_id, rm.sex, rm.rm_cat
    36             FROM     rmpct, rm
    37             WHERE     rm.bl_id = rmpct.bl_id (+)
    38             AND     rm.fl_id = rmpct.fl_id (+)
    39             AND     rm.rm_id = rmpct.rm_id (+)
    40             AND     rm.bl_id = NVL (p_bl_id, rm.bl_id)
    41             AND     rm.fl_id = NVL (p_fl_id, rm.fl_id)
    42             AND     rm.rm_id = NVL (p_rm_id, rm.rm_id)
    43             AND     (rm.sex   = p_sex OR rm.sex = 0)
    44             AND     rmpct.date_start (+) <= TO_DATE (p_date_end, 'DD-MM-YYYY')
    45             AND     rmpct.date_end (+) >= TO_DATE (p_date_start, 'DD-MM-YYYY')
    46             GROUP     BY rm.bl_id, rm.fl_id, rm.rm_id, rm.sex, rm.rm_cat, rm.rm_std
    47             HAVING SUM (NVL2 (rmpct.rm_id (+), 1, 0))
    48                 < DECODE (rm_std, 'S', 1, 'D', 2, 'T', 3)
    49             ORDER     BY DBMS_RANDOM.RANDOM)
    50        WHERE ROWNUM = 1;
    51        -- reserve room:
    52        INSERT INTO rmpct (bl_id, fl_id, rm_id, em_id, cod_stud, date_start, date_end)
    53        VALUES (v_bl_id, v_fl_id, v_rm_id, p_em_id, p_cod_stud,
    54             TO_DATE (p_date_start, 'DD-MM-YYYY'),
    55             TO_DATE (p_date_end, 'DD-MM-YYYY'));
    56        -- update students:
    57        UPDATE em
    58        SET     bl_id = v_bl_id, fl_id = v_fl_id, rm_id = v_rm_id
    59        WHERE     em_id = p_em_id
    60        AND     cod_stud = p_cod_stud;
    61        -- update sex of room or apartment floor:
    62        IF v_sex = 0 THEN
    63          IF v_rm_cat = 'ROOM' THEN
    64            UPDATE rm
    65            SET    sex = p_sex
    66            WHERE  bl_id = v_bl_id
    67            AND    fl_id = v_fl_id
    68            AND    rm_id = v_rm_id;
    69          ELSIF v_rm_cat = 'APARTMENT' THEN
    70            UPDATE rm
    71            SET    sex = p_sex
    72            WHERE  bl_id = v_bl_id
    73            AND    fl_id = v_fl_id;
    74          END IF;
    75        END IF;
    76    EXCEPTION
    77        -- if no room is available:
    78        WHEN NO_DATA_FOUND THEN
    79          RAISE_APPLICATION_ERROR (-20001, 'Sorry, there is no such room available.');
    80    END;
    81  END reserve_rooms;
    82  /
    Procedure created.
    scott@ORA92> SHOW ERRORS
    No errors.
    -- rejects unavailable room of wrong sex:
    scott@ORA92> EXECUTE reserve_rooms ('BARBARA', 654321, 'BL1', 1, 101, 2, '04-02-2005', '10-02-2005')
    BEGIN reserve_rooms ('BARBARA', 654321, 'BL1', 1, 101, 2, '04-02-2005', '10-02-2005'); END;
    ERROR at line 1:
    ORA-20001: Sorry, there is no such room available.
    ORA-06512: at "SCOTT.RESERVE_ROOMS", line 79
    ORA-06512: at line 1
    -- accepts available room of same sex:
    scott@ORA92> EXECUTE reserve_rooms ('BARBARA', 654321, 'BL1', 1, 103, 2, '04-02-2005', '10-02-2005')
    PL/SQL procedure successfully completed.
    -- assigns random available room of same or no sex:
    scott@ORA92> EXECUTE reserve_rooms ('BARBARA', 654321, NULL, NULL, NULL, 2, '11-02-2005', '23-02-2005')
    PL/SQL procedure successfully completed.
    -- results:
    scott@ORA92> -- one and only one row added to em table:
    scott@ORA92> SELECT * FROM em WHERE em_id = 'BARBARA'
      2  /
    EM_ID           BL_I      FL_ID      RM_ID   COD_STUD
    BARBARA         BL1           1        102     654321
    scott@ORA92> -- rooms reserved and other people who have reserved same room
    scott@ORA92> -- (there are 3 who reserved a double room, but only 2 at a time):
    scott@ORA92> SELECT *
      2  FROM   rmpct
      3  WHERE  (bl_id, fl_id, rm_id) IN
      4           (SELECT bl_id, fl_id, rm_id
      5            FROM   rmpct
      6            where  em_id = 'BARBARA')
      7  /
    EM_ID           BL_      FL_ID      RM_ID DATE_STAR DATE_END    COD_STUD
    BARBARA         BL1          1        102 11-FEB-05 23-FEB-05     654321
    SERAFINO        BL1          1        102 12-FEB-04 19-FEB-04     589920
    VIDAL           BL1          1        102 16-FEB-04 01-MAR-04     602877
    SERAFINO        BL1          1        103 01-FEB-04 15-FEB-04     589920
    TAKEM           BL1          1        103 04-FEB-05 10-FEB-05     569673
    BARBARA         BL1          1        103 04-FEB-05 10-FEB-05     654321
    6 rows selected.
    scott@ORA92> -- rooms reserved are all correct sex
    scott@ORA92> -- (note that room 102 was updated from 0 to 2):
    scott@ORA92> SELECT *
      2  FROM   rm
      3  WHERE  (bl_id, fl_id, rm_id) IN
      4           (SELECT bl_id, fl_id, rm_id
      5            FROM   rmpct
      6            where  em_id = 'BARBARA')
      7  /
    BL_      FL_ID      RM_ID        SEX R RM_CAT
    BL1          1        102          2 D ROOM
    BL1          1        103          2 T ROOM
    scott@ORA92>

  • Need to insert data in 2 tables thro stored procedure

    I need to create a stored procedure which will insert data in two tables. The procedure will get
    its inputs from an Oracle Developer Form which will be inserted into the tables.
    The 2 tables structure:
    1.FEES_MASTER
    Name Null? Type
    FEES_ID NOT NULL NUMBER(8) -- Primary Key
    CS_ID NOT NULL NUMBER(8) -- Class Student ID; An enrolled student
    REC_DATE NOT NULL DATE -- Fees receipt date
    REC_AMOUNT NOT NULL NUMBER(6) -- Fees receipt amount
    2.FEES_DETAIL
    Name Null? Type
    FEES_ID NOT NULL NUMBER(8) -- Foreign Key
    MONTH NOT NULL DATE -- First of each month to identify fee month
    Scenario:
    A student submits fees for 3 months through Master/Detail related blocks in a Developer Form as
    Under:
    Fees Master
    Fees ID : 11002
    Class Student ID : 356
    Receipt Date : 06-JAN-2001
    Receipt Amount : 1500
    Fees Detail
    Fees ID Fees Month
    11002 01-JAN-2001
    11002 01-FEB-2001
    11002 01-MAR-2001
    I need to check each fees detail record for fees month duplication as well before inserting new records.
    How can this be achieved?
    Thanks in advance.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Fan Liu ([email protected]):
    create primary key in the detail table. i think it's FEES_ID + MONTH. then mark the columns in Forms as primary key property true, then call check_record_uniqueness built-in in on-check-unique trigger.<HR></BLOCKQUOTE>
    Thanks very much. But what I need is to ensure transaction integrity thro stored procedure. The system allows a certain CS_ID (i.e. Class Student ID, which is assigned a new one to every student annually) to submit fees for 12 months only, because a student stays in a class for a year. The 2 columns in the FEES_DETAIL table are composite primary key which only ensures that a certain FEES_ID will not be repeated for the same month. But suppose:
    1)this data already exist in the tables:
    Fees Master
    Fees ID : 11002
    Class Student ID : 356
    Receipt Date : 06-JAN-2001
    Receipt Amount : 1500
    Fees Detail
    Fees ID Fees Month
    11002 01-JAN-2001
    11002 01-FEB-2001
    11002 01-MAR-2001
    2)And this data is currently being inserted:
    Fees Master
    Fees ID : 11300
    Class Student ID : 356
    Receipt Date : 04-FEB-2001
    Receipt Amount : 1500
    Fees Detail
    Fees ID Fees Month
    11300 01-JAN-2001
    11300 01-FEB-2001
    11300 01-MAR-2001
    The data in the 2nd condition is perfectly valid but the application can't allow a student to submit fees for a month which he has already submitted. Now only a stored procedure can make sure after checking that the same student doesn't pay fees for duplicate months. Another reason for my emphasis on stored procedure is that what if a user tries to insert data thro an SQL* Plus session instead of the Form.
    PROBLEM: Now the problem I am having is I don't know how the procedure will take input for multiple records from the FEES_DETAIL block in the Form.
    Please assist in this regard. Thanks.

  • Help Needed in Xml Stored Procedure

    Hi , i am trying to write one sp which takes xml document as a parameter. I want to update/Insert the data in the xml based on some conditions. So i put the data from xml to a global TEMPORARY table. The i process this data and will update /Insert the data based on the output.
    Right now i am unable to create global TEMPORARY table my stored procedure .When i compile the stored procedure , its showing the following error
    Compilation errors for PROCEDURE SYSTEM.TESTXML
    Error: PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
    begin case declare exit for goto if loop mod null pragma
    raise return select update while with <an identifier>
    <<
    close current delete fetch lock insert open rollback
    savepoint set sql execute commit forall merge pipe
    Line: 8
    Text: CREATE GLOBAL TEMPORARY TABLE temp
    I am attaching my sp below
    create or replace procedure testxml
    ( xmlDoc IN clob )
    is
    updCtx DBMS_XMLStore.ctxType;
    rows NUMBER;
    begin
    CREATE GLOBAL TEMPORARY TABLE temp
    ( cdcalendar NUMBER,
    cdperiod NUMBER,
    cdsubperiod NUMBER,
    cdglperiod NUMBER,
    dtstartsubperiod date,
    dtendsubperiod date
    ) ON COMMIT PRESERVE ROWS;
    updCtx := DBMS_XMLStore.newContext(temp)
    rows := DBMS_XMLStore.insertXML(updCtx,xmlDoc);
    DBMS_XMLStore.closeContext(updCtx);
    if Not exists
    (Select Distinct cd_calendar from Calendar_Period
    Where cd_calendar = select Distinct cdcalendar from temp )
    Then
    Insert into calendar_period
    cd_calendar,
    cd_period,
    cd_subperiod,
    cd_gl_period,
    dt_start_subperiod,
    dt_end_subperiod
    select cdcalendar,
    cdperiod,
    cdsubperiod,
    cdglperiod,
    dtstartsubperiod,
    dtendsubperiod
    from temp ;
    Else
    Update calendar_period
    Set cdp.cd_calendar = temp.cdcalendar ,
    cdp.cd_period = temp.cdperiod,
    cdp.cd_subperiod = temp.cdsubperiod ,
    cdp.cd_gl_period = temp.cdglperiod,
    cdp.dt_start_subperiod = temp.dtstartsubperiod ,
    cdp.dt_end_subperiod = temp.dtendsubperiod
    From
    calendar_period cdp Inner Join temp
    On
    cdp.cd_calendar = temp.cdcalendar
    cdp.cd_subperiod = temp.cdsubperiod
    cdp.cd_period = temp.cdperiod ;
    End if
    end testxml;
    Kindly guide me !!!!

    Hi,
    "CREATE GLOBAL TEMPORARY TABLE" is not a PL/SQL sommand; it is a SQL command. That explain the error message you're getting.
    Normally, tables (including Global Temporary Tables) are created once for all, without using PL/SQL. After they are created, you can write PL/SQL code to populate and use them. This job doies not seem to be an exception.
    In the rare event that you do need to create a table in PL/SQL, use EXECUTE IMMEDIATE, which can do any SQL command from withiin PL/SQL.
    EXECUTE IMMEDIATE is documented in the PL/SQL manual:
    http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/dynamic.htm#sthref857

  • Calling Stored Procedure from Oracle DataBase using Sender JDBC (JDBC-JMS)

    Hi All,
    We have requirement to move the data from Database to Queue (Interface Flow: JDBC -> JMS).
    Database is Oracle.
    *Based on Event, data will be triggered into two tables: XX & YY. This event occurs twice daily.
    Take one field: 'aa' in XX and compare it with the field: 'pp' in YY.
    If both are equal, then
         if the field: 'qq' in YY table equals to "Add" then take the data from the view table: 'Add_View'.
         else  if the field: 'qq' in YY table equals to "Modify"  then take the data from the view table: 'Modify_View'.
    Finally, We need to archive the selected data from the respective view table.*
    From each table, data will come differently, means with different field names.
    I thought of call Stored Procedure from Sender JDBC Adapter for the above requirement.
    But I heard that, we cannot call stored procedure in Oracle through Sender JDBC as it returns Cursor instead of ResultSet.
    Is there any way other than Stored Procedure?
    How to handle Data Types as data is coming from two different tables?
    Can we create one data type for two tables?
    Is BPM required for this to collect data from two different tables?
    Can somebody guide me on how to handle this?
    Waiting eagerly for help which will be rewarded.
    Thanks and Regards,
    Jyothirmayi.

    Hi Gopal,
    Thank you for your reply.
    >Is there any way other than Stored Procedure?
    Can you try configuring sender adapter to poll the data in intervals. You can configure Automatic TIme planning (ATP) in the sender jdbc channel.
    I need to select the data from different tables based on some conditions. Let me simplify that.
    Suppose Table1 contains 'n' no of rows. For each row, I need to test two conditions where only one condition will be satisfied. If 1st condition is satisfied, then data needs to be taken from Table2 else data needs to be taken from Table3.
    How can we meet this by configuring sender adapter with ATP?
    ================================================================================================
    >How to handle Data Types as data is coming from two different tables?
    If you use join query in the select statement field of the channel then whatever you need select fields will be returned. This might be fields of two tables. your datatype fields are combination of two diff table.
    we need to take data only from one table at a time. It is not join of two tables.
    ================================================================================================
    Thanks,
    Jyothirmayi.

  • Calling Stored Procedure which returns single or multiple value

    Hi All Experts,
    I have written one stored procedure which is returning different
    integer values according to condition.I am calling this
    stored procedure through prepareCall() method then executeUpdate() method ,but it is always returning 1,not my desired value.
    Pl also tell me,if my stored procedure returns more than one value
    then how will i get those values in my java program?
    Pl help by writing some sample code.
    Thanx in Advance.
    Pradipto

    1) Create CallableStatement
    2) For each of returning value you have to call RegisterOutParameter()
    3) For others user setXXX() method depending on type of parameter
    4) call
    5) use vars registered through RegisterOutParameter() - all your values should be there
    Pavel

  • Passing multi-value parameter in stored procedure ssrs

    I have  customer parameter which is a drop down list in my report and I have set it to "allow multiple values". This is an SSRS report.
    How do I pass multiple values to my stored procedure?
    RJ

    Hi ,
    Create a Table valued function in SQL Functions  as below 
    Step 1
    CREATE FUNCTION [dbo].[FnSplit]
    @List nvarchar(2000),
    @SplitOn nvarchar(5)
    RETURNS @RtnValue table 
    Id int identity(1,1),
    Value nvarchar(100)
    AS  
    BEGIN
    While (Charindex(@SplitOn,@List)>0)
    Begin 
    Insert Into @RtnValue (value)
    Select
    Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) 
    Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
    End 
    Insert Into @RtnValue (Value)
    Select Value = ltrim(rtrim(@List))
    Return
    END
    Step 2 in your store procedure change the parameter where condition something like below
    ALTER PROCEDURE [dbo].[SomeSP] 
    -- Add the parameters for the stored procedure here
    @CostCentre NVARCHAR(255)
    SELECT
    [ProjectCode],[ProjectName],[ProjectManager],SUM([Hours]) AS [Hours MTD]FROM dbo.Rpt_NRMA_CATS NC
    INNER JOIN PeriodID P ON NC.PeriodID=P.PeriodID
    WHERE 
    ([CostCentre]) collate database_default IN(SELECT Value FROM dbo.FnSplit(@CostCentre,','))
    END
    I hope this will help you.
    Dasari

  • Migrating from TSQL Need Help? printing multiple columns in Stored procedur

    I need to create a Stored Procedure which returns set of columns not all
    (for eg FirstName, LastName, Dept_Id) from emloyee table based on certain conditions.
    In TSQL, it is very simple to create such stored procedures which return full recordset(or subset).
    I tried creating explicit cursors but I can print only one record at a time. I dnt want concatenation of columns. I want seperate columns in ouput. How to achieve this.?
    Code : (But i can only concatenate and print results, what i want is diffrent columns)
    declare
    f_name employees.first_name%type;
    l_name employees.last_name%type;
    cursor c is
    select first_name,last_name
    from employees;
    begin
    open c;
    loop
    fetch c into f_name,l_name;
    exit when c%notfound;
    DBMS_OUTPUT.PUT_LINE (f_name||' '||l_name);
    end loop;
    close c;
    end;

    Another very informative article on ref cursors:
    PL/SQL 101 : Understanding Ref Cursors

  • Stored procedure for exec. multiple insert queries

    Hello everybody,
    I am running on
    select banner from v$version;
    Output:
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    I am a new to oracle and I need some help.
    I have to populate a table on monthly basis, for which I am required to run 20 queries.
    All queries retrieve data from the same table in another schema.
    I need to schedule a SP in order to be able to run all these queries.
    Till now, I have built separate SPs for the queries Proc_1, Proc_2, Proc_3, ..., Proc_20 and a final big SP (*main_sp*) which calls these SPs.
    Being that I have to change the date value for every month (I am doing it manually every single month), for every single query,
    I was wondering if there is a way to include all these queries in a single stored procedure.
    I am reading here execute parallel queries
    but I do not want to create 20 jobs.
    I found this topic, which seems to be interesting too, but I have never done this before:
    Executing multiple SQL scripts in a batch
    Moreover, how can I manage to insert the value of date only once (e.g in the main_sp) and every queries gets this value automatically.
    P.S.
    All queries retrieve data from the same table in another schema and fill in with data the same table in actual schema.
    insert into destin_tbl
    select from source_tbl@dbb_link
    Here is how my actual work looks like:
    First, I have built 20 sp, for each and every query:
    CREATE OR REPLACE PROCEDURE "SP_1" (
    START_DATE out DATE,
    IS
    BEGIN
    START_DATE := SYSDATE;
    insert /*+ append nologging parallel(x,4)*/into destin_tbl x
    select /*+ parallel(a,4)*/ field_1, field_2, field_3
    from source_tbl@dbb_link a
    where a.date_key >= 20101201 and a.date_key < 20100101 -- full month (december 2010) here
    and other conditions
    commit;
    END SP_1;
    = = = = = =
    CREATE OR REPLACE PROCEDURE "SP_2" (
    START_DATE out DATE,
    IS
    BEGIN
    START_DATE := SYSDATE;
    insert /*+ append nologging parallel(x,4)*/into destin_tbl x
    select /*+ parallel(a,4)*/ field_1, field_2, field_3
    from source_tbl@dbb_link a
    where a.date_key >= 20101201 and a.date_key < 20100101 -- full month (december 2010) here
    and other conditions
    commit;
    END SP_2;
    CREATE OR REPLACE PROCEDURE "SP_20" (
    START_DATE out DATE,
    IS
    BEGIN
    START_DATE := SYSDATE;
    insert /*+ append nologging parallel(x,4)*/into destin_tbl x
    select /*+ parallel(a,4)*/ field_1, field_2, field_3
    from source_tbl@dbb_link a
    where a.date_key >= 20101201 and a.date_key < 20100101 -- full month (december 2010) here
    and other conditions
    commit;
    END SP_20;
    I change the values for the date_key field every month I need to insert data, before I run the main_sp:
    CREATE OR REPLACE PROCEDURE "main_sp" (
    START_DATE out DATE
    IS
    BEGIN
    sp_1(START_DATE);
    sp_2(START_DATE);
    sp_3(START_DATE);
    sp_20(START_DATE);
    END main_sp;
    /

    You would need to pass a parameter to the main procedure and to all of the sub procedures. I would do it somethign like this. The main procedure takes a single date parameter the way I have structured it here, any date in the month you are interested in will do
    PROCEDURE main_sp(p_date IN DATE)
       l_st_dt  VARCHAR2(8);
       l_end_dt VARCHAR2(8);
    BEGIN
       l_st_dt := TO_CHAR(TRUNC(p_date, 'Mon'), 'yyyymmdd');
       l_end_dt := TO_CHAR(LAST_DAY(p_date) + 1, 'yyyymmdd');
       sp_1 (l_st_dt, l_end_dt);
       sp_2 (l_st_dt, l_end_dt);
    END;Then your individual procedures would look something like:
    PROCEDURE SP_1 (p_st_dt  IN VARCHAR2,
                    p_end_dt IN VARCHAR2) IS
    BEGIN
       insert /*+ append nologging parallel(x,4)*/into destin_tbl x
       select /*+ parallel(a,4)*/ field_1, field_2, field_3
       from source_tbl@dbb_link a
       where a.date_key >= p_st_dt and a.date_key < p_end_dt -- full month (december 2010) here
         and other conditions
       commit;
    END;
    Regarding the queries...they do not change only in the conditions statement, but also in the retrieve.
    I have 2 constant fields, whose value changes based on conditions (e.g. query_1 retrieves data regarding diesel car >speed => constant_field_1 = 'Diesel|_Car_Speed' ... query_2 retrieves data related to motorbike unleaded speed >=> constant_field_1 = 'MotorB_UL_Speed'.
    These makes the grouping of these queries impossible.Without seeing actual queries, it is impossible to say, but my guess would be that the 20 queries could be reduced to a single query, or at least less than 20. Something like:
    insert into destin_tbl x
    select CASE WHEN condition that indicates car THEN 'Diesel|_Car_Speed'
                WHEN condition that indicates motor bike THEN 'MotorB_UL_Speed'
                WHEN other conditions THEN other field1 values END
           field_2, field_3
    from source_tbl@dbb_link a
    where a.date_key >= p_st_dt and
          a.date_key < p_end_dt
      and other conditionsThe conditions for the CASE statement could be pulled out of the where caluses for the individual queries.
    John
    Edited by: John Spencer on Jan 24, 2011 9:37 AM

  • Stored procedure for disabling all foreign key constraints ?

    hi all,
    I need a stored procedure for disabling / enable all constraints in that database schema.
    Create or replace procedure enable_disable_proc(status varchar2(3),schema_name varchar2(20))
    ---> where 'status' parameter condition should be ( YES for enable) (NO for disable)
    ---> 'schema_name' is a parameter where ,only this schema should be affected with this procedure..
    Thanks in Advance..

    Hi,
    Try this code
    /* Formatted on 2009/07/16 08:15 (Formatter Plus v4.8.8) */
    CREATE PROCEDURE enab_disab_proc (
       enforce          IN   VARCHAR2,
       current_schema   IN   VARCHAR2
    IS
       v_alter_table_sql   VARCHAR2 (2000);
    BEGIN
       FOR rec_con IN (SELECT table_name, constraint_name
                         FROM user_constraints
                        WHERE owner = current_schema)
       LOOP
          IF (enforce = 'NO')
          THEN
             v_alter_table_sql :=
                   ' alter table '
                || current_schema
                || '.'
                || rec_con.table_name
                || ' disable constraint '
                || rec_con.constraint_name;
          ELSE
             v_alter_table_sql :=
                   ' alter table '
                || current_schema
                || '.'
                || rec_con.table_name
                || ' enable constraint '
                || rec_con.constraint_name;
          END IF;
          EXECUTE IMMEDIATE v_alter_table_sql;
       END LOOP;
    END;Edited by: Salim Chelabi on 2009-07-16 05:15

Maybe you are looking for