How to Use the Procedures in a Sql Query

Hi Friends,
Can anyone help me out whether can we use the procedure in the sql query..
if yes help me out with an example
my requirement is
i have one sql query .. in which i need to use the procedure which returns multiple values... how can i overcome it,can anyone help me out for this..
for your reference i am pasting the sql query
SELECT paf.person_id
FROM per_all_assignments_f paf START WITH paf.person_id = p_person_id
AND paf.primary_flag = 'Y'
AND paf.assignment_type IN('E', 'C')
AND l_effective_date BETWEEN paf.effective_start_date
AND paf.effective_end_date
CONNECT BY PRIOR paf.supervisor_id = paf.person_id
AND paf.primary_flag = 'Y'
AND paf.assignment_type IN('E', 'C')
AND l_effective_date BETWEEN paf.effective_start_date
AND paf.effective_end_date
and paf.person_id not in (>>>I HAVE TO USE THE PROCEDURE HERE<<<<);
Thanks in advance

We never saw your procedure, but maybe you could wrap it in a function
SQL> create or replace procedure get_members(in_something IN number, out_members OUT sys_refcursor)
is
begin
  open out_members for
    'select level member_id from dual connect by level <= :num' using in_something;
end get_members;
Procedure created.
SQL> create or replace type numbers as table of number;
Type created.
SQL> create or replace function members(in_something IN number)
return numbers
as
  member_cur sys_refcursor;
  members numbers;
begin
  get_members(in_something, member_cur);
  fetch member_cur bulk collect into members;
  close member_cur;
  return members;
end;
Function created.
SQL> select * from  table(members(4));
COLUMN_VALUE
           1
           2
           3
           4
4 rows selected.Variant on same using piplined function
SQL> create or replace function members_piped(in_something IN number)
return numbers pipelined
as
  member_cur sys_refcursor;
  rec number;
begin
  get_members(in_something, member_cur);
  loop
     fetch member_cur into rec;
     exit when member_cur%notfound;
     pipe row(rec);
  end loop;
  close member_cur;
  return;
end;
Function created.
SQL> select * from  table(members_piped(4));
COLUMN_VALUE
           1
           2
           3
           4
4 rows selected.
SQL> drop function members_piped;
Function dropped.
SQL> drop function members;
Function dropped.
SQL> drop type numbers;
Type dropped.
SQL> drop procedure get_members;
Procedure droppedEdit:
Sorry Blu, had not seen you already posted similar thing
Edited by: Peter on Jan 27, 2011 5:38 AM

Similar Messages

  • How to use the result of a sql query for a max () function

    Hi
    I wrote a query on which i wrote
    "select max(id) from users "
    how can i use the returned value.
    if i made the var name ="userid"
    can it be userid.rows[0] or what.
    thnx for any help

    Hi!
    The result of this query will be the max ID of users' IDs.
    Let say we have:
    String sql="select max(users.id) from users";
    Statement st = ctx.conn.createStatement();
    ResultSet rs = st.executeQuery(sql);
    rs.next();     
    So you can get the max Id in the following way:     
    int maxId=rs.getInt("id");
    Regards,
    Rossi

  • How to use the procedure column in reports

    Hi all
    How to call the procedure in reports as source.. If possible how to use columns of procedure in the layout column of report????

    Hi,
    Your query is not clear.
    1. In subject are you asking total column.
    A. Edit pivot view and go to Rows and click Total BY option here you can find option like (none,before,after) the you can select after it will display total all culms.
    2. I want use the columnC in columnD ? -- Am not understand.
    A. What my understand is you want to see the report only C and D values only.
    If it is correct we can apply filter in report level click column filed and apply not equal to A then it will show only C and D only.
    If it is wrong pleas post me correct one with example. Will try to help out this.
    I am not sure this is what your looking so far.
    Award points it is useful.
    Thanks,
    Satya

  • How to Suppress The Output of a SQL Query In Oracle 11gR2

    Hi Friends,
    I am using oracle version 11.2.0.1, I have set a cronjob which will run on every 15 minutes and give us a log file mentioning the execution time taken for that SQL query:-
    For example:
    SQL> set timing on;
    SQL> SELECT objProp FROM aradmin.arschema WHERE (schemaId = 175);
    OBJPROP+
    --------------------------------------------------------------------------------+
    *6\60006\4\0\\60008\40\0\60009\4\0\\60010\4\0\\60018\4\0\\600*
    *22\4\68\1\63\AR:jRL#*
    Elapsed: 00:00:00.00
    The above query will return the output as well as the time taken for execution of the query. I want to suppress the output of the query and only want the time taken to be printed. Is it possible by set commands. I have marked the output as bold and made it Italic.
    Please help me at the earliest.
    Regards,
    Arijit

    >
    I am using oracle version 11.2.0.1, I have set a cronjob which will run on every 15 minutes and give us a log file mentioning the execution time taken for that SQL query:-
    The above query will return the output as well as the time taken for execution of the query. I want to suppress the output of the query and only want the time taken to be printed. Is it possible by set commands. I have marked the output as bold and made it Italic.
    >
    How would that even be useful?
    A query from a tool such as sql*plus is STILL going to send the output to the client and the client. You can keep sql*plus from actually displaying the data by setting autotrace to trace only.
    But that TIME TAKEN is still going to include the network time it takes to send ALL rows that the query returns across the network.
    That time is NOT the same as the actual execution time of the query. So unless you are trying to determine how long it takes to send the data over the network your 'timing' method is rather flawed.
    Why don't you tell us WHAT PROBLEM you are trying to solve so we can help you solve it?

  • Use the results of an SQL query to create another query

    I am working on a bidding/allocation system using C# and MySQL, and I am currently having difficulty implementing the “post-allocation” mechanics of the system. Let me explain how it works, before introducing the code:
    When the bids are made on the system, they all have the position 0, so the first thing the system does is order them according to priority.
     (bid table)
    In this case, 5 bidders are interested in what is in plot 15, but we have not assigned them a position yet.
     (bid table)
    The system then organises the bids according to the order in which they are going to processed.
    The bids are then allocated, but unfortunately there are only 3 spaces in plot 15 that can be allocated. As a result, we use the priority listing to determine which bidder gets what.
     (booking table)
    At this stage, I can say that I am stuck, there are two things that I want to do, both of which involve reusing the results of the query at step 2.
     (bid table)
    The bidders that have their job allocated should see: their bid.status updated
    from Queued to Allocated,
    the bid.position should
    be set to 0,
    and their job_id should
    be set to the booking they have been allocated (i.e. booking.id).
    The bidders that have not had their jobs allocated should see: their bid.status remain
    as Queued, and their bid.position should
    be set to 0 in
    preparation for the method to run again
    The difficulty I am having is in stage 3, how do I determine which jobs have been allocated and which ones have not, and then run the necessary SQL queries in order to make the updates?
    My code so far is as follows:
    // STEP 1a - SELECT BIDS
    string query =
    "SELECT t1.operator_id, t1.datetime, t1.plot_id, t1.position, t2.market_access FROM bid t1 " +
    "JOIN operator t2 ON t1.operator_id = t2.id WHERE t1.status='Queued' AND t1.postcode=@postcode " +
    "ORDER BY t2.market_access ASC, t1.datetime ASC";
    var bidList = new List<BidList>();
    var cmd = new MySqlCommand(query, _connection);
    cmd.Parameters.AddWithValue(("@postcode"), _plot);
    MySqlDataReader dataReader = cmd.ExecuteReader();
    while (dataReader.Read())
    var item = new BidList
    OperatorId = dataReader["operator_id"] + "",
    PlotId = dataReader["plot_id"] + "",
    Position = dataReader["position"] + "",
    Datetime = dataReader["datetime"] + "",
    MarketAccess = dataReader["market_access"] + "",
    bidList.Add(item);
    dataReader.Close();
    // STEP 1b - SET PRIORITIES
    for (var i = 0; i < bidList.Count; i++)
    var position = i + 1;
    bidList[i].Position = position.ToString();
    query = "UPDATE bid SET position=@position WHERE status='Queued' AND postcode=@postcode AND operator_id=@operator_id;";
    cmd = new MySqlCommand(query, _connection);
    cmd.Parameters.AddWithValue(("@position"), position);
    cmd.Parameters.AddWithValue(("@postcode"), _plot);
    cmd.Parameters.AddWithValue(("@operator_id"), bidList[i].OperatorId);
    cmd.ExecuteNonQuery();
    dataReader.Close();
    // STEP 2 - ALLOCATE JOBS ACCORDING TO PRIORITY
    foreach (var t in bidList)
    query = "SELECT operator_id, plot_id, status FROM booking " +
    "WHERE status='open' AND postcode=@postcode AND operator_id='0'" +
    "ORDER BY datetime ASC;" +
    "UPDATE booking SET operator_id=@operator_id, status='Allocated' " +
    "WHERE (plot_id=@plot_id AND operator_id='0' AND status='Open') LIMIT 1;";
    cmd = new MySqlCommand(query, _connection);
    cmd.Parameters.AddWithValue(("@operator_id"), t.OperatorId);
    cmd.Parameters.AddWithValue(("@postcode"), _plot);
    cmd.Parameters.AddWithValue(("@plot_id"), t.PlotId);
    cmd.ExecuteNonQuery();
    dataReader.Close();
    // STEP 3
    CloseConnection();

    I can't tell.  When modifying row(s) you have to be able to uniquely be able to identify the row(s) in the database you want to modify.  That is why I recommended the other day to use DataAdapter instead of the DataReader.  You can with the
    reader, but you need to unique identify the rows.
    I can't tell from the limited amount of data you posted if in table 2 that there is only one row with the same operator id and plot id.  It looks like in table 2 the 'id' number is unique.  When working with a database with multiple tables you
    need to be able to link tables together with primary keys (columns wit unique values).  If the database isn't designed properly then it is impossible to do some operations.  So you always have to make sure you design a database properly.
    jdweng
    When you say table 2 are you referring to the bid or booking table?
    Here is the table structure for the bid table
    Here is the table structure for the booking table
    Hope it helps makes things clearer!

  • How to write the output of an SQL query to a text file?

    I am using Oracle 11g and SQL plus.
    I have a large table called rating.
    Whenever I do
    SQL> select * from rating;
    The output goes much beyond what the SQL Plus screen can show. I want to therefore store the output of this query into a text file.
    How can this be done? Please help. Thanks.

    SQL> SPOOL results.txt
    SQL> select * from rating;
    SQL> SPOOL OFF

  • How to measure the performance of a SQL query?

    Hello,
    I want to measure the performance of a group of SQL queries to compare them, but i don't know how to do it.
    Is there any application to do it?
    Thanks.

    You can use STATSPACK (in 10g its called as AWR - Automatic Workload Repository)
    Statspack -> A set of SQL, PL/SQL, and SQL*Plus scripts that allow the collection, automation, storage, and viewing of performance data. This feature has been replaced by the Automatic Workload Repository.
    Automatic Workload Repository - Collects, processes, and maintains performance statistics for problem detection and self-tuning purposes
    Oracle Database Performance Tuning Guide - Automatic Workload Repository
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14211/autostat.htm#PFGRF02601
    or
    you can use EXPLAIN PLAN
    EXPLAIN PLAN -> A SQL statement that enables examination of the execution plan chosen by the optimizer for DML statements. EXPLAIN PLAN causes the optimizer to choose an execution plan and then to put data describing the plan into a database table.
    Oracle Database Performance Tuning Guide - Using EXPLAIN PLAN
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14211/ex_plan.htm#PFGRF009
    Oracle Database SQL Reference - EXPLAIN PLAN
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9010.htm#sthref8881

  • How to use an array in a SQL Query

    Hi
    I need to use an array of numbers such as a VARRAY or Associated Index Array so that I can do the following SQL:
    select *
    from *
    where array is null or id is in array
    So that if the array is empty it will return all the records, and if the array is not empty then it will return only the rows associated with the ids in the array.
    Is this possible?
    Regards,
    Néstor Boscán

    Actually, solution I posted returns all rows when VARRAY is empty, not when it is null. To return all rows when VARRAY is null, use:
    SQL> select  ename
      2    from  emp
      3    where deptno in (select * from table(cast(sys.OdciNumberList(10,30) as sys.OdciNumberList)))
      4       or sys.OdciNumberList(10,30) is null
      5  /
    ENAME
    ALLEN
    WARD
    MARTIN
    BLAKE
    CLARK
    KING
    TURNER
    JAMES
    MILLER
    9 rows selected.
    SQL> select  ename
      2    from  emp
      3    where deptno in (select * from table(cast(null as sys.OdciNumberList)))
      4       or null is null
      5  /
    ENAME
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    ENAME
    JAMES
    FORD
    MILLER
    14 rows selected.
    SQL> SY.

  • How to get the desire output from sql query

    Consider an Order Table: ...
    Order Table:
    Order Id Item Qty
    O1 A1 5
    O2 A2 1
    O3 A3 3
    Please provide SQL which will explode the above data into single unit level records as shown below
    Desired Output:
    Order Id Order Id Qty
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O2 A2 1
    O3 A3 1
    O3 A3 1
    O3 A3 1

    How do I ask a question on the forums?
    SQL and PL/SQL FAQ

  • Use realational operators in an SQL query??????

    does any one knows how to use realational operators in an SQL query??????
    i wud like to do something like
    select decode(2<3,sysdate,sydate +1) from dual
    but i know decode does not supports relational operators......
    thanx and Regards
    amyt

    You can use a CASE statement which does support relational operators, or if you must use DECODE, then you can use something like:
    SELECT DECODE(SIGN(2 - 3),-1,sysdate,sysdate - 1)
    FROM dual;The SIGN function returns -1 if the expression is < 0, 1 if the expression is > 0 and 0 if the expression is 0. This works for numeric comparisions. You can use the GREATEST or LEAST functions in a similar fashion for character comparisions.
    TTFN
    John

  • Pass the result of a SQL Query as table_name  for another SQL Query

    Hi All,
    How to pass the result of a SQL Query as parameter to another SQL Query
    Eg: I am doing the steps below.
    1) select distinct table_name as TAB1 from all_tab_cols where table_name like 'T_%' and column_name like '%XYZ'
    2) I want to pass the table_name from step 1 above as parameter to another query "select * from TAB1"
    Thanks

    Naveen B wrote:
    Hi All,
    How to pass the result of a SQL Query as parameter to another SQL Query
    Eg: I am doing the steps below.
    1) select distinct table_name as TAB1 from all_tab_cols where table_name like 'T_%' and column_name like '%XYZ'
    2) I want to pass the table_name from step 1 above as parameter to another query "select * from TAB1"
    ThanksYou should craete PL/SQL code with cursor which will accept a parameter and call that cursor inside the first one
    But if the first sql returns only one row, you can do it with simple sql code
    select * from (select distinct table_name as TAB1 from all_tab_cols where table_name like 'T_%' and column_name like '%XYZ')- - - - - - - - - - - - - - - - - - - - -
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com
    [Step by Step install Oracle on Linux and Automate the installation using Shell Script |http://kamranagayev.wordpress.com/2009/05/01/step-by-step-installing-oracle-database-10g-release-2-on-linux-centos-and-automate-the-installation-using-linux-shell-script/]

  • How to build a report in web Intelligence using Store procedure under Microsoft SQL Server 2000

    Post Author: ltkin
    CA Forum: WebIntelligence Reporting
    Hi,
    How to build a report in web Intelligence using Store procedure under Microsoft SQL Server 2000 ?
    Regards,

    Hi ltkin,
    Unfortunately, it is not possible in Xir2 to create Webi reports from stored procedures.
    Webi reports can only be created from Universe. So in Business Objects XIR3 we can create a special universe that enables Web Intelligence user's to access stored procedures residing in the database. This is the only way that Web Intelligence user's can access stored procedures.
    Please let me know if the above information helps.
    Regards,
    Pavan

  • How to use the item of table(ms access) in pl/sql

    I want to use oracle forms builer developer application with ms access database
    I don't know how to use the item of table in the pl/sql;please help me !

    MERGE INTO EmpDept Target
    Using (SELECT E.Address,
    e.Design,
    e.EmailId,
    e.EmpId,
    e.Ename,
    e.ManagerId,
    e.Salary,
    D.DeptId,
    D.DeptName,
    D.Location
    FROM Employee E
    INNER JOIN Dept D
    ON E.DeptId = D.DeptId) AS Source (Address, Design, EmailId, EmpId, EName, ManagerId, Salary, DeptId, DeptName, Location)
    ON Source.EmpId = Target.EmpId
    WHEN NOT matched THEN
    INSERT (Address,
    Design,
    EmailId,
    EmpId,
    Ename,
    ManagerId,
    Salary,
    DeptId,
    DeptName,
    Location)
    VALUES (Address,
    Design,
    EmailId,
    EmpId,
    EName,
    ManagerId,
    Salary,
    DeptId,
    DeptName,
    Location)
    WHEN matched THEN
    UPDATE SET Address = Source.Address,
    Design = Source.Design,
    EmailId = Source.EmailId,
    Ename = Source.Ename,
    ManagerId = Source.ManagerId,
    Salary = Source.Salary,
    DeptId = Source.DeptId,
    DeptName = Source.DeptName,
    Location = Source.Location;

  • How to use stored procedures in DIAdem and Can the stored procedures be used to return values?

    Can anyone please tell me how to use stored procedures in diadem and to return values from it. Its really important, can you please answer it at the earliest.
    Thanks In advance
    spiya

    Hi Spria,
    I'm very sorry for the mix-up, I thought Allen was going to answer you back with the particulars that we found out. Check out the attached Word document and the below tidbits:
    The built-in DIAdem ODBC functions {SQL_...()} can only call stored functions, which return a scaler result {found then in SQL_Result(1,1)}. The syntax for this with an ORACLE db is
    "select function(parameters) from package"
    ...where package defaults to "dual" if you don't use your own package.
    There might be exceptions to that though, and the syntax will be different for other databases. Note that stored ORACLE procedures can NOT be called from the ODBC functions, instead you must use either ADO function calls in the DIA
    dem VBScript or the OO4O COM wrapper that ORACLE provides (this is described in further detail in the below Word document).
    Hope this helps,
    Brad Turpin
    DIAdem Product Support Engineer
    National Instruments
    Attachments:
    Calling_ORACLE_Stored_Procedures_from_DIAdem.doc ‏28 KB

  • How to use the default database service name on creating procedure for data

    how to use the default database service name on creating procedure for datagaurd client failover ??? all oracle doc says create a new service as below and enable at DB startup. but our client is using/wanted database default service to connect from application on the datagaurd environment (rac to non rac setup).please help.
    Db name is = prod.
    exec DBMS_SERVICE.CREATE_SERVICE (service_name => 'prod',network_name =>'prod',failover_method => 'BASIC',failover_type => 'SELECT',failover_retries => 180,failover_delay => 1);
    says already the service available.
    CREATE OR REPLACE TRIGGER manage_dgservice after startup on database DECLARE role
    VARCHAR(30);BEGIN SELECT DATABASE_ROLE INTO role FROM V$DATABASE;
    IF role = 'NO' THEN DBMS_SERVICE.START_SERVICE('prod');
    END IF;
    END;
    says trigger created, but during a swithover still the service is listeneing on listener.
    tns entry.
    prod =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (LOAD_BALANCE = YES)
    (ADDRESS = (PROTOCOL = TCP)(HOST = prod1)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = prod2)(PORT = 1521)) ---> primary db entry
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = proddr)(PORT = 1521)) --> DR DB entry
    (CONNECT_DATA =
    (SERVICE_NAME = prod)
    thanks in advance.
    Edited by: 854393 on Dec 29, 2012 11:52 AM

    Hello;
    So in the example below replace "ernie" with the alias you want the client to use.
    I can show you how I do it :
    First an entry need to be added to the client tnsnames.ora that uses a SERVICE_NAME instead of a SID.
    ernie =
    (DESCRIPTION =
        (ADDRESS_LIST =
           (ADDRESS = (PROTOCOL = TCP)(HOST = Primary.host)(PORT = 1521))
           (ADDRESS = (PROTOCOL = TCP)(HOST = Standby.host)(PORT = 1521))
           (CONNECT_DATA =
           (SERVICE_NAME = ernie)
    )Next the service 'ernie' needs to be created manually on the primary database.
    BEGIN
       DBMS_SERVICE.CREATE_SERVICE('ernie','ernie');
    END;
    /After creating the service needs to be manually started.
    BEGIN
       DBMS_SERVICE.START_SERVICE('ernie');
    END;
    /Several of the default parameters can now be set for 'ernie'.
    BEGIN
       DBMS_SERVICE.MODIFY_SERVICE
       ('ernie',
       FAILOVER_METHOD => 'BASIC',
       FAILOVER_TYPE => 'SELECT',
       FAILOVER_RETRIES => 200,
       FAILOVER_DELAY => 1);
    END;
    /Finally a database STARTUP trigger should be created to ensures that this service is only offered if the database is primary.
    CREATE TRIGGER CHECK_ERNIE_START AFTER STARTUP ON DATABASE
    DECLARE
    V_ROLE VARCHAR(30);
    BEGIN
    SELECT DATABASE_ROLE INTO V_ROLE FROM V$DATABASE;
    IF V_ROLE = 'PRIMARY' THEN
    DBMS_SERVICE.START_SERVICE('ernie');
    ELSE
    DBMS_SERVICE.STOP_SERVICE('ernie');
    END IF;
    END;
    /lsnrctl status - should show the new service.
    When I do this the Database will still register with the listener. I don't give that to the clients. That one will still be available but nobody knows about it. Meanwhile "ernie" moves with the database role.
    So in my example the default just hangs out in the background.
    Best Regards
    mseberg
    Edited by: mseberg on Dec 29, 2012 3:51 PM

Maybe you are looking for

  • Internal Error 7204, 6107874, 7775428, 0

    I've seen that I'm not the only one faced with the same problem of Internal Error 7202 but I'm getting rather annoyed because it is now nearly impossible to update a book without a crash. Typical error message as shown below. Error Internal Error 720

  • How do you set up two sided printing on HP Laser Jet P2055

    I just purchased a HP Laser Jet P2055 that advertises automoatic 2 sided printing; however, all of my copies are one-sided and I can't figure out where or how to format the printer to print two sided copies.  Any help is appreciated.

  • How do I get a password into my keychain?

    During a previous log-in to a website, I chose the option to not remember the password.  How do I now change it so the password is remembered?

  • To set the audio upto the end of the slide

    Hi all, Please let me know how I can extend the audio time upto the end of the slide time. For example a slide time will be 20 mts . I want to set the audio time upto that time . Now I am using the file loop ampient3. It can hear only 17 seconds. I a

  • Regarding studying for ABAP HANA

    Hello Gurus,      I have been working as a ABAP SRM techno-Functional Consultant for 5 years. Now I feel like to switch into something new and increase my knowledge. Can someone help me regarding learning of HANA for ABAP. Also can SAP HANA somehow b