PLEASE HELP! OLE DB COMMAND - capturing Stored Procedure output run from an OLE DB Command

I've searched through the related posts but cannot find exactly what I'm looking for.
I have an OLE DB Source that produces multiple rows (around 200) through a stored procedure. I need to take 3 values in each row and pass them to an OLE DB Command that runs another stored procedure for each row.
What I need is to get the output of the second stored procedure. I've read posts regarding using lookup, merge join, derived column but I still don't understand exactly how to put it all together. My destination is an excel file. Once I've obtained this output,
I believe I can figure out how to merge the OLE DB Source results and the OLE DB Command results into one table.  The output columns of the second SP are not available to be mapped to my destination file.
I've also explored running an Execute SQL task and passing the rowset to a variable but don't know how to pass that variable into the next stored procedure and then capture the second stored procedure results.
OLE DB Source contains Stored Procedure X:
DECLARE @return_value int
declare @newdate smalldatetime
set @newdate = convert(smalldatetime,convert(varchar(10),getdate(),101))
EXEC @return_value = [dbo].[getnumbers]
@dtAfterDate = @newdate,
@dtBeforeDate = '2099-12-31 5:00:00',
@Status = 'Any'
NEXT IN THE PIPELINE
OLE DB Command contains Stored Procedure Y:
EXEC [dbo].[getaddress] ?,?,?
One more thing, CREATING A TEMP TABLE ISN'T AN OPTION!
Thanks!

Thanks again for your help David!  Let me rephrase.  I'm working a server that doesn't need to be altered in anyway by me.  I just need to pull the data to my server so I can use it as needed.  I can't build any links either.  The
only other option I'd have would be coding SQL code with some sort of cursor similar to.
EXEC
@return_value = [dbo].[getnumbers]
@dtAfterDate = @newdate,
@dtBeforeDate = '2099-12-31 5:00:00',
@Status = 'Any'
For every number in the resulting table:
EXEC
[dbo].[getaddress] something1, something2, something 3

Similar Messages

  • Can you please help me in creating a stored procedure which can create a tablesapce in a specified location ?

    Hello ...
    Can you please help me in writing a procedure which can create tablespace. like i have a requirement that
    through sql query i need to get the location of where the data file of the newly created tablespace need to be stored.
    I need to store the location of the datafiles in a variable and i need to use that variable for specifying the location of the data file in newly created data file.
    please help me writing this procedure.....
    Regards vinay raj.
    Thanks in advance..

    select distinct substr(file_name,1,instr(file_name,'/',-1)) from dba_data_files;
    Run this in a few of your databases and see how often you ONLY get one row returned.
    Normally there is no "default" file system for data files (read about the DB_CREATE_FILE_DEST parameter and Oracle Managed Files for the exception to that).
    You could pick the same file system as an existing tablespace, but even then there is no guarantee that the file system has sufficient space for the new tablespace - you have to check that OUTSIDE the database.
    Given the query at the top it is pretty simple to create a SQL script that generates a SQL script.  This can be run in any database via sqlplus, and then the generated script can be edited as needed and run immediately, under human control, avoiding surprises.
    set echo off
    set feedback off
    set heading off
    set lines 100
    spool myts.sql
    select distinct 'create tablespace MYTS DATAFILE '''||substr(file_name,1,instr(file_name,'/',-1))||'myts01.dbf'' size 40M ONLINE;' stmt
    from dba_data_files;
    spool off
    For example, if you find there are multiple filesystems to choose from, run this then delete the filesystems you don't want from the generated script.  If you find there is only one file system but it is too full, work with your SA or storage to to add space or allocate a new file system, then modify the generated script accordingly.
    This example assumes a traditional file system, on a UNIX-style OS, not using Oracle-Managed files.  ASM or RAW will have a different format for the data file location, of course.

  • How to move stored procedure output to file

    Hi All,
    Thanks in advance.
    Please let me know how to move the oracle stored procedure output to the flat file.
    By
    Pyarajan

    Hi Pyarajan,
    As David posted, in the Data Flow Task (DFT), you can add an OLE DB Source, and create a connection manager to connect to the Oracle server using the Microsoft OLE DB for Oracle provider. Then, in the OLE DB Source Editor, you can set the “Data access mode”
    to “SQL command” and input the query to execute your Oracle stored procedure. To export the data to a flat file, we need to add a Flat File Destination to the DFT.
    Besides, also note that you need to install the Oracle client on this server, and you may need to edit the tnsnames.ora and sqlnet.ora files to establish the connection to Oracle successfully.
    References:
    http://commerceserverguy.wordpress.com/2011/12/04/ssis-package-tutorial/ 
    http://sqlblog.com/blogs/jorg_klein/archive/2011/06/09/ssis-connect-to-oracle-on-a-64-bit-machine.aspx 
    Regards,
    Mike Yin
    TechNet Community Support

  • How to schedule a Stored procedure to run every hour in SQL server express

    Hi,
    I am using SQL server express and I want to schedule a Stored procedure to run every hour.
    The Stored Procedure name is UpdateData.
    Can someone please give an example of how i can schedule this to run every hour, given that I do not have SQL Server agent on my express edition.

    Hello,
    You will find more options on the following thread.
    http://stackoverflow.com/questions/1675206/how-to-run-a-stored-procedure-every-day-in-sql-server-express-edition
    Hope this helps.
    Regards,
    Alberto Morillo
    SQLCoffee.com

  • Hello, Honestly I just updated my 4s and my iPad 3 to iOS 6 and when try to press on the Music app or the iTunes app it says "cannot connect to iTunes Store" Could you please help me with this thank you so much, Charbel from Lebanon

    Hello, Honestly I just updated my 4s and my iPad 3 to iOS 6 and when try to press on the Music app or the iTunes app it says "cannot connect to iTunes Store" Could you please help me with this thank you so much, Charbel from Lebanon

    See these previous discussions help.
    App Store Updates (but only Updates)...: Apple Support Communities
    Apps suddenly don't update: Apple Support Communities

  • Can a Stored Procedure be called from a Rule (not using Scripted JDBC Adap)

    Hello smart people,
    Question: Can a stored procedure be called from a Rule without using the Scripted JDBC Resource Adapter?
    What's Desired: The project has very strong sql developers and average at best Java developers. We have been successful at implementing the scripted JDBC Resource Adapter which makes use of Java Bean shells. The custom Java Bean shell contains code that calls a stored procedure. We are cool with that process.
    However we would like to be able to set up a Rule that calls a Stored Procedure without jumping thru the Java Bean Shell layer. The Stored Procedures we will be developing will not access the tables in the Waveset database. Instead the Stored Procedures will be accessing custom tables in a custom database. The custom DBase will be housed on the same dBase server as the Waveset database.
    Environment: Identity Manager 7.1. Oracle 10i, and Unix OS.
    It seems to me that this task is possible, but the posts in this forum make it sound like perhaps it is not.
    Do any of you smart people agree that it should be possible or better yet has anyone implemented such an approach?
    Thanks.

    We did this by putting all of the JDBC calls into a Java class, then instantiated that class and called the methods from within a rule. An example of a rule we used is this:
    <Rule authType='UserMembersRule' name='List UserType'>
      <block>
        <defvar name='adapter'>
          <new class='com.waveset.adapter.ScriptedJdbcResourceAdapter'/>
        </defvar>
        <invoke name='setResource'>
          <ref>adapter</ref>
          <invoke name='getObject' class='com.waveset.ui.FormUtil'>
            <ref>context</ref>
            <s>Resource</s>
            <s>Banner</s>
          </invoke>
        </invoke>
        <defvar name='jdbcHelper'>
          <new class='edu.bates.JDBCHelper'>
            <invoke name='getConnection'>
              <ref>adapter</ref>
            </invoke>
          </new>
        </defvar>
        <defvar name='userList'>
          <new class='java.util.ArrayList'/>
        </defvar>
        <invoke name='addQueryToList'>
          <ref>jdbcHelper</ref>
          <s>SQL QUERY HERE</s>
          <ref>userList</ref>
        </invoke>
        <ref>userList</ref>
      </block>
      <MemberObjectGroups>
        <ObjectRef type='ObjectGroup' id='#ID#Top' name='Top'/>
      </MemberObjectGroups>
    </Rule>

  • Stored procedure OUTPUT VARCHAR2 value truncated using 12c client on Server 2012

    Questions:
    1) Is there a version of Oracle Client 11g which runs on Server 2012? My 11.2.0.0, when the installer begins, says my Server 2012 (8GB mem, 80 GB drive) does not meet the minimum requirements and I should not install it. This same installer worked on Server 2008.
    2) Have you seen the following behavior from Oracle client 12c? Are there any patches?
    When I was unable to find a version of Oracle 11g client for Windows Server 2012, I downloaded Oracle 12c. We have a few stored procedures, when invoked from a Windows Server 2008 with an 11g client work. However, when running that some code on a Windows Server 2012 machine does not work. I have certain stored procedures with an OUTPUT parameter when executed on 2012 has the values truncated.
    For instance:
    Output parameter is set to return "SUCCESS", but was is received is "SUC"
    Ouput parameter is set to return "AllSet", but it returns "All"
    Output parameter is set to cast a NUMBER to a VARCHAR2 and the value is a 5 digit number, but only the first two digits are returned. Ex: "83976", but only "83" appears.
    The above takes place when run from Server 2012, but works fine from Server 2008 and Win 7.

    Questions:
    1) Is there a version of Oracle Client 11g which runs on Server 2012? My 11.2.0.0, when the installer begins, says my Server 2012 (8GB mem, 80 GB drive) does not meet the minimum requirements and I should not install it. This same installer worked on Server 2008.
    2) Have you seen the following behavior from Oracle client 12c? Are there any patches?
    When I was unable to find a version of Oracle 11g client for Windows Server 2012, I downloaded Oracle 12c. We have a few stored procedures, when invoked from a Windows Server 2008 with an 11g client work. However, when running that some code on a Windows Server 2012 machine does not work. I have certain stored procedures with an OUTPUT parameter when executed on 2012 has the values truncated.
    For instance:
    Output parameter is set to return "SUCCESS", but was is received is "SUC"
    Ouput parameter is set to return "AllSet", but it returns "All"
    Output parameter is set to cast a NUMBER to a VARCHAR2 and the value is a 5 digit number, but only the first two digits are returned. Ex: "83976", but only "83" appears.
    The above takes place when run from Server 2012, but works fine from Server 2008 and Win 7.

  • Without calling stored procedure or functions from database

    Hi,
    I am using Jdeveloper 11.1.1.5.0.
    =>How to do PL/SQL procedures and functions in ADF without calling stored procedure or function from DB?

    S, PL/SQL procedures and functions are done in Application Module class or in managed bean..By calling the stored procedures or functions from DB.
    But I am asking how to do if DB doesn't have any procedures,triggers and functions.

  • Premiere Pro CS5: please help problem with importing a file error output "there was an error decompressing audio or video"

    please help problem with importing a file error output "there was an error decompressing audio or video"

    this is related to what adobe program/version?

  • Please help.  I can't open pdf comparables report from MLS Realist website.

    Please help.  I can't open pdf comparables reporT from MLS Realist website.  Thank you!

    I have this problem too and no one seems to be able to figure out what should be done. Feel like not using iPad any more.

  • Please help, i used to have various colors to select from when selecting FILL in Keynote such as red, now I only have a few pastel colors. I cannot seem to fix this problem to get the original colors back. Can someone please advise what I need to do.

    Please help, i used to have various colors to select from when selecting FILL in Keynote such as red, now I only have a few pastel colors. I cannot seem to fix this problem to get the original colors back. Can someone please advise what I need to do.

    i have on my ipad2 and re-download it - but I can't afford to loose the presentation that I currently have ....??
    Are you using Keynote on an iPad?
    If so you posted in the wrong forum, this is the Keynote Mac discussion and it is a different application. I dont know how the iPad works at all, so I cant help you, post in the Keynote iOS discussion.
    If you are using a Mac, the colours palette is accesed by clicking on the colour well (arrowed) and then selecting a colour chip or changing the color in the colour wheel.

  • Help required in executing the stored procedure and capturing the responce

    Hi,
    I have to execute a stored procedure in oracle database and if it successful then have to create a status IDOC.
    The stored procedure may throw an exception(it is not an out parameter), in such cases i should capture the exception as part of the response message and send this information while creating the status IDOC.
    i would like if the below is possible.
    As the stored procedure is not returning( this means if you execute the procedure manually it will display the result), will it be possible to capture the result which has the exception details?
    any direction to this will be highly appreciated.
    Many thanks.
    Regards,
    Rahul

    @Bhaskar,
    i need to execute the procedure by sending the input data from a file and capture the result after executing the procedure to create a status idoc
    im not sure how the provided links helps me here.
    i m not looking for any look ups. i would like to know is there any way that we can capture the result after executing the database procedure?
    Edited by: Rahul_10416 on Dec 5, 2011 10:07 PM

  • Can someone help me diagnose a strange stored procedure performance issue please?

    I have a stored procedure (posted below) that returns message recommendations based upon the Yammer Networks you have selected. If I choose one network this query takes less than one second. If I choose another this query takes 9 - 12 seconds.
    /****** Object: StoredProcedure [dbo].[MessageView_GetOutOfContextRecommendations_LargeSet] Script Date: 2/18/2015 3:10:35 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [dbo].[MessageView_GetOutOfContextRecommendations_LargeSet]
    -- Parameters
    @UserID int,
    @SourceMessageID int = 0
    AS
    BEGIN
    -- variable for @HomeNeworkUserID
    Declare @HomeNeworkUserID int
    -- Set the HomeNetworkID
    Set @HomeNeworkUserID = (Select HomeNetworkUserID From NetworkUser Where UserID = @UserID)
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON
    -- Begin Select Statement
    Select Top 40 [CreatedDate],[FileDownloadUrl],[HasLinkOrAttachment],[ImagePreviewUrl],[LikesCount],[LinkFileName],[LinkType],[MessageID],[MessageSource],[MessageText],[MessageWebUrl],[NetworkID],[NetworkName],[PosterEmailAddress],[PosterFirstName],[PosterImageUrl],[PosterName],[PosterUserName],[PosterWebUrl],[RepliesCount],[Score],[SmallIconUrl],[Subjects],[SubjectsCount],[UserID]
    -- From View
    From [MessageView]
    -- Do Not Return Any Messages That Have Been Recommended To This User Already
    Where [MessageID] Not In (Select MessageID From MessageRecommendationHistory Where UserID = @UserID)
    -- Do Not Return Any Messages Created By This User
    And [UserID] != @UserID
    -- Do Not Return The MessageID
    And [MessageID] != @SourceMessageID
    -- Only return messages for the Networks the user has selected
    And [NetworkID] In (Select NetworkID From NetworkUser Where [HomeNetworkUserID] = @HomeNeworkUserID And [AllowRecommendations] = 1)
    -- Order By [MessageScore] and [MessageCreatedDate] in reverse order
    Order By [Score] desc, [CreatedDate] desc
    ENDThe Actual Execution Plan Shows up the same; there are more messages on the Network that is slow, 2800 versus 1,500 but the difference is ten times longer on the slow network.Is the fact I am doing a Top 40 what makes it slow? My first guess was to take the Order By Off and that didn't seem to make any difference.The execution plan is below, it takes 62% of the query to look up theIX_Message.Score which is the clustered index, so I thought this would be fast. Also the Clustered Index Seek for the User.UserID take 26%which seems high for what it is doing.
    I have indexes on every field that is queried on so I am kind of at a loss as to where to go next.
    It just seems strange because it is the same view being queried in both cases.
    I tried to run the SQL Server Tuning Wizard but it doesn't run on Azure SQL, and my problem doesn't occur on the data in my local database.
    Thanks for any guidance, I know a lot of the slowness is due to the lower tier Azure SQL we are using, many of the performance issues weren't noticed when were on the full SQL Server, but the other networks work extremely fast so it has to be something to
    with having more rows.
    In case you need the SQL for the View that I am querying it is:
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE VIEW [dbo].[MessageView]
    AS
    SELECT M.UserID, M.MessageID, M.NetworkID, N.Name AS NetworkName, M.Subjects, M.SubjectsCount, M.RepliesCount, M.LikesCount, M.CreatedDate, M.MessageText, M.HasLinkOrAttachment, M.Score, M.WebUrl AS MessageWebUrl, U.UserName AS PosterUserName,
    U.Name AS PosterName, U.FirstName AS PosterFirstName, U.ImageUrl AS PosterImageUrl, U.EmailAddress AS PosterEmailAddress, U.WebUrl AS PosterWebUrl, M.MessageSource, M.ImagePreviewUrl, M.LinkFileName, M.FileDownloadUrl, M.LinkType, M.SmallIconUrl
    FROM dbo.Message AS M INNER JOIN
    dbo.Network AS N ON M.NetworkID = N.NetworkID INNER JOIN
    dbo.[User] AS U ON M.UserID = U.UserID
    GO
    The Network Table has an Index on Network ID, but it non clustered but I don't think that is the culprit.
    Corby

    I marked your response as answer because you gave me information I didn't have about the sort. I ended up rewriting the query to be a join instead of the In's and it improved dramatically, about one second on a very minimal Azure SQL database, and before
    it was 12 seconds on one network. We didn't notice the problem at all before we moved to Azure SQL, it was about one - three seconds at most.
    Here is the updated way that was much more efficient:
    CREATE PROCEDURE [dbo].[Procedure Name]
    -- Parameters
    @UserID int,
    @SourceMessageID int = 0
    AS
    BEGIN
    -- variable for @HomeNeworkUserID
    Declare @HomeNeworkUserID int
    -- Set the HomeNetworkID
    Set @HomeNeworkUserID = (Select HomeNetworkUserID From NetworkUser Where UserID = @UserID)
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON
    ;With cteMessages As
    -- Begin Select Statement
    Select (Fields List)
    -- Join to Network Table
    From MessageView mv Inner Join NetworkUser nu on MV.NetworkID = nu.NetworKID -- Only Return Networks This User Has Selected
    Where nu.HomeNetworkUserID = @HomeNeworkUserID And AllowRecommendations = 1
    -- Do Not Return Any Messages Created By This User
    And mv.[UserID] != @UserID
    -- Do Not Return The MessageID
    And mv.[MessageID] != @SourceMessageID
    ), cteHistoryForThisUser As
    Select MessageID From MessageRecommendationHistory Where UserID = @UserID
    -- Begin Select Statement
    Select Top 40 (Fields List)
    -- Join to Network Table
    From cteMessages m Left Outer Join cteHistoryForThisUser h on m.MessageID = h.MessageID
    -- Do Not Return Any Items Where User Has Already been shown this Message
    Where h.MessageID Is Null
    -- An Order By Is Needed To Get The Best Content First
    Order By Score Desc
    END
    GO
    The Left Outer Join to test for null was the biggest improvement, but it also helped to join to the NetworkUser table instead of do the In sub query.

  • Please Help!!! error executing procedure and populating to gridview

    I have used a tool to convert my sql 2000 stored procedure to Oracle 10g PL/SQL, here is an example
    SQL 2000 Stored Procedure
    CREATE PROCEDURE [GetEmployees]
    AS
    Select * from EMPMST ORDER BY emp_name
    GO
    After Transformation i got 2 files, one was a procedure and other a package
    CREATE OR REPLACE PACKAGE GLOBALPKG
    AS
         TYPE RCT1 IS REF CURSOR;
         TRANCOUNT INTEGER := 0;
         IDENTITY INTEGER;
    END;
    CREATE OR REPLACE PROCEDURE GetEmployees
         RCT1 IN OUT      GLOBALPKG.RCT1
    AS
    BEGIN
         OPEN RCT1 FOR
         SELECT *
         FROM EMPMST
         ORDER BY emp_name;
    END;
    When i execute the procedure GetEmployees i got this error :
    SQL> execute GetEmployees;
    BEGIN GetEmployees; END;
    ERROR at line 1:
    ORA-06550: line 1, column 7:
    PLS-00306: wrong number or types of arguments in call to 'GETEMPLOYEES'
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    I am developing a webpage (asp.net using Visualstudio .net 2005 ) where i have a grid view in which i want to display all the output of the GetEmpoyee procedure but it is giving an error.
    Please Help me in debugging this error. Thanks in advance.

    Hi Pruthvi,
    It seems to me the convertion is correct. You need a REF CURSOR and by the Stored Procedure itself without using Package the REF CURSOR cannot specify as OUT output parameter.
    You can use SQL Developer to help you to make sure you put the Package in the right Database Schema.
    http://otn.oracle.com/sqldeveloper
    After you converted the Stored Procedure you need to modify your .NET code as well. The .NET code that calls your old MS SQL Stored Procedure will not work. To see how to involve the REF CURSOR in .NET you can check here:
    http://blogs.oracle.com/MingMan/stories/storyReader$9
    Good Luck
    Ming Man

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

Maybe you are looking for