List columns used by query

Hi,
Does anyone know of a way to display all the tables and columns in a database that a particular query uses? I have a long query that will be run on a different database in the future, and we want to make sure the tables and columns we need are copied there regularly. I've combed through it by hand, but would like to verify I haven't missed anything.
I am using 10g.
Thanks!

example use of dbms_sql...
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2) IS
  v_v_val     VARCHAR2(4000);
  v_n_val     NUMBER;
  v_d_val     DATE;
  v_ret       NUMBER;
  c           NUMBER;
  d           NUMBER;
  col_cnt     INTEGER;
  f           BOOLEAN;
  rec_tab     DBMS_SQL.DESC_TAB;
  col_num     NUMBER;
  v_rowcount  NUMBER := 0;
BEGIN
  -- create a cursor
  c := DBMS_SQL.OPEN_CURSOR;
  -- parse the SQL statement into the cursor
  DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
  -- execute the cursor
  d := DBMS_SQL.EXECUTE(c);
  -- Describe the columns returned by the SQL statement
  DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
  -- Bind local return variables to the various columns based on their types
  FOR j in 1..col_cnt
  LOOP
    CASE rec_tab(j).col_type
      WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000); -- Varchar2
      WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);      -- Number
      WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);     -- Date
    ELSE
      DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);  -- Any other type return as varchar2
    END CASE;
  END LOOP;
  -- Display what columns are being returned...
  DBMS_OUTPUT.PUT_LINE('-- Columns --');
  FOR j in 1..col_cnt
  LOOP
    DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' - '||case rec_tab(j).col_type when 1 then 'VARCHAR2'
                                                                              when 2 then 'NUMBER'
                                                                              when 12 then 'DATE'
                                                     else 'Other' end);
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('-------------');
  -- This part outputs the DATA
  LOOP
    -- Fetch a row of data through the cursor
    v_ret := DBMS_SQL.FETCH_ROWS(c);
    -- Exit when no more rows
    EXIT WHEN v_ret = 0;
    v_rowcount := v_rowcount + 1;
    DBMS_OUTPUT.PUT_LINE('Row: '||v_rowcount);
    DBMS_OUTPUT.PUT_LINE('--------------');
    -- Fetch the value of each column from the row
    FOR j in 1..col_cnt
    LOOP
      -- Fetch each column into the correct data type based on the description of the column
      CASE rec_tab(j).col_type
        WHEN 1  THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
                     DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_v_val);
        WHEN 2  THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
                     DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_n_val);
        WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
                     DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'));
      ELSE
        DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
        DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_v_val);
      END CASE;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('--------------');
  END LOOP;
  -- Close the cursor now we have finished with it
  DBMS_SQL.CLOSE_CURSOR(c);
END;
SQL> exec run_query('select empno, ename, deptno, sal from emp where deptno = 10');
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
DEPTNO - NUMBER
SAL - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
DEPTNO : 10
SAL : 2450
Row: 2
EMPNO : 7839
ENAME : KING
DEPTNO : 10
SAL : 5000
Row: 3
EMPNO : 7934
ENAME : MILLER
DEPTNO : 10
SAL : 1300
PL/SQL procedure successfully completed.
SQL> exec run_query('select * from emp where deptno = 10');
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
JOB - VARCHAR2
MGR - NUMBER
HIREDATE - DATE
SAL - NUMBER
COMM - NUMBER
DEPTNO - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
JOB : MANAGER
MGR : 7839
HIREDATE : 09/06/1981 00:00:00
SAL : 2450
COMM :
DEPTNO : 10
Row: 2
EMPNO : 7839
ENAME : KING
JOB : PRESIDENT
MGR :
HIREDATE : 17/11/1981 00:00:00
SAL : 5000
COMM :
DEPTNO : 10
Row: 3
EMPNO : 7934
ENAME : MILLER
JOB : CLERK
MGR : 7782
HIREDATE : 23/01/1982 00:00:00
SAL : 1300
COMM :
DEPTNO : 10
PL/SQL procedure successfully completed.
SQL> exec run_query('select * from dept where deptno = 10');
-- Columns --
DEPTNO - NUMBER
DNAME - VARCHAR2
LOC - VARCHAR2
Row: 1
DEPTNO : 10
DNAME : ACCOUNTING
LOC : NEW YORK
PL/SQL procedure successfully completed.
SQL>

Similar Messages

  • Converting rows to columns using dynamic query.

    I am trying to use the below code that I founnd on the web to conver rows to columns. the reason that I want to use dynamic query is that the number of rows are not know and changes.
    declare
        lv_sql varchar2(32767) := null ;
    begin
        lv_sql := 'SELECT Iplineno ';
        for lv_rec in (SELECT distinct vendor from bidtabs  where letting = '10021200' and call ='021')
        loop
            lv_sql :=   lv_sql
                        || CHR(10)
                        || ', MAX( DECODE( vendor, '
                        || chr(39)
                        || lv_rec.vendor
                        || CHR(39)
                        || ', bidprice, NULL ) ) as "'
                        || lv_rec.vendor
                        || '" ' ;
        end loop;
        lv_sql :=   lv_sql
                    || CHR(10)
                    || 'FROM bidtabs  where letting =  ''10021200''  and call =  ''021''  and lineflag = ''L''  '
                    || CHR(10)
                    || 'GROUP BY iplineno ;' ;
    here is the result
    BIDPRICE     CALL     IPLINENO     LETTING     VENDOR
    9,585     021     0010     10021200     C0104        
    1,000     021     0020     10021200     C0104        
    1,000     021     0030     10021200     C0104        
    17     021     0040     10021200     C0104        
    5     021     0050     10021200     C0104        
    11,420     021     0010     10021200     K0054        
    1,100     021     0020     10021200     K0054        
    1,100     021     0030     10021200     K0054        
    5     021     0040     10021200     K0054        
    3     021     0050     10021200     K0054        
    8,010     021     0010     10021200     V070         
    900     021     0020     10021200     V070         
    1,320     021     0030     10021200     V070         
    11     021     0040     10021200     V070         
    3     021     0050     10021200     V070         
    and here is the desired output
    CALL     IPLINENO     LETTING      C0104              K0054              V070         
    021     0010     10021200      9,585                     11,420                                   8,010
    021     0020     10021200      1,000       1,100     900
    021     0030     10021200      1,000     1,100     1,320
    021     0040     10021200       17     5     11
    021     0050     10021200      5     3     3

    Here is the error message I am getting:
    RA-06550: line 22, column 43:
    PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
    begin case declare end exception exit for goto if loop mod
    null pragma raise return select update while with
    <an identifier> <a double-quoted delimited-identifier>
    <a bind variable> << close current delete fetch lock insert
    open rollback savepoint set sql execute commit forall merge
    pipe

  • Oracle Forms List Items :not populating list Dynamically using select Query

    Hi ,
    I need to create a list item. And the values to be populated in the list are from Database but I'm unable to populate from database.
    Please help me out how to populate the values from database.
    In Property Pallete we have Elements in List under Functional section , In this we have to manually enter the List values but we can't use any Query to Dynamically populate.
    Thanks,
    Kiran Sanga

    When you need to fill a list with the result of a query, you'll have to do it at runtime.
    Andreas has a good blogpost about this.

  • Filter SharePoint list items using CAML query as same as Like operator in SQL Server.

    Hi ,
    I have filtered SharePoint list items based on Name using CAML query <Contains> . Now I have a new requirement is to filter list items using Like operator in SQL. But Like operator is not in CAML.
    How do I filter list items using CAML as same as Like operator in SQL.
    Please let me know.
    Thanks in Advance.

    Did you try using <Contains>?
    http://social.technet.microsoft.com/Forums/sharepoint/en-US/15766fd5-50d5-4884-82a1-29a1d5e38610/caml-query-like-operator?forum=sharepointdevelopmentlegacy
    --Cheers

  • Color coded calculated list column in content query web part

    I have a calculated column in a list for displayig color coded values using the link
    http://blog.splibrarian.com/2012/06/06/using-calculated-columns-to-add-color-coding-to-your-sharepoint-lists/
    Now for rollup i am using the lists to display them in a content query web part so i have updated the .webpart file and imported and added, made changes in the item style and uploaded ad finally inserted the web part in the page.
    But while diplaying hte custom column, it is being eing displayed as
    string;#<DIV style='font-weight:bold; font-size:24px; color:green;'>•</DIV>
    As you can notice it has appended string;# before the text. How do i avoid this? Thanks

    <xsl:value-of-select="@RAG" disable-output-escaping=“yes”
    />

  • Sql query parsing (retrieve tables and columns used in query)

    Hi,
    1. Is there any view in Oracle which can tell me, which tables and columns were used in the last ran query, no matter how complex the query is.
    2. Secondly I can get table names, used in stored procedures and views, using USER_DEPENDENCIES VIEW, however this System View does not return column names, used in that procedure / view, any idea please .
    Thanks and Regards,
    Luqman

    luqman123 wrote:
    Hi,
    1. Is there any view in Oracle which can tell me, which tables and columns were used in the last ran query, no matter how complex the query is.I don't believe so. You can look at V$SQL and retrieve the actual SQL text from there.
    2. Secondly I can get table names, used in stored procedures and views, using USER_DEPENDENCIES VIEW, however this System View does not return column names, used in that procedure / view, any idea please .Oracle added fine grained dependency tracking in 11g, but didn't expose any views to query this information. However, it has been reverse engineered a bit: About Oracle: DBA_DEPENDENCY_COLUMNS

  • Rows ino Columns using BEX query

    I got DSO with declaration records. Every declaration can have several versions, say:
    DECLR_ID  VERSION   AMNT
    1111                 01         1000
    1111                 02         1100
    1111                 03         1500 
    1112                 01         5000
    1112                 02         4999
    I need a query to compare final version with corrections (previuos to last versions). The result table must look like this:
    DECLR_ID  VERSION   CORR_AMNT      FINAL_AMNT        DIFF
    1111                 01         1000                     1500                 500
    1111                 02         1100                     1500                 400
    1112                 01         5000                     4999                   -1 
    is it possible by query designer to get results like this by not using any InfoSet?

    Hi,
    There is one possible provided if the version are less,
    Create the restricted KF, for each version where in keep amount as common in all RKF. Then create the calculated KF again for getting the difference of Last version Restricted KF and starting Restricted KF and continue the same for remaining keeping the Last version Restricted KF as common.
    Hope this may work. Try it.
    Regards,
    Vishwa.

  • Find a string between numeric in a column using SQL Query

    Hi need help with the query in SQL. let say i have a column that has 2000 data.. i just need to show the data that has A  at the strating of the data. For example:
    A100001
    W23456
    A234444
    I just need to show all the data got "A" at the start.
    Thank u

    I don't think there is any standard command, so you will need to search for the pattern matching commands of your DBMS.
    In SQL Server the keyword is "like", so your query would be Select col1 from tbl1 where col1 like 'A%'
    Try to take over the world!

  • Can i update more then one column using select query.

    Dear All,
    Can i update more then one column of target table A from source table B. Like
    Update table A set A.A1 = (Select B.B1 from B where A.A3 = B.B3).
    Above stmt. I am updating only one column. But I want to update more then one column A.A1 and A.A2
    Plz give me the possible ways.
    Thanks,
    Vikas

    Yes you can do. Try the below Query
    Update A set (A.A1,A.A2) = (Select B.B1,B.B2 from B where A.A3 = B.B3)

  • Retreving Hexadecimal Values from a table using SQL query.

    Hi,
    I'm looking for a query to retreive Hexadecimal values contained in a column using SQL query.
    The column contains the values of all types, alphanumeric, numeric and hexadecimal.
    I need to retreive only hexadecimal values.
    any help to me in this regard will be appreciated.
    Thanks

    Presumably, you can be sure that any valid hex value is indeed MEANT to BE a hex value. For example the value 'ACE' is meant to be a hexadecimal value and not the word.

  • How to retrive the blob data from a table using sql query

    Hi gurus,
    I have a table which has " BLOB "content in a column .I want to view the data From BLOB column using sql query .It would be helpfull If some one share their idea.
    Regards,
    vardhani.

    You can use data templates.
    See this: http://blogs.oracle.com/xmlpublisher/entry/blob_clob_raw_and_looooong
    http://blogs.oracle.com/xmlpublisher/entry/inserting_blobs_into_your_repo
    Thanks,
    Bipuser

  • How to query the sharepoint list with using lookup column

    hi,
    I have requrement like below
     List A
    Title                         Postionid(Lookup)       PositionDescription
    [email protected]             1                              
    xxxx
    [email protected]             1                               
    xxx
    [email protected]           1                                
    xxx
    [email protected]                    2                               
    sss
    [email protected]             2                               
    www
    List B
    Title                         fistname  lastname age fathername
    [email protected]         x             x             10      y
    [email protected]         p            p               12      p
    [email protected]               q           q                12    
    y
     here in List A positionid is lookup column i have creating one visual web part i am querying the List A using  query string as Positionid in that web part .finally what i want is i need date from list B on basis of corresponding position id here
    List A Title and List B title are same i need each candidate info how can i do it
    Srinivas

    Hello,
    Still you have not told that whether Position id is there in listB or not. Anyway if Poistion id is not there in list B then you have to use Title column of listA to gete data from ListB. Look at sample code below. It will first get data from ListA,
    and use StringBuilder class to create dynamic CAML query because one Position id is having multiple values in listA.
    query.Query = "<Where><Eq><FieldRef Name='Postionid' /><Value Type='Lookup'>1</Value></Eq></Where>";
    SPListItemCollection items = list.GetItems(query);
    if(items.Count > 0)
    foreach(SPListItem item in items)
    string strTitle = Convert.Tostring(item["Title"]);
    //since listA is having multiple values for Postionid so you have to build a dynamic query here to get data from listB
    //Refer link to build dynamic query
    SPQuery listB = new SPQuery ();
    query.Query = stringbuilder;
    SPListItemCollection ListBitems = listB.GetItems(query);
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/2dfe5fd6-e556-4132-a35c-e9f240360307/issue-with-caml-query?forum=sharepointdevelopmentlegacy
    http://sharepoint.stackexchange.com/questions/69690/foreach-loop-inside-caml-query
    Above links will help you to create dynamic query (in your case you need to use multiple <OR> operator in listB.
    Hope it could help
    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

  • How can I get list of columns used of specific table in all stored procedure?

    How can I get used column list of a specific table in among all stored procedure?
    Suppose that,
    I have a table(VendorMaster) which has 100 columns just I want to know how many columns used in among all stored procedure.

    We have solved by below query...
    IF OBJECT_ID('tempdb.dbo.#SPDependencyDetails') IS NOT NULL
    DROP TABLE #SPDependencyDetails
    CREATE TABLE #SPDependencyDetails
     Or_Object_Database NVARCHAR(128)
    ,Or_Object_Name NVARCHAR(128)
    ,Ref_Database_Name NVARCHAR(128)
    ,Ref_Schema_Name NVARCHAR(128)
    ,Ref_Object_Name NVARCHAR(128)
    ,Ref_Column_Name NVARCHAR(128)
    ,Is_Selected BIT
    ,Is_Updated BIT
    ,Is_Select_All BIT
    ,Is_All_Columns_Found BIT
    DECLARE @database_name VARCHAR(100)
    DECLARE database_cursor CURSOR
    FOR
    SELECT name
        FROM sys.databases
        WHERE database_id =8
    OPEN database_cursor
    FETCH NEXT FROM database_cursor
    INTO @database_name
    WHILE @@FETCH_STATUS = 0 --Outer Loop begin
    BEGIN
        DECLARE  @WholeLotofSQL NVARCHAR(MAX) =       '
        DECLARE @object_name VARCHAR(150)
        ,@sqlstatement NVARCHAR(2500)
        DECLARE object_cursor CURSOR --Inner cursor, iterates list of objects that match type
        FOR
            SELECT name
                FROM '+@database_name+'.sys.objects AS o
                WHERE o.type = ''P'' --Change Object type to find dependencies of Functions, Views and etc.
                ORDER BY 1    
        OPEN object_cursor
        FETCH NEXT FROM object_cursor INTO @object_name
        WHILE @@FETCH_STATUS = 0  --Inner Loop Begin
            BEGIN
                SET @sqlstatement = ''USE '+@database_name+';
                                    INSERT INTO #SPDependencyDetails
                                    SELECT DB_NAME() AS Or_Object_Database
                                            ,'''''' + @object_name + '''''' AS Or_Object_Name
                                            ,CASE WHEN referenced_database_name IS NULL THEN DB_NAME()
                                                    ELSE referenced_database_name
                                            END AS Ref_Database_Name
                                            ,referenced_schema_name AS Ref_Schema_Name
                                            ,referenced_entity_name AS Ref_Object_Name
                                            ,referenced_minor_name AS Ref_Column_Name
                                            ,is_selected
                                            ,is_updated
                                            ,is_select_all
                                            ,is_all_columns_found
                                        FROM sys.dm_sql_referenced_entities(''''dbo.'' + @object_name + '''''', ''''OBJECT'''');''
                EXEC sys.sp_executesql @sqlstatement
                FETCH NEXT FROM object_cursor INTO @object_name
            END      
        CLOSE object_cursor
        DEALLOCATE object_cursor'
        EXEC sys.sp_executesql @WholeLotofSQL
        FETCH NEXT FROM database_cursor INTO @database_name
    END
    CLOSE database_cursor;
    DEALLOCATE database_cursor;
    SELECT Or_Object_Database as 'Database'
    ,Or_Object_Name as 'Procedure'
    ,Ref_Object_Name as 'Table'
    ,Ref_Column_Name as 'Column'
    FROM #SPDependencyDetails

  • Appearing Duplicate column in  ABAP QUERY  output list.

    Hi ,
             I have created a report using SAP QUERY having issue on output display  showing duplicate columns  name Weight Unit (LIKP-GEWEI ) and rest          of  field area  coming fine .
                  Kindly help to solve the issue .
             Thanks .

    Hi,
    The currency and quantity units get automatically displayed based on the currency and quantity fields on the output list. However you can restrict it by selecting those fields and choose No currency fields before or after and apply.
    How to do?
    1. Goto SQ01, provide your query name and select "Infoset Query".
    2. Now choose the currency/quantity fields on the lower bottom of window.
    3. Right click on the field and traverse to Unit->Do not Output.
    Hope this helps!
    Br,
    Maju

  • Join two list of integer type field using CAML query

    Hi
    As i am struggling in Join two sharepoint list (Roles and Emplyees). As there is no lookup columns and lists are already existing and have almost 3000 + recodrs. As a part of enhancement work need to join these list. List structure is some thing as follows: 
    Roles :
    ID                              Title
    1                               Project Manager
    2                               Business Analyst
    3                              Developer
    Employees :
    Name                       Role
    1 James                  1
    2 Petar                    3
    3 John                     2
    Output should be like as follows:
    Name                       Role
    1 James                  Project Manager
    2 Petar                    Developer
    3 John                     Developer
    So please suggest, if the joining can be done other than lookup columns through the CAML query.
    Akhilesh Rao

    Follow below CAML query to add joins between two lists.
    SPList list = SPContext.Current.Site.RootWeb.Lists["Employees"];
    SPQuery query = new SPQuery();
    query.Joins = @" <Join Type="INNER" ListAlias="Roles">
    <Eq>
    <FieldRef Name="Employees" RefType="Role" />
    <FieldRef List="Roles" Name="ID" />
    </Eq>
    </Join>";
    query.ProjectedFields = @" <Field Name="RoleName" Type="Lookup" List="Roles" ShowField="Title">";
    query.ViewFields = @ "<FieldRef Name="Title">
    <FieldRef Name="RoleName"> ";
    SPListItemCollection result = tablea.GetItems(query);
    Adnan Amin MCT, SharePoint Architect | If you find this post useful kindly please mark it as an answer :)

Maybe you are looking for

  • Can't download Find My Friends on devices running  iOS 6 or 7

    I can no longer download Find My Friends on my kids iPhone 4(iOS 7) and 3gs(iOS 6) as the app store now says it is only for iOS 8 and up. I used to have one main iCloud account that I used on all my families devices to easily track them. We have had

  • Excise Duty in SAP CRM

    Dear Team In SAP CRM Sales Process i have to create quotation where excise duty has to calculate (Read the details from the Excise Master in SAP) is there any excise master in CRM or system has to get the value from SAP through RFC? Regards Raju

  • Using "File" as the technology in ODI Designer to populate dimensions

    Hello John (John Goodwin) I am currently following your blog on "ODI series 4 - Enter the Designer" and so far I have been successful with the following: 1) Connecting to the planning application 2) Performing Reverse operation However, when I create

  • [CS3-CS5|JS] is there a script to apply creep to artwork?

    I work for a commercial printer and we have RIP software which, when requested, applies creep to impositions. it literally pushes the pages in the imposition closer to the spine in increments of the total creep calculation. Out of curiosity more than

  • How to 'clear' file upload field in forms

    In a form I am designing I need a 'clear' or 'reset' button that clears the entire form. This is easy to do, of course, but alas my form has a file upload field, and in Safari I just can't get that field to go back to it's default empty state. is the