How to check all columns existing in a table?

Hello.
I'm writing a function that returns average value of every numeric column existing in a database.
I have a general idea how to do it, but I need just one more thing, look at the expamle:
select table_name from user_tables where rownum=1
this allows me to select names of all tables, and then I can access them 1 by 1...
But now i have a little problem - how to access all the atributes in selected table in this way?
Oh, and I have a problem with this rownum... For example it works for 1 (shows 1st row - name of first table) but does not for 2, or greater number :(.
And expresions like rownum < 5 work, but rownum > 3 not :/. What may be the reason?
Thank you for help!
Regards.

user13483761 wrote:
Thanks a lot! This 3 atributes is all I need, its a way lot easier than I thought before. I simply use cursor loop and your advices.
Almost got working code - but there is 1 error telling that select is incorrect :/. Why is that?
create or replace
FUNCTION SHOW_ALL
RETURN VARCHAR2 IS
v_ret VARCHAR(100):='Null';
BEGIN
DECLARE
CURSOR cur_stats IS SELECT table_name, column_name, data_type
FROM user_tab_columns;
v_zdanie VARCHAR(100);
BEGIN
FOR v_cur IN cur_stats LOOP
IF v_cur.data_type = 'NUMBER'
THEN
strike
--> select 'In table ' || v_cur.table_name || ' average value of ' ||--
--> v_cur.column_name || ' is:' || avg(v_cur.column_name)--
--> into v_zdanie--
--> from v_cur.table_name;--
strike
>
dbms_output.put_line(v_zdanie);
END IF;
END LOOP;
RETURN v_ret;
END;
END SHOW_ALL;
select 'In table ' || v_cur.table_name || ' average value of ' ||
v_cur.column_name || ' is:' || avg(v_cur.column_name)
into v_zdanie
from v_cur.table_name;
sql_developer tells that this 'table or perspective does not exist' or sth like that, I translated from my native language.You are missing lot of things in the function, you are hard coding the return value to null?
What you should do is as below:
remove the part what you have written, i have strike that above. declare a variable your_select_statement VARCHAR2(1000);
your_select_statement := 'select nvl(avg('||v_cur.column_name||'),0) from '||v_cur.table_name;
execute immediate your_select_statement into v_zdanie;
dbms_output.put_line('In table ' || v_cur.table_name || ' average value of ' ||v_cur.column_name || ' is:' ||v_zdanie);

Similar Messages

  • How to Determine Whether Columns Exist in a Table

    Greetings,
    Because of customizations to our product, certain database table structures vary between different client installations. Hence, in the code, I need to determine whether two columns exist in a certain table, in order to apply some business logic.
    Please suggest a way for me to determine whether two named columns appear in a specific table. In other words, for example, I need to discover whether both columns SAL_GRADE and BONUS appear in the EMP table, or not. Only if both columns are defined, can I apply my business logic.
    Thanks,
    Avi.

    Avi,
    Like this ?
    SQL> declare
      2    l_count number;
      3  begin
      4    select count(*)
      5      into l_count
      6      from user_tab_columns
      7     where table_name = 'EMP'
      8       and column_name in ('SALGRADE','BONUS')
      9    ;
    10    if l_count = 2
    11    then
    12      dbms_output.put_line('Both columns exist, so apply business logic');
    13    else
    14      dbms_output.put_line('One or both columns are missing, so don''t apply business logic');
    15    end if;
    16  end;
    17  /
    One or both columns are missing, so don't apply business logic
    PL/SQL-procedure is geslaagd.Regards,
    Rob.

  • How to check  which column data differs from master table and archive table

    Hi All,
    i have two tables, table a (a1 number,a2 varchar2,a3 varchar2) and table b (b1 number,b2 varchar2,b3 varchar2).
    how to check the data in both the table are same( including all columns).
    data in a.a1 is same as b.b1 and a.a2 is same as b.b2 like that.
    if they not same , i need to know which field differs.
    Kindly Share ur ideas.

    887268 wrote:
    thanks Sven W. ,
    above reply clearly shows what my question is.
    one column must be primary key, based on that key i need to find out which are the fields having different data..
    im strugling with this, i tried the following already, but not able to get.
    select the columns from a MINUS select the columns from b.
    -- from this i can find whether the difference occurred or not.
    but i cant able to get which are the fields value changed.Good. Then you would match the rows using the PK column and need to compare the columns
    Instead of a MINUS + UNION ALL + MINUS we can now use a FULL OUTER JOIN
    It is a little task to write out all column names, but 40 columns can be handled.
    This statement would show you both tables with matching rows on the same line.
    select a.*, b.*
    from a
    FULL OUTER JOIN b on a.id = b.idNow filter/check for mismatches
    select case when a.col1 != b.col1 then 'COL1 value changed'
                    when a.col2 != b.col2 then 'COL2 value changed'
                    when a.col3 != b.col3 then 'COL3 value changed'
             end as compare_result
            ,a.*, b.*
    from a
    FULL OUTER JOIN b on a.id = b.id
    /* return only non matching columns */
    where (a.col1,a.col2,a.col3) != (b.col1,b.col2,b.col3) You might need to add nvls to take care of null values. Test this!
    Another way could be to group upon the primary key
    select *
    from (
      select id 
               ,count(distinct col1)-1 cnt_col1
               ,count(distinct col2)-1 cnt_col2
               ,count(distinct col3)-1 cnt_col3
       from
         select 'A' source, a.*
         from a
         UNION ALL
         select 'B' source, b.*
         from b)
       group by ID
    /* only records with differences */
    where 1 in (cnt_col1, cnt_col2, cnt_col3)
    ;The count columns will hold either 1 or 0. If it is 1 then this column has a difference.

  • How to print all columns in one page

    Hi,
    Can anybody explain me how to print all columns in one page.we have around 15 to 20 columns for 4 reports and all these reports are build on one multiprovider.we are using BW 3.5.
    Can anyone explain me  how to print ALL COLUMNS IN ONE PAGE  .currently they are getting all columns in 2 to 3 pages. They are using PORTAL to run the reports here.
    Is it possible to do by customizing Webtemplate or by macros in Workbook.Please help me
    Edited by: kotha123 on Oct 11, 2010 5:58 PM

    Hi,
    Your best bet is to use a workbook template or else Excel to pdf option...Thanks

  • How to bring all columns in a single/first page in report painter

    Hi Experts,
    There are 9 columns in a report painter which includes 1 lead column.
    7 columns comes in a first page including lead column and rest 2 columns goes to second page. This report is copied from another report where all columns comes in a first page only. if the original report comes in one page then why not the new report.
    Please suggest how to bring all columns in a single/first page in the new report.
    Please revert if I'm confused anywhere.
    Thanks in advance
    Arabinda

    Hi Rajneesh,
    Thanx for the quick reply. When I change the column width to 10 it comes in one page. But the requirement is to keep the column width to at least 12 or 14 and moreever the original report from where this report is copied having the same no of columns and the column width for original report is 14 and it comes in one page only.
    Do I need to do something more to achieve this. My question is how the original reports data comes in one page where as the current report having issue with same number of columns.
    Pls suggest.
    Thanx,
    Arabinda

  • How to test if a column exists in a table

    Hello,
    Using SQL or PL/SQL how do I test if a column exists in a table. For example,
    if column_aa exists in table_1
    then do this....
    else do that....
    Thank you.

    Set ServerOutput On;
    CREATE Table a_Test (
    Col1 NUMBER
    , Col2 NUMBER )
    DECLARE
    lnCol1 NUMBER ;
    lnCol7 NUMBER ;
    PROCEDURE TestColumn( acColName IN VARCHAR2 )
    IS
    lnColX NUMBER ;
    NoColumnExists EXCEPTION;
    PRAGMA EXCEPTION_INIT(NoColumnExists, -904);
    BEGIN
    EXECUTE IMMEDIATE
    'SELECT Max('|| acColName||') FROM a_Test WHERE RowNum = 0'
    INTO lnColX ;
    DBMS_OUTPUT.Put_Line('Column: '||acColName||' exists.');
    EXCEPTION
    WHEN NoColumnExists THEN
    DBMS_OUTPUT.Put_Line('Column: '||acColName||' NOT exists.');
    WHEN Others THEN
    DBMS_OUTPUT.Put_Line( SqlCode ||' '||SqlErrM );
    END
    BEGIN
    DBMS_OUTPUT.Enable ;
    TestColumn( 'Col1') ;
    TestColumn( 'Col2') ;
    TestColumn( 'Col7') ;
    END
    DROP Table a_Test;

  • How to remove all columns and cells in numbers

    how to remove all columns and cells in numbers

    Click on the Table's icon in the Sheets list. Press delete.
    Done.
    Regards,
    Barry

  • How to check all songs in iTunes

    Until now when I double click a song in iTunes, it plays the song and continues to play all the songs that follow. Now I play one song and it stops and does not play the following songs.  How to check all songs in iTunes? so the music will continue

    52,
    Try command clicking a song and it should select all in songs or a playlist.

  • How to check any directory existance in TestStand?

    How to check any directory existance in TestStand?
    Is there TestStand function? Or other option without writing my own code in external program (e.g. LabVIEW or C)?
    Thanks in advance
    Solved!
    Go to Solution.

    At Least according to the NI TestStand 2012 documentation, Engine.FindPath and Engine.FindFile work only within the NI TestStand SearchDirectories.
    Maybe related: remote paths (\\server\path\to\file.ext) are considered "invalid" by Engine.FindPath.

  • How to return all columns starting with E?

    How to return all columns starting with E in emp table?
    My attempts (below) were unsuccessful.
    SQL> SELECT E* from emp;
    SELECT E* from emp
    ERROR at line 1:
    ORA-00936: missing expression
    SQL> SELECT E.* FROM EMP;
    SELECT E.* FROM EMP
    ERROR at line 1:
    ORA-00904: "E": invalid identifier
    SQL> SELECT EMP.E* FROM EMP;
    SELECT EMP.E* FROM EMP
    ERROR at line 1:
    ORA-00936: missing expression

    You need to do it dynamically:
    SQL>  var cur refcursor
    SQL>  declare
      co varchar2(40);
    begin
      for c in (select column_name from cols where table_name = 'EMP' and column_name like 'E%') loop
        co := co || c.column_name || ',';
      end loop;
      open :cur for 'select '|| rtrim(co,',') || ' from emp';
    end;
    PL/SQL procedure successfully completed.
    SQL>  print cur
         EMPNO ENAME    
          7369 SMITH    
          7499 ALLEN    
          7521 WARD     
          7566 JONES    
          7654 MARTIN   
          7698 BLAKE    
          7782 CLARK    
          7788 SCOTT    
          7839 KING     
          7844 TURNER   
          7876 ADAMS    
          7900 JAMES    
          7902 FORD     
          7934 MILLER   
    14 rows selected.

  • How to rename a column name in a table? Thanks first!

    I tried to drop a column age from table student by writing the
    following in the sql plus environment as :
    SQL> alter table student drop column age ;
    but I found the following error
    ORA-00905: &#32570;&#23569;&#20851;&#38190;&#23383; (Lack of Key word)
    I have oracle enterprise edition 8.0.5 installed at windows 2000
    thank you
    And I want to know how to rename a column name in a table?
    thanks

    In Oracle 8i, your syntax would have worked.  However, if I
    recall correctly, in Oracle 8.0, you can't rename or drop a
    column directly.  One way to get around that problem is to
    create another table based on a select statement from your
    original table, providing the new column name as an alias if you
    want to change the column name, or omitting that column from the
    select statement if you just want to drop it.  Then drop the
    original table.  Then re-create the original table based on a
    select statement from the other table.  Then you can drop the
    other table.  Here is an example:
    CREATE TABLE temporary_table_name
    AS
    SELECT age AS new_column_name,
           other_columns
    FROM   student
    DROP TABLE student
    CREATE TABLE student
    AS
    SELECT *
    FROM   temporary_table_name
    DROP TABLE temporary_table_name
    Something that you need to consider before doing this is
    dependencies.  You need to make a list of all your dependecies
    before you do this, so that you can re-create them afterwards. 
    If there are a lot of them, it might be worthwhile to do
    something else, like creating a view with an alias for the
    column or just providing an alias in a select.  It depends on
    what you need the different column name for.

  • How do I restore column headings in Response table after accidently deleting them? Undo doesn't work.

    How do I restore column headings in Response table after accidently deleting them? Undo doesn't work (only seems to go back one level). I also have so many headings I won't be able to type them in manually!

    With the help of information from user, ptressel, in [https://support.mozilla.org/en-US/questions/1032154#answer-673322 a post here about the existence of sessionstore.js when version 33 was released], I was able to easily recover my tabs and tab groups as follows:
    # close Firefox and, perhaps, allow a few seconds (30s?) for any final closing of files;
    # check to see if you have a sessionstore.js file in your profile folder, named like the one I documented in my original post above;
    # if it is not timestamped prior to the discovery of your problem, open the sessionstore-backups folder;
    # check if the recovery.js file is suitably timestamped and, if not, the recovery.bak.
    # At this point, you are likely to find that none of them are prior to your problem occuring. If so, open your backups of this folder and go through steps 2-4 to find a file prior to your problem occuring.
    # When you find a file, copy it to the root of your current profile folder and name it, "sessionstore.js"
    # Open Firefox. Mine opened up as desired.
    This is a Windows solution. Sorry I can't comment on other platforms, but I'd bet that as this is just a file copy and renaming, it is likely the same.
    For Windows users, you may find you need to sign out and login as an administrator in order to access the backups. You need not logoff your standard account, but do have Firefox closed as described above.
    Hope that helps.

  • How to get the column names of the table into the Dashboard prompt

    how to get the column names of the table into the Dashboard prompt
    Thanks & Regards
    Kishore P

    Hey john,
    My requirement is as follows
    I have created a Rank for Total sales by Region wise i.e RANK(SUM(Dollars By Region)) in a pivot table.
    My pivot table looks like this
    COLUMN SELECTOR: TOTAL US , REGION , DISTRICT , MARKET
    ---------------------------------------------------- JAN 2009          FEB 2009        MAR 2009
    RANK              REGION                  DOLLARS           DOLLARS        DOLLARS DOLLARS
    1 CENTRAL 10 20 30 40
    2 SOUTHERN 10 30 30 70
    3 EASTERN 20 20 20 60
    4 WESTERN 10 20 30 40
    When i select the District in column selector
    Report has to display rank based on Total Sales by District. i.e
    ------------------------------------------------- JAN 2009         FEB 2009       MAR 2009
    RANK             DISTRICT              DOLLARS           DOLLARS        DOLLARS DOLLARS
    for this i need to change the fx of rank i.e RANK(SUM(Dollars By Region)) to RANK(SUM(Dollars By District)) and fx of Region i.e Markets.Region to Markets.District dynamically.
    so , i need to capture column name of the value selected from the column selector and dynamically i need to update the fx 0f RANK & fx of region.
    do you have any solution for this?
    http://rapidshare.com/files/402337112/Presentation1.jpg.html
    Thanks & Regards
    Edited by: Kishore P on Jun 24, 2010 7:24 PM
    Edited by: Kishore P on Jun 24, 2010 7:28 PM

  • How to check the records in Master Data Table?

    Hi,
       I am trying to load the Master Data Table using the Flat File.Now how to check the records in Master Data Table?
    I done the following way:
    Info Provider->Info Object->Right Click->Display Data or Maintain Master Data
    But it's not showing the records.It's asking like CID from......To......
                                                                        CID(SID)from.............To.......
                                                                         here CID means customer id(characteristic).
    and showing some settings.
    Please guide me.
    Thanks & Regards

    Hi Sri,
    Go to T- code RSD1 and type your info object name and open the P- table in the infoobject then select execute symbol to see the updated  data in to master data info object.
    regards
    sap

  • How to insert select columns from one internal table to another

    Hi,
    How to insert select columns from one internal table to another based on condition as we do from a standart table to internal table.
    regards,
    Sriram

    Hi,
    If your question is for copying data from 1 int table to other ;
    we can use
    APPEND LINES OF it_1 TO it_2.
    or if they have different columns then:
    loop at it_1 into wa_it1.
    move wa_it1-data to wa_it2-d1.
    apped wa_it2 to it_2.
    clear wa_it2.
    endloop.
    thnxz

Maybe you are looking for