Column names from another table

Hi All,
I have a scenario where i need to get names of a column from another table
for eg,
Table EMP
EmpNo EmpName EmpContact EmpPhone
1 xyz [email protected] 345     
2 abc [email protected] 897
3 ttp [email protected] 345
The column names of this table can be configurable from some other place and its value is stored in another table like
Table Config (2 Columns)
Column_Name Value
EmpName First name
EmpContact Email
EmpPhone Mobile
Now i want to fetch the values from Emp table but with column headers that are changed and have a value in Config table.
If a column name is not there in config table then the original column name should come.
As shown below
EmpNo First name Email Mobile
1 xyz [email protected] 345
2 abc     [email protected] 897
3 ttp [email protected] 345
Another eg, If EmpName is not changed and entered in second table , then i want to have the same name as the original EMP table has as shown below.
EmpNo EmpName Email Mobile
1 xyz [email protected] 345
2 abc     [email protected] 897
3 ttp [email protected] 345
In other words something like this,
select empno,
EmpName as (select value from config where column_name=EmpName),
               EmpContact as (select value from config where column_name=Empcontact),
               EmpPhone as (select value from config where column_name=EmpPhone)
     From EMP
Can some one please help me in providing a solution for this.
Edited by: 941386 on May 30, 2013 6:20 AM

Unfortunately, I think this is a job for dynamic sql ...
Build your "query" first:
(note this won't work "as is", fix the syntax - but you get the idea.)
lv_str := 'select empno,
EmpName as ' || (select value from config where column_name=EmpName) || ',
EmpContact as ' || (select value from config where column_name=Empcontact) || ',
EmpPhone as ' || (select value from config where column_name=EmpPhone) || '
From EMP;';
execute immediate lv_str;Not sure if there's a better way or not.
Only other way I can think of is to leverage the way UNION [ALL] works.
So the following query:
select a, b, c from dual
union all
select d, e, f from dual
/returns data in columns "named" : "a, b, c"
Effectively renaming columns d, e, f. You just need to turn your data on edge in that first query, then throw out the rows (I don't know how to get it to work, but perhaps somebody else does?)
[edit]
another thought is create a view over top of the table, query that view, then drop the view :P
that would work nicely - avoid the dynamic SQL. shrug
[edit]
Edited by: Greg.Spall on May 30, 2013 9:37 AM

Similar Messages

  • How to get only column names from different tables as single table columns

    Hi All,
       I have one requirement in which we want only column names from different tables.
    for example :
     I have three tables T1 ,T2, T3 having
      col1 clo2 clo3 -->  T1 , 
      col3 col5 ,clo6 --> T2 ,
      Clo6 col8 col9 --> T3
    columns i want to get only all  Column names from all table as single Resultset not any data from that how can i get that empty resultset 
    because this empty result i want to bind in datagridview(front end) as Empty resultset 
    Please tell me anyways to do this
    Niraj Sevalkar

    If I understand you want an empty result set, just with metadata. SET FMTONLY do the trick:
    SET FMTONLY ON
    SELECT Col1, Col2, Col3, ....., Coln
    FROM
    T1 CROSS JOIN T2 CROSS JOIN T3
    SET FMTONLY OFF
    Another alternative is to include an imposible contition
    SELECT Col1, Col2, Col3, ....., Coln
    FROM
    T1 CROSS JOIN T2 CROSS JOIN T3
    WHERE 1 = 0
    If you are using a SqlDataAdapter in your client application. You can use the FillSchema method. the select command may be any select statement that returns the columns you want. Under the covers FillSchema will call SET FMTONLY ON.
    If you are using SqlCommand.ExecuteReader you can pass SchemaOnly to CommandBehavior argument. SET FMTONLY ON is called under the covers. Again the select command may be any select statement that returns the columns you want.
    "No darás tropezón ni desatino que no te haga adelantar camino" Bernardo Balbuena

  • Finding a column name from all tables

    i have 85 tables in my user, i forget a column name and its table name. how to find that particular column amongst all tables i have.

    hi
    You can use User_tab_columns or All_tab_columns
    SQL>Select Table_Name,Column_Name From user_tab_columns
    where lower(table_name) like '%emp%' or
    lower(column_name) like '%dept%'
    Khurram Siddiqui
    [email protected]
    Message was edited by:
    pcbyte12

  • Dynamic Column Names from one table and its corresponding values from another table

    I have 2 tables. First tables gives the specification if a column is required or not. we have the 2nd table with the same column name where we provide the actual values.
    I want to select all the required columns from the 1st table and retrieve the values for those from the 2nd table. Both this i want to achieve in a single select statement.

    This wil require a dynamic Query with a Pivot
    DECLARE @ColsPivot as VARCHAR(MAX);
    DECLARE @Query  AS VARCHAR(MAX);
    1. Retreive the ID for all required field
    SET @ColsPivot = (SELECT STUFF((SELECT  ',' + quotename(CAST([RequirementID] as varchar(3))) FROM [dbo].[Requirement] WHERE required=1 FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,''));
    This will give you : [1],[2],[3],[8],[9],[14] for exemple.
    2. Build your Query
    SET @Query ='SELECT ClientID,'+@ColsPivot+''
        FROM (
            SELECT [ClientID],[RequirementID],[Value]
            FROM dbo.RequirementValue
            WHERE ClientID=@CliendID --Optional SP parameter
        )src
              PIVOT(
                MAX(Value)
                for [RequirementID] in ('+@ColsPivot+')
        ) p';
    3. Exec(@Query);

  • Same column name from different table

    i have a sql query as like this : "SELECT * FROM TABLE1,TABLE2". i use oracle. both TABLE1 and TABLE2 have the same column named 'COLUMN1'. while i get rows how i know the value of COLUMN1 from which table (TABLE1 or TABLE2).
    sample code snippet is above. do u help me!
    while (rs.next())
    value1 = rs.getString("COLUMN1");
    // is value1's value from table1 or table2. how do i know this?
    // i try value1 = rs.getString("TABLE1.COLUMN1"); but it doesn't work :(
    ....

    I case you don't know what an alias is, it would look something like this:
    SELECT a.COLUMN1 as FirstColumn1, b.COLUMN1 as SecondColumn1 FROM FirstTable a, SecondTable b
    Notice that in the FROM clause we've appended a short name for each table. You're not limited to one character, but I try to keep it simple. Now we can refer to the tables as a and b.
    Because I did that I have to refer to any ambiguous columns (although it's good practice to refer to ALL columns) using the table name prefix and a period. This tells the driver which "COLUMN1" I want. Then we include as AS clause which allows us to tell the driver what we want that column name to be when it's returned to us. This is specially usefule when I have two columns in two separate tables with the same name (as you have here) or if I'm calculating data (i.e. (a.QTY * b.PRICE) as UnitPrice) that doesn't have a column name, so here I can give it one.
    It's a little weird at first since you use the alias names in the select before you actually define them in the FROM clause, but you'll get use to it.
    Now you retrieve FirstColumn1 and SecondColumn1 from your ResultSet, not Column1.
    HTH.

  • Finding the column names from a table.

    I am on 10g
    I would like to find out the columns of a table where there are null columns in a table.....this table contains about 300 + columns where i do not want to put where condition for all the columns
    is there a way i can write a sql to find?
    for a given table or the results set that i need to get, i will have same results for all the rows, so it
    cant be like col1 is null on row1, but col2 is not null on row2 ...they are all identical....
    example table, but it has 300 + cols
    F_IND     H_IND     P_IND     DMA_IND
    N     N          
    N     N          
    N     N          
    N     N          Thanks

    select count(col1), count(col2), count(col3), ...
    from your table;
    The results with 0s are null throughout the table (or the table has no rows).

  • JTable not seeing Column names from Abstract table model

    Here is the code for the getColumnNames method
    It is getting called and it is send the correct data However the Headings do not match the data.
    public String getColumnName(int col){
    String s_retval;
    switch (m_iMode){
    case PhoneTableModel.NORMALVIEW:
    default:
    s_retval= m_sHeadingsDefault[col];
    break;
    System.err.println("TableNames "+s_retval);
    return s_retval;
    }

    Why do you show the part of the code that works?!
    Show how you create and display your table!
    What is displayed in the headings then?
    Which JDK on which platform?

  • Looping delete statement where table name is coming from another table

    Hi All,
    We have to write code to delete records from active tables of WDSO.We have 5 DSO  so inspite of writing delete statement for each table we want to put all table names into one table and then loop through it .but we are getting error when we are refering that table field which has active table name.error is :
    "dictionary structure or table is either not active or does not exist "
    As per my understanding in a delete /select /insert /update statement we need to put table name (whose field we are refering ) it can't be replaced by a variable .
    ex: v_table = 'EMPLOYEE' .
    DELETE FROMv_table WHERE EMP_NAME = 'ABDC' .
    is wrong and it must be like
    ex : DELETE FROM EMPLOYEE WHERE EMP_NAME = 'ABDC' .
    but we want to make our code dynamic .
    Can you please suggest a way so that we can read the table names from another table and delete data based on some selection fom those tables .
    I tried variants ,perform etc and even searched FM for the same but not found a solution .Your help will be greatly appreciated .
    Thanks in advance .
    Regards,
    Jaya

    Hi,
    You can change your statement as follows:
    DELETE FROM (v_table) WHERE EMP_NAME = 'ABDC' .
    However, I would not recommend this. There is a standard function module RSAN_ODS_DATA_DELETE which allows selective deletion - that should be a safer way to do this. You can

  • Result of an SQL query as a Column name of another query

    Hi Friends,
    Can we use a result of a SQL Query as a column name of another table to retrieve data. If so please help me.
    For eg :
    I have a table where is store numbers;
    select col1 from table1 where col1='5';
    and i have another table where .. this value of col is a column name..
    select ( select col1 from table1 where col1='5') from table2;
    Thanks in advance.

    Hi,
    ORAFLEX wrote:
    Hi Friends,
    Can we use a result of a SQL Query as a column name of another table to retrieve data. If so please help me.
    For eg :
    I have a table where is store numbers;
    select col1 from table1 where col1='5';
    and i have another table where .. this value of col is a column name..
    select ( select col1 from table1 where col1='5') from table2;
    Thanks in advance.Do you really mean that?
    select col1 from table1 where col1='5';That query will always return either '5' or nothing. Whatever you're trying to accomplish with that, you can do with an EXISTS query.
    Perhaps you meant to reference two different columns in that query:
    select col1 from table1 where col2='5';In that case, sorry, no, you can't do that without resorting to dynamic SQL.
    If the same column is used throughout the query (but could change every time you run the query), then the dynamic SQL might be pretty easy. In SQL*Plus, for example, you could use substitution variables, defined in another query at run-time.
    If there are only a few possible values that the sub-query could possibly return, and you know what they all are, then you can fake a dynamic query like this:
    SELECT     CASE     ( SELECT  col1
                FROM       table1
                WHERE       col2     = '5'
              WHEN  'BONUS'     THEN  bonus
              WHEN  'COMM'     THEN  comm
              WHEN  'SAL'     THEN  sal
         END     AS col1
    FROM     table2
    ;Sorry to give such a vague answer, but it's the best I can do with the information I have.
    It would help if you posted a little sample data (CREATE TABLE and INSERT statments for both tables), and the results you want to get from that data. If you want to pass a parameter to the query, give the results you want for a couple of different parameters.

  • SQL select a concatination of entries from another table

    I am trying to select a few columns from one table and then add another column that is a concatination or names from another table that referances it as below. Is this possible to do in one select statement?
    table x1
    ID NAME
    1 Bob
    1 Tom
    2 Bill
    table x2
    ID NAMES
    1 Bob, Tom
    2 Bill
    3

    You mean string aggregation?
    If yes, then it is possible like this, if you are on 10g.
    SQL> create table x1 (id,name)
      2  as
      3  select 1, 'Bob' from dual union all
      4  select 1, 'Tom' from dual union all
      5  select 2, 'Bill' from dual
      6  /
    Tabel is aangemaakt.
    SQL> select id
      2       , rtrim(n,', ') names
      3    from ( select id
      4                , n
      5                , rn
      6             from x1
      7            model
      8                  partition by (id)
      9                  dimension by (row_number() over (partition by id order by null) rn)
    10                  measures (cast(name as varchar2(20)) n)
    11                  rules
    12                  ( n[any] order by rn desc = n[cv()] || ', ' || n[cv()+1]
    13                  )
    14         )
    15   where rn = 1
    16  /
            ID NAMES
             1 Bob, Tom
             2 Bill
    2 rijen zijn geselecteerd.There are several other (slower) techniques possible as well. You can find them by using the search function on this forum.
    Regards,
    Rob.

  • How to know primary key column name form a table name in sql query

    Suppose I only know the table name. How to get its primary key column name from the table name?
    Thanks

    Views don't have primary keys though their underlying tables might. You'd need to pick apart the view to determine where it's columns are coming from.
    You can select the text of the view in question from user_views.

  • How to rename a column name while copying it from another table

    Hi All,
    I am copying rows between two table TableA to TableB
    but I wanted to insert data in he column name (username) in tableA to a column named (NewUser) in tableB
    How do I go about that?
    insert into TableB
    select Username
    from TableA
    Thanks

    If you want to only move username, then just specify the column name for TableB, that is
    insert into TableB(NewUser)
    select Username
    from TableA
    If you want to move all (or some) of the columns while putting Username in a column named NewUser, just list the columns in both the insert into part and the select part, for example
    insert into TableB(NewUser, ColA, ColB, ColC)
    select Username, ColA, ColB, ColC
    from TableA
    Tom

  • How to read the column name of a table from sap system using C#?

    Hi!!
    I am using SAP .NET connector and creating a windows application.
    Now I wanna read the column name when a table name is given....
    Connection is done, but I don't know the code to read the column names alone...
    Can anyone help me with the code??

    fine!!
    So if i give the table name, which the RFC_READ_TABLE function module have, will it run properly? or i wanna change all the codes in order to support RFC_READ_TABLE function module?
    Because from the beginning I was using BAPI_CUSTOMER_GETLIST function, but my client requirement is to use ERP function module RFC_READ_TABLE, he didn't give any table name also..
    This is my code: What I have to change in this???
    ECCDestinationConfig ECCDestination = new ECCDestinationConfig();
                RfcDestinationManager.RegisterDestinationConfiguration(ECCDestination);
                RfcDestination rfcDest = null;
                rfcDest = RfcDestinationManager.GetDestination(a);
                    RfcRepository repo = rfcDest.Repository;
                    IRfcFunction customerList = repo.CreateFunction("BAPI_CUSTOMER_GETLIST");
                    IRfcTable addressData = customerList.GetTable("AddressTable"));
                    int j = addressData.Metadata.LineType.FieldCount;
                    for (int i = 0; i < j; i++)
                        RfcElementMetadata metadata = addressData.GetElementMetadata(i);
                        listallcolumn.Items.Add(metadata.Name);
    Message was edited by: Jeswin Rebil

  • Update SAME column name in two tables from ONE query

    Dear All Seniors
    Please tell me is it possible to update a same column name in two tables.
    I have two tables in same schema
    (1)table name
    pem.igp_parent
    column name
    igp_no.
    igp_type
    (2)table name
    pem.igp_child
    column name
    igp_no.
    igp_type
    i want to update igp_no column in one query please tell me how it would be possible.
    thanks
    yassen

    You want to update the data from what to what? Where is the new data coming from?
    If you are trying to put the same data in two different tables, that strongly implies that you have a normalization problem that needs to be addressed.
    Why do you want a single query rather than updating each table in turn? Can you join the two target tables to produce a key-preserved view?
    Justin

  • Create a table which has columns from another table in other schema

    Hi All
    I need to create a table which is table driven i.e., the columns of that table need to be inserted from another table in a different schema...Can any one help me with this??

    create table newtab (--column spec here--) as
    Select --columns here-- from otherschema.tablename;max

Maybe you are looking for

  • Error syncing photos on Vista

    I am having this problem: "dlrgc1_w01.jpg" was not copied to the iPod "iPod Touch" because it cannot be played on this iPod. My computer is Windows Vista and I purchased this 16GB iPod Touch on Sept. 27th. That afternoon I was able to sync the exact

  • Personal edition vs standard edition

    I am new to oracle and am planning on installing oracle (personal or standard) onto a standalone desktop in my cube for test and develop(and learn) on. My company is currently using Enterprise on sperate database servers that eventually I will probab

  • Write Back to Database

    Hi, Is there any component available in market which I can use to write back to database? I looked into web service option but there is not clear (Step by step) instruction on how to use it. If some body used any workaround or component please let me

  • 3 Displays to X220

    Hi , someone tell me please, is it possible to connect three displays to X220 with dock-station? Now I have two attached displays.

  • How to use Connection Pool on WLS 10.0 MP2?

    Hello there! I have a WS and i use ibatis to talk iwth a Oracle DB, but i have a requirement to use connection pool by wls and i dont where should i do this to my application. I just have this method to the connection private static void initDb() thr