Can we give multiple select statements in one function in PL/SQL

Hi All,
I am new to the PL/SQL. Can you please help me with the following problem.
I am cretaing a function in which I have to extract some fields from three tables and to handle three tables I am first extracting the primary key from one table(acting as a foreign key for other table) in a variable declared in declare section, then I am selecting the required fields from the other table by giving that variable name in Where condition. My code is attached as well.
e.g
Create or Replace function <function name>(arguments)
Return return type
AS
a1 a%TYPE;
b_new b%TYPE;
n fn%TYPE
Select a into a1 from table1 where b like b_new;
Select fn into n from table2 where id like a1;
return a1,n; (can we pass record like this?)
end;

Left's first look at the approach you use.. 2 SQL statements that can be done using a single SQL statement.
Actually it is not as bad as some other code posted here where people go and write complex PL/SQL code to do what could have been done using a single SQL statement.
There are a couple of golden rules in Oracle development. One of these is to 'Maximise SQL and Minimise PL/SQL'.
Why take a value from one SQL statement into PL/SQL and then use that value in a second SQL statement?
Rather do this:
select
fn into n
from table2
where id like (Select a from table1 where b like b_new);Also keep in mind that in PL/SQL the above SQL construct is only able to fetch a single row - look at the bold above. The variable N can only hold a single value. If that SQL SELECT returns more than one value, how is variable N suppose to hold it?
I suggest that you read [url http://www.oracle.com/pls/db102/to_toc?pathname=appdev.102%2Fb14261%2Ftoc.htm&remark=portal+%28Books%29]Oracle® Database PL/SQL User's Guide and Reference guide.

Similar Messages

  • How to return the result set of multiple select statements as one result set?

    Hi All,
    I have multiple select statements in my stored procedure that I want to return as one result set 
    for instance 
    select id from tableA
    union 
    select name from table b 
    but union will not work because the result sets datatypes are not identical so how to go about this ?
    Thanks

    You have to CAST or CONVERT (or implicitly convert) the columns to the same datatype.  You must find a datatype that both columns can be converted to without error.  In your example I'm guessing id is an int and name is a varchar or nvarchar. 
    Since you didn't convert the datatypes, SQL will use its data precedence rules and attempt to convert name to an int.  If any row contains a row that has a value in name that cannot be converted to an int, you will get an error.  The solution is
    to force SQL to convert the int to varchar.  So you want something like
    select cast(id as varchar(12)) from tableA
    union
    select name from tableb
    If the datatypes are something other that int or varchar, you must find a compatable datatype and then convert one (or both) of the columns to that datatype.
    Tom

  • Can we give a select statement for list items

    HI
    I am new to forms please help me out...
    My question is can we give a query to list item instead of specifying the values.
    i want to fetch the values from the lookups so i need to write query which can fetch the meanings of lookups in the list...
    Thanks
    Sandeep.G

    Hello,
    See the online-help (ctrl-h) of the Forms Builder concerning the following:
    Create_Group_From_Query()
    Populate_Group_From_Query()
    Populate_List()
    Populate_List_From_Query()Francois

  • Multiple Select statements in EXECUTE SQL TASK in SSIS

    Can we write multiple select statements in execute sql task(SSIS).
    If possible how to assign them to variables in result statement

    Hi ,
    You can use below steps to set result set to variable.
    1.In the Execute SQL Task Editor dialog box, on the General page, select the Single row, Full result set, or XML result set type(According to your result set).
    2.Click Result Set.
    3.To add a result set mapping, click Add.
    4.From the Variables Name list, select a variable or create a new variable.
    What is multiple selection ? May be below can be used in your scenario !
    http://stackoverflow.com/questions/17698908/how-to-set-multiple-ssis-variables-in-sql-task
    What you want to achieve ?
    Thanks
    Please Mark This As Answer or vote for Helpful Post if this helps you to solve your question/problem. http://techequation.com

  • Can i join these sql select statement in one

    Hi All,
    Can i join this two select statement in to one select statement.
    select username from dba_users where username like 'SAP%%%'; # to the Schemaid of the below /BMC/YGO_CPROD.
    select YOP_PRD_NM, YOP_VERS from <schemaid>."/BMC/YGO_CPROD"; # To know a version from the table.
    I am using this in a script can any help to join the above select statements into one.
    Schemaid should be passed to this select YOP_PRD_NM, YOP_VERS from <schemaid>."/BMC/YGO_CPROD"; and i need to get output!
    Thanks a lot

    Are you asking about using the output of one query as the input to the WHERE clause of another?
    If so look at the demos here: http://www.psoug.org/reference/conditions.html
    PS: There is zero value in the construct 'SAP%%%'
    What is it you are trying to do?
    One "%" wildcard is sufficient.

  • Multiple Select boxes in one form

    Does anyone know if it is possible to have multiple select
    boxes inside one form? I have six different select boxes that are
    generated by six separate queries. The action page for all six
    select boxes is the same so I just want one submit button so users
    don't get confuse. I don't want to cluster up the page with submit
    buttons but I may resort to that option.
    My problem is the select boxes generate a number and when
    that number is submitted I want to pass two other variables with it
    to the action page. I tried putting a "Form" tag around the 6
    select boxes. Inside each select box I tried to add an "Input
    type="hidden"" and give thte name and values that I needed. That
    worked for the first select box but when I tried the second select
    box it used the hidden values for the first select box.
    If anyone can help, I would greatly appreciate it. Or if you
    have any other suggestions I am open to any.
    Thanks

    Paross1,
    I wasn't thinking about that and you gave me a great idea.
    Here is how I changed my select boxes.
    <cfform
    action="Resolution_History.cfm?year=#year#&sessiontype=#sessiontype#&btype=res"
    name="form">
    <select name="SRINPUT">
    <option value="">SR
    <CFOUTPUT Query="findSR"><Option
    Value="#BILLNUMBER#">#BILLNUMBER#</cfoutput>
    </select>
    <select name="HRINPUT">
    <option value="">HR
    <CFOUTPUT Query="findHR"><Option
    Value="#BILLNUMBER#">#BILLNUMBER#</cfoutput>
    </select>
    <select name="SCRINPUT">
    <option value="">SCR
    <CFOUTPUT Query="findSCR"><Option
    Value="#BILLNUMBER#">#BILLNUMBER#</cfoutput>
    </select>
    <br>
    <select name="HCRINPUT">
    <option value="">HCR
    <CFOUTPUT Query="findHCR"><Option
    Value="#BILLNUMBER#">#BILLNUMBER#</cfoutput>
    </select>
    <select name="SJRINPUT">
    <option value="">SJR
    <CFOUTPUT Query="findSJR"><Option
    Value="#BILLNUMBER#">#BILLNUMBER#</cfoutput>
    </select>
    <select name="HJRINPUT">
    <option value="">HJR
    <CFOUTPUT Query="findHJR"><Option
    Value="#BILLNUMBER#">#BILLNUMBER#</cfoutput>
    </select>
    <INPUT TYPE="Submit" VALUE="Submit" alt="submit
    button">
    </cfform>
    On the action page I need the below IF statement to work so
    it will set the variables. It isn't working at this time. Its not
    bringing the values of billnumber, houseorig or the billtype.
    Do you have any thoughts? I know it is close. I need to set
    all of the inputs to input4 to generate my queries.
    <cfif form.srinput gt 0>
    <cfset houseorig eq "s">
    <cfset billtype eq "r">
    <cfset srinput eq input4>
    <cfelseif form.hrinput gt 0>
    <cfset houseorig eq "h">
    <cfset billtype eq "r">
    <cfset hrinput eq input4>
    <cfelseif form.scrinput gt 0>
    <cfset houseorig eq "sc">
    <cfset billtype eq "r">
    <cfset scrinput eq input4>
    <cfelseif form.hcrinput gt 0>
    <cfset houseorig eq "hc">
    <cfset billtype eq "r">
    <cfset hcrinput eq input4>
    <cfelseif form.sjrinput gt 0>
    <cfset houseorig eq "sj">
    <cfset billtype eq "r">
    <cfset sjrinput eq input4>
    <cfelse>
    <cfset houseorig eq "hj">
    <cfset billtype eq "r">
    <cfset hjrinput eq input4>
    </cfif>

  • Executing a stored procedure containing multiple Select statements

    Post Author: Beverly
    CA Forum: General
    I am using Crystal  10.0 against a MS SQL 2000 server.
    I am trying to create a report based on a stored procedure that contains multiple select statements.  The sp requires a single parameter (Claim number) and contains 17 Select statements that produce results.
    I am able to use the Add command and execute the sp with the parameter, but I am only getting the results of the first select statement in the sp back in my data set.  Is there a way to have the data from each Select statement returned to my report?
    I have used Crystal for a while, but pretty much for straight-forward reporting.  I am familiar with the basics of SQL.
    I would appreciate any help anyone can offer.
    Thanks.

    Post Author: BISoftware
    CA Forum: General
    I believe Crystal Reports can only handle one recordset at a time, which means it can only handle a single select statement.  The only way I can see around this would be to break up your stored procedure into multiple stored procedures, so that each only contains a single select statement.  Then, use subreports to report on each individual sp. Hope this helps. - Davewww.BusinessSoftwareResource.com

  • Preview; How can I open multiple PDF's in one window with Preview?

    How can I open multiple PDF's in one window with Preview?
    I have gone to Previews preferences and selected 'Open groups of images in the same window' and have tried every setting imaginable and nothing works. What am I doing wrong?

    Hi, a few ways..
    http://www.monkeybreadsoftware.info/Freeware/CombinePDFs.shtml
    http://www.monkeybreadsoftware.info/Freeware/CombinePDFs.shtml
    http://www.macosxhints.com/article.php?story=20060327192826493&lsrc=osxh

  • Need to create a transaction for multiple select statements?

    Hello,
    I am a newbie and have a question about database transaction, e.g. whether/not to enclose multiple select statements (and select statements only) into a transaction.
    My database is set to transaction isolation level 2: REPEATABLE READ, where dirty read & non-repeatable read are not allowed, only phantom read is allowed.
    Now, in my code I have a number of methods that only contain select statements only. Since they are merely select statements, which don't do any modifications to the data, I am not sure if I am supposed to enclose them into a transaction.
    However, if I don't put them into a transaction will the transaction isolation level takes into effect automatically when another user is modifying the data that I am reading? In other words, I need to make sure the select statements will never do either dirty read or non-repeatable read. But I am not sure if it is necessary to enclose multiple select statements in a transaction, since I believe putting the select statements into a transaction will put some locks to the data being read which may reduce the concurrency of my application.
    Any help/advice would be very much appreciated.
    Duane

    You might want to try asking this on a forum that specific to your database. I suspect the answer can vary depending on the database and probably requires in depth knowledge of what the database does.

  • Can we give multiple destination files for a crystal report.

    Hi all,
    Can i give multiple destination files for crystal check process when the out put type is file.
    What i want is the text file, which will be created at the destination given by the user, must be created at other location also.
    Please help me.
    Thank you,
    Bye.

    Multiple default values for a presentation variable is only possible in 11g as Srini Said.
    Check mark : Enable user to select multiple values. (under variable datatypes)
    http://obieetraining11.blogspot.com/2012/08/obiee-11g-using-presentation-variable.html
    If it is 10g, then it is not possible.

  • In Pages (5.5.1) can I create multiple Paragraph Styles within one paragraph?

    In Pages (5.5.1) can I create multiple Paragraph Styles within one paragraph. 
    I need to make a table of contents for my Mater's Thesis and want to use the automatic table of contents feature.  I want the first sentence of a paragraph to have a different paragraph setting than the rest of the paragraph.  Is this possible?
    Thanks!

    Nice of you to be doing this for your Mother's Thesis.
    There are several issues here:
    1. A Paragraph Style is what it says, the style for the paragraph
    2. You can format text within a paragraph by applying a Character Style
    3. Pages 5.5.1 lets you apply formatting to overall text for the T.O.C. but not parts of it and you can not retain that formatting as a Paragraph style
    So in answer to your question, no.
    You will need to create the T.O.C. manually and unfortunately as Pages 5.5.1 can't create bookmarks (just one of over 100 missing features) you will be unable to link the T.O.C. to the referred pages in the list.
    Peter
    Apple's marketing slogan:
    Pages 5.5.1 - Can't Do That!

  • HT4436 can I have multiple icloud accounts on one device

    can I have multiple icoud accounts on one device (ipad)?

    Are you sure only primary account can use Find my iPhone?
    I added a secondary account to my daughters iphone and was able to use it for Find my iPhone.
    It warns you that the Find my iphone service will be disabled on the other icloud account.
    I think this is a good change so I can use a single account to track all the devices.
    Looks like only photostream and documents/data are tied to the primary account.

  • How can I have multiple accounts and only one library

    How can I have multiple accounts and only one library that all the purchased items feed into?

    ask your wireless provider.

  • Can you have multiple iCloud's with one Apple ID?

    Can you have multiple iCloud's with one Apple ID?

    You can have multiple iclouds with multiple id's

  • Can I have multiple volume levels in one track?

    I have a track composed of audio from a cassette recorder of a now deceased relative's voice.
    Since the cassette was turned on and off during recording, the recordings are at different volumes.
    I have been able to import the cassette into a GarageBand project, and delete dead space, but now I find that the volume is not consistent.
    Any way to remedy that? I am wondering if I need to chop it up and make more than one track, and adjust the volume on each track? Will this work as long as the tracks don't overlap?
    And then do I send the finished product to iTunes in order to be able to use it in an iMovie?
    Or can I drag it right from GarageBand to iMovie?
    Many thanks, I'm a very new GarageBand user.

    JGolomb wrote:
    Can I have multiple volume levels in one track?.
    search GB's help for "curve"
    And then do I send the finished product to iTunes in order to be able to use it in an iMovie?
    "Share" it to disc
    can I drag it right from GarageBand to iMovie?
    no

Maybe you are looking for

  • Network and Sharing Centre problems (again)

    I seem to be tearing my hair out once again with the Network List Service. Several months back the Network and Sharing centre stopped displaying or reporting the active networks, although I was connected with internet access. A consequence was most o

  • How can I restore Camera Roll to my iphone 4s? I have Photo Stream and am backed up on iCloud

    How can I restore Camera Roll to my iphone 4s? I have Photo Stream and am backed up on iCloud

  • SQL Server two database at two different locations

    Hi Guys, We are having same database at two different locations (Database A with Sqlserver 2008/Express, database B with Sqlserver 2012). Our requirement is when ever a row is inserted in a table of Database A from location 1. Same value should also

  • Location of Log file

    Hi, Could anyone help me to find the  location of log file in crystal reports serverXI. I need the information of crystal reports execution time,parameters used etc. Anything related to crystal reports is fine with me. Thanks in advance. -Vijay Kanth

  • Spotlight only showing results of 1 partition

    I skipped lion and went from Snow Leopard to Mountain Lion. I am using a 1tb External HD. If I type "screen shot" into spotlight, it only shows the files on 1 of the partitions (there are 4 partitions on the drive) The one thing I noticed is that if