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

Similar Messages

  • How to edit the records value fetched by select statement query in sqldever

    How to edit the records value fetched by select statement query in sqldever 2.1.1.
    EX-
    SELECT * FROM emp WHERE ename like 'Jaga%'
    Edited by: user9372056 on Aug 31, 2010 2:16 AM

    Hi,
    Although some forum members may be using that tool, there is still a dedicated forum for SQL Developer.
    SQL Developer
    Maybe your chances are better there.
    Regards
    Peter

  • How to Return the Index of a Mouse-Selected Array Element in the View Interface of Diadem

    Hello,
    Does anybody have an idea of how to obtain the index of a mouse-selected array element in the View interface of Diadem??
    Thanks!

    Hi Ovidius,
    Keep explaining.  The only way I know of to trap a selected cell value in VIEW is to embed into the desired VIEW area a second non-modal SUDialog that has a Table control or an XTable control displaying the values of certain data channels.  There are callbacks in both table controls for cell selection, and you can configure the table control to allow only single cell selection if that's what you want.  The regular table control will be easier to program, but the XTable control will perform much better for larger channels.
    But what happens with that value the user selected?  Is it used for a calculation?  Is it added to a report?  Why would the user select that cell in a table rather than selecting the corresponding feature in a graph with the crosshair cursor?
    Brad Turpin
    DIAdem Product Support Engineer
    National Instruments

  • Why does getdate() function return same time value for multiple select statements executed sequentially

    When I run the following code
    set nocount on
    declare @i table(id int identity(1,1) primary key, sDate datetime)
    while((select count(*) from @i)<10000)
    begin
    insert into @i(sDate) select getdate()
    end
    select top 5 sDate, count(id) selectCalls
    from @i
    group by sDate
    order by count(id) desc
    I get the following results. 
    sDate                   selectCalls
    2014-07-30 14:50:27.510 406
    2014-07-30 14:50:27.527 274
    2014-07-30 14:50:27.540 219
    2014-07-30 14:50:27.557 195
    2014-07-30 14:50:27.573 170
    As you can see the select getdate() function returned same time up to the milisecon 406 time for the first date value.  This started happening when we moved our applications to a faster server with four processors.  Is this correct or am I
    going crazy?
    Please let me know
    Bilal

    Observe that adding 2 ms is accurate only with datetime2.  As noted above, datetime does not have ms resolution:
    set nocount on
    declare @d datetime, @i int, @d2 datetime2
    select @d = getdate(), @i = 0, @d2 = sysdatetime()
    while(@i<10)
    begin
    select @d2, @d, current_timestamp, getdate(), sysdatetime()
    select @d = dateadd(ms,2,@d), @i = @i+1, @d2=dateadd(ms,2,@d2)
    end
    2014-08-09 08:36:11.1700395 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.1700395
    2014-08-09 08:36:11.1720395 2014-08-09 08:36:11.173 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.1700395
    2014-08-09 08:36:11.1740395 2014-08-09 08:36:11.177 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.1700395
    2014-08-09 08:36:11.1760395 2014-08-09 08:36:11.180 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.1700395
    2014-08-09 08:36:11.1780395 2014-08-09 08:36:11.183 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.1700395
    2014-08-09 08:36:11.1800395 2014-08-09 08:36:11.187 2014-08-09 08:36:11.170 2014-08-09 08:36:11.170 2014-08-09 08:36:11.1700395
    DATE/TIME functions:
    http://www.sqlusa.com/bestpractices/datetimeconversion/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

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

  • Values from a Multi-Select in the where clause of a Select statement

    I have a web page that solicits query parameters from the user.
    The selections that the user makes will populate the WHERE clause of a Select statement.
    One of the controls on the page is a multi-select control.
    When this page posts, I would like to execute a Select statement wherein the selected values from this control appear in the .. Column IN ( <list here> ) portion of the WHERE clause.
    This is an extremely common scenario, but I cannot seem to locate a how-to or message thread that addresses this specific case.
    I have an idea that it may involve dynamic SQL or Execute Immediate, but cannot seem to pin down the answer.
    Any help would be greatly appreciated!

    anonymous - As illustrated here: Re: Search on a typed in list of values
    Scott

  • How to add a dummy row in the result set of a SELECT statement.

    Hello Everyone -
    I have requirment to add a dummy row in the result set of a SELECT statement.
    For e.g. lets say there is a table Payment having following colums:
    Payment_id number
    status varchar2(10)
    amount number
    payment_date date
    so here is the data :-
    Payment_id Status Amount payment_date
    1 Applied 100 12/07/2008
    2 Reversed 200 01/ 06/2009
    3 Applied 300 01/ 07/2009
    Here is my SQL
    Select * form payment where payment_date >= 01/01/2009
    Output will be
    2 Reversed 200 01/ 06/2009
    3 Applied 300 01/ 07/2009
    My desired output is below
    2 Reversed 200 01/ 06/2009
    3 Applied 300 01/ 07/2009
    2 Reversed -200 01/ 06/2009 ------(Dummy Row)
    Thrid row here is the dummy row which I want to add when status is "Reversed"
    I would be very thankful for any kind of help in this regard ...
    Thanks,
    Gaurav

    Cartesion joining against a dummy table is a useful method of creating a dummy row:
    with my_tab as (select 1 cust_id, 1 Payment_id, 'Applied' Status, 100 Amount, to_date('12/07/2008', 'mm/dd/yyyy') payment_date from dual union all
                    select 1 cust_id, 2 Payment_id, 'Reversed' Status, 200 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
                    select 1 cust_id, 3 Payment_id, 'Applied' Status, 300 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
                    select 2 cust_id, 1 Payment_id, 'Applied' Status, 100 Amount, to_date('12/07/2008', 'mm/dd/yyyy') payment_date from dual union all
                    select 2 cust_id, 2 Payment_id, 'Reversed' Status, 200 Amount, to_date('01/05/2009', 'mm/dd/yyyy') payment_date from dual union all
                    select 2 cust_id, 3 Payment_id, 'Applied' Status, 300 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
                    select 2 cust_id, 4 Payment_id, 'Reversed' Status, -400 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
                    select 2 cust_id, 5 Payment_id, 'Applied' Status, 500 Amount, to_date('01/07/2009', 'mm/dd/yyyy') payment_date from dual),
                    --- end of mimicking your table
          dummy as (select 'Reversed' col1, 1 rn from dual union all
                    select 'Reversed' col1, 2 rn from dual)
    select mt.cust_id,
           mt.payment_id,
           mt.status,
           decode(dummy.rn, 2, -1*mt.amount, mt.amount) amount,
           mt.payment_date
    from   my_tab mt,
           dummy
    where  mt.status = dummy.col1 (+)
    order by mt.cust_id, mt.payment_id, dummy.rn nulls first;
    CUST_ID     PAYMENT_ID     STATUS     AMOUNT     PAYMENT_DATE
    1     1     Applied     100     07/12/2008
    1     2     Reversed     200     06/01/2009
    1     2     Reversed     -200     06/01/2009
    1     3     Applied     300     06/01/2009
    2     1     Applied     100     07/12/2008
    2     2     Reversed     200     05/01/2009
    2     2     Reversed     -200     05/01/2009
    2     3     Applied     300     06/01/2009
    2     4     Reversed     -400     06/01/2009
    2     4     Reversed     400     06/01/2009
    2     5     Applied     500     07/01/2009Edited by: Boneist on 07-Jan-2009 23:10
    NB. You may have to mess around with the ordering if that's not come back in the order you wanted. You didn't mention what the rules were for any expected ordering though, so I've made up my own *{;-)
    Also, I added an identifier (cust_id) to differentiate between different sets of payments, since that's usually the case. Remove that if it's not applicable for your case.

  • How to return the name (or ID) of the Task FLow in Script

    Sitaution; two task flows created which can be accessed via Tools > TaskFlows within FDQM
    Task Flow "1.1 Multi Load - Import" --> Should run Batch Process Up to Import (enmBatchProcessLevel: 2)
    Task Flow "2.1 Multi Load - Import Up To Validate" --> Should run Batch Process Up to Validate (enmBatchProcessLevel: 4)
    I have developed one generic script which I would like to use for each task flow.
    Only the enmBatchProcessLevel differs between the task flows and therefore I would like to parse this enmBatchProcessLevel as a parameter my generic script.
    To be able to do this, the script needs to know on which task flow a user has clicked. So, I am looking for a function or statement which returns the name (or ID) of the task flow. Based on this name (or ID) a conditional statement can be performed in which a variable is dynamically filled. This variable can then be parsed as a parameter to my generic script.
    For instance:
    Sub GenericRoutine
         Dim strTaskFlow
         Dim intBatchProcessLevel
         '--Get the Task Flow Name
         strTaskFlow = ......<How to return the TaskFlow name or ID?>
         '--Validate the task flow and fill variable intBatchProcessLevel dynamically
         Select Case strTaskFlow
              Case "1.1 Multi Load - Import"
                   intBatchProcessLevel = 2
              Case "2.1 Multi Load - Import Up To Validate"
                   intBatchProcessLevel = 4
         End Select
         '--Execute generic script
         '--Call Batch script and parse intBatchProcessLevel as a parameter:
         Call sBatchProcess(intBatchProcessLevel)
         '--Execute generic script
    End Sub
    Sub sBatchProcess(Byval intBatchProcessLevel)
         Dim lngProcessLevel
         Dim strDelimiter
         Dim blnAutoMapCorrect
         '--Use intBatchProcessLevel to fill lngProcessLevel
         lngProcessLevel = intBatchProcessLevel
         strDelimiter = "_"
         blnAutoMapCorrect = 0
         Set BATCHENG.PcolFiles = BATCHENG.fFileCollectionCreate(CStr(strDelimiter))
         BATCHENG.mFileCollectionProcess BATCHENG.PcolFiles, CLng(lngProcessLevel), , CBool(blnAutoMapCorrect)
    End Sub
    Edited by: user13642656 on Jul 21, 2011 4:55 AM

    Hi, thanks for your reply.
    The Generic script contains 600+ records, which I would like to maintain once, when having multiple Task Flows for Import, UpToValidate, ValidateOnly, UpToExport, ExportOnly etc.
    Is there a central storage in FDQM workbench for script, like a "Module" in Excel VisualBasic environment? Thanks!

  • How to calculate the individual sums of multiple columns in a single query

    Hello,
    Using Oracle 11gR2 on windows 7 client. I have a question on calculating sum() on multiple columns on different columns and store the results in a view. Unfortunately I could not post the problem here as it keeps on giving error "Sorry, this content is not allowed", without telling where or what it is! So I had to post it in the stack-overflow forum, here is the link: http://stackoverflow.com/questions/16529721/how-to-calculate-the-individual-sums-of-multiple-columns-in-a-single-query-ora
    Will appreciate any help or suggestion.
    Thanks

    user13667036 wrote:
    Hello,
    Using Oracle 11gR2 on windows 7 client. I have a question on calculating sum() on multiple columns on different columns and store the results in a view. Unfortunately I could not post the problem here as it keeps on giving error "Sorry, this content is not allowed", without telling where or what it is! So I had to post it in the stack-overflow forum, here is the link: http://stackoverflow.com/questions/16529721/how-to-calculate-the-individual-sums-of-multiple-columns-in-a-single-query-ora
    Will appreciate any help or suggestion.
    ThanksLooks like you want a simple group by.
    select
              yr
         ,      mnth
         ,      region
         ,     sum(handled_package)
         ,     sum(expected_missing_package)
         ,     sum(actual_missing_package)
    from test
    group by
         yr, mnth, region
    order by      
         yr, mnth, region;I wouldn't recommend storing your data for year / month in 2 columns like that unless you have a really good reason. I would store it as a date column and add a check constraint to ensure that the date is always the first of the month, then format it out as you wish to the client.
    CREATE TABLE test
         year_month                              date,
        Region                     VARCHAR2(50),
        CITY                       VARCHAR2(50),             
        Handled_Package            NUMBER,       
        Expected_Missing_Package   NUMBER,   
        Actual_Missing_Package     NUMBER
    alter table test add constraint firs_of_month check (year_month = trunc(year_month, 'mm'));
    ME_XE?Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
      2  Values (to_date('2012-nov-12', 'yyyy-mon-dd'), 'Western', 'San Fransisco', 200, 10, 5);
    Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
    ERROR at line 1:
    ORA-02290: check constraint (TUBBY.FIRS_OF_MONTH) violated
    Elapsed: 00:00:00.03
    ME_XE?Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
      2  Values (to_date('2012-nov-01', 'yyyy-mon-dd'), 'Western', 'San Fransisco', 200, 10, 5);
    1 row created.
    Elapsed: 00:00:00.01
    ME_XE?select
      2        to_char(year_month, 'fmYYYY')    as year
      3     ,  to_char(year_month, 'fmMonth')   as month
      4     ,  Region
      5     ,  CITY
      6     ,  Handled_Package
      7     ,  Expected_Missing_Package
      8     ,  Actual_Missing_Package
      9  from test;
    YEAR         MONTH                REGION                         CITY                    HANDLED_PACKAGE EXPECTED_MISSING_PACKAGE ACTUAL_MISSING_PACKAGE
    2012         November             Western                        San Fransisco                       200                       10                      5
    1 row selected.
    Elapsed: 00:00:00.01
    ME_XE?Then you have nice a nice and easy validation that ensures you data integrity.
    Cheers,

  • How to display the Form output in multiple languages ? ------ Urgent

    How to display the Form output in multiple languages ? -
    Urgent

    Hi,
    Do u mean the Form output in 2-3 languages ...like form output will be having english russian etc...
    for the above case , what u have to do ...
    first have to get the transalations for all the fields
    say for example : if the english word is PO Number , then u have to get the transalation text for PO Number :
    after this , what u have to is ,,, Log on to SAP with russian language ,  then in the form o/p u have to paste that russian test ( translation text ) ...
    do like this for all the components in the form .
    Reward Points if it is Useful.
    Thanks,
    Manjunath MS

  • Inadvertently the typesize on the iPad screen has increased until it is impossible to read the screen.  Can someone tell me how to return the type size to standard please.

    As a newcomer to the iPad, can you please tell me how to return the font size to standard. Somehow I've enlarged the font size until it's unreadable and it applies to the icons as well!  Many thanks in anticipation.

    Try this:
    Settings>General>Accessibility>Zoom>Off

  • How to Park the single invoice for multiple purchase orders

    Hi,
    How to Park the single invoice for multiple purchase orders?

    brother this is possible to enter one Invoice fro multiple purchase order.
    call transaction MIR7, select " purchase order/Scheduling Agreement" from the PO refernce Tab. then click on arrow. give your purchase orders here.
    Thanks

  • Ever since I updated my OS, the screen on my Mac will automatically become huge if I keep my mouse on the screen. I can't figure out how to return the screen to a normal size. Any help?

    Ever since I updated my OS, the screen on my Mac will automatically become huge if I keep my mouse on the screen. I can't figure out how to return the screen to a normal size. Any help?

    Hold down the 'ctrl' key and scroll backwards on the mouse.
    Then go to System Preferences > Universal Access > Seeing. Turn off the Zoom feature.
    Similarly in Sys Pref > Mouse uncheck the box for 'Zoom using scroll wheel…'
    If it still occurs after that there may be a corrupt .plist file.

  • How to clear the node which has multiple attributes

    Hello ,
               how to clear the node which has multiple attributes.
    I've tried this using the method SET_STATIC_ATTRIBUTES_NULL but it's not working.
    Thanks,
    Sandhya.

    Hello,
    I suggest you to create an internal table or structure clear it and the bind it to the context node.
    Another possibility is to try to invalidate the node (method INVALIDATE of interface IF_WD_CONTEXT_NODE).
    Regards.

  • как вернуть текущий ключ how to return the current key

    подскажите как вернуть текущий ключ виндовс.так как диска востановления нет
    tell me how to return the current key vindovs.tak as repairing disk no

    Hi Xolostjak,
    Welcome to the HP Community, I hope you enjoy your experience! To help you get the most out of the HP Forums I would like to direct your attention to the HP Forums Guide First Time Here? Learn How to Post and More.
    It sounds like you are looking to to restore your original version of Windows because the Recovery Disk wasn't working. Is that correct? Since this is an English forum can you please respond back in English.
    I will be happy to help but I need some more information. I will need to know what your operating system is? What is the product number for your notebook?
    Thank you,
    Please click “Accept as Solution ” if you feel my post solved your issue.
    Click the “Kudos Thumbs Up" on the right to say “Thanks” for helping!
    Thank you,
    BHK6
    I work on behalf of HP

Maybe you are looking for

  • Copy Sales Order with reference to a Contract

    Hello, how can I create a copy of a sales order referred to a contract mantaining the reference? I have an order referred to a contract; I would like to create a new order in copy ( va01 - create with Reference) maintaining the relationship to the co

  • Mac OS X 10.5 Leopard and WWDC. When?

    The new mac os system software is coming out during the WWDC taking place from aug. 7 to 15, correct? Does that mean all iMacs and other intel duo macs will be equipped with the new system? Will I be able to buy a new iMac on Monday (Aug. 7) that has

  • Console/Site Server showing old data

    We have a single site with a Management point on a seperate box (sql replica). In my console, I have columns added for the inventory dates and when looking at the dates, my most recent date is yesterday. If I connect to a machine with an old date, co

  • Timeline too long

    So I have my project finished at 6 minutes or so but when I export, I'm getting about double that amount of time loaded into my final movie. It's the "extra space" that comes after my ending... how do I get rid of this so the movie ends when the cont

  • MacBook and an External Drive

    I'd like to backup my entire laptop, I have an external USB drive. I'd like to have a seperate partition for my backup files. How do I partition my external USB drive?