English 中文(简体)
a. 检索具有一定价值的栏目清单(滚动中)
原标题:SQL: Search a list of columns with a given value (within a row)
  • 时间:2010-01-26 15:26:41
  •  标签:
  • sql
  • oracle

我的表格有many栏。 是否有办法回答问题: “对于某个特定(主要关键)来说,这一增长中哪些领域具有10个价值”?

EDIT:

查明: 表格是适当的。 在我追踪一些不正确的数据时,我正在做一些人工查询。 该表被优化为代表<><>vast>查询主要内容的自动查询最快。 (有9 500多万行,每个优化区都很重要)

我认识到,我的问题是要做一些事情。 我只想看到,我想要得到的东西有些trick。

EDIT 邮政:

在我们的系统中,我们有许多不同的用户账户。 其中一个账户是我们用于所有只读查询的账户(这是我使用的大部分时间)。 我没有掌握这些表格,因此,当我对我的情况做出调整时,我不得不作以下改动:

USER_TAB_COLUMNS 不得不成为<条码>。

最佳回答

这不是一个正常的数据库功能。 然而,你们不是第一个要求这样做的人,也不是像那个人。

解决办法需要两点。 第一是数据词典;Oracle数据库支持反省,但它确实提出了一套看法,使我们对数据库物体的元数据。 在这种情况下,我们需要<代码>用户_tab_columns,这将给我们一个表格的栏目。 第二件是动态的,是能够在操作时间组装,然后执行。 这样做有两种方法,但通常可以 re。

以下法典是概念的证明。 它包含四个参数:

  1. the name of table you want to search
  2. the name of that table s primary key column
  3. the primary key value you want to restrict by
  4. the value you want to search for.

它是粗略的,因此,你可能需要把产出提炼或使方案更加灵活。

create or replace procedure search_cols
  (tname in user_tables.table_name%type
   , pk_col in user_tab_columns.column_name%type
   , pk in number
   , val in number )
is
    firstcol boolean := true;
    stmt varchar2(32767);
    result varchar2(32767);
    rc sys_refcursor;
begin
    stmt :=  select  ;
    << projection >>
    for lrec in ( select column_name from user_tab_columns
                  where table_name = tname
                  and column_name != pk_col
                  and data_type =  NUMBER 
                  order by column_id )
    loop
        if not firstcol then
            stmt := stmt || chr(10) ||  ||  ,  || ;
        else
            firstcol := false;
        end if;
        stmt := stmt ||   case when  || lrec.column_name||  =  || val ||
                             then    || lrec.column_name ||     else null end ;
    end loop projection;
    stmt := stmt || chr(10)||   from  ||tname||  where  || pk_col ||   =  || pk;
    --  dbms_output.put_line(stmt);
    open rc for stmt;
    fetch rc into result;
    close rc;
    dbms_output.put_line(tname ||  ::  || val ||   found in  ||result);
end search_cols;
/

如你所看到的那样,“动态”难以读到。 更难以想象: 因此,有手段表明最后声明是个好主意。

无论如何,结果如下:

SQL> set serveroutput on size unlimited
SQL> exec search_cols( T23 ,  ID , 111, 10)
T23::10 found in ,COL_B,COL_C,

PL/SQL procedure successfully completed.

SQL> exec search_cols( T23 ,  ID , 222, 10)
T23::10 found in COL_A,,,

PL/SQL procedure successfully completed.

SQL>
问题回答

如同你的数据库一样,它不适当地正常化。 尽管如此,你可能利用联普观察团在分局内的指挥来做你再次试图做的事情。

我的解决办法是使用字典表格(USER_TAB_COLUMNS),以动态的方式从您的桌子和动态车库中选取所有NUMBER栏的名称,因为我在这里看不到如何避免。

DECLARE
   CURSOR cur_columns IS
          select COLUMN_NAME from USER_TAB_COLUMNS 
          where TABLE_NAME= <MY_TABLE>  and DATA_TYPE= NUMBER ;
   query_text VARCHAR2(1000);
   result_value NUMBER;
BEGIN
   -- Iterate through each NUMBER column of the table
   FOR rec_col IN cur_columns LOOP

       -- In my line of primary key <MY_ID>, check if the current column has
       --  the wanted value.
       query_text := 
          SELECT count(1) FROM <MY_TABLE> WHERE <TABLE_ID> = <MY_ID> AND   
         || rec_col.COLUMN_NAME ||   = <MY_VALUE> ; -- < the "magic" is here

       EXECUTE IMMEDIATE query_text INTO result_value; 

       IF result_value > 0 THEN
          DBMS_OUTPUT.PUT_LINE( Got a match for column   || 
                               rec_col.COLUMN_NAME ||  . );
       END IF;

   END LOOP;
END;

显然,你需要用自己选择的价值来取代所有“斜线”、变数和大体。

对于人工查询,它进行罚款。 然而,这种情况的表现可能不好,因此与大量数据相比没有变化。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...