需要关于使用动态列编写sql查询的帮助吗

需要关于使用动态列编写sql查询的帮助吗,sql,oracle,Sql,Oracle,我必须写一个查询,该查询如下所示。我试过了,但写不出来。请帮帮我 我有一个返回结果集下面的表 select * from table1; --(rowid and ColumnName are columns of the table) 输出: rowid ColumnName ------------------------------ 1 Segment1 2 Segment2 appId Segment1

我必须写一个查询,该查询如下所示。我试过了,但写不出来。请帮帮我

我有一个返回结果集下面的表

select * 
from table1;  --(rowid and ColumnName are columns of the table)
输出:

rowid       ColumnName
------------------------------
   1        Segment1    
   2        Segment2
appId     Segment1     Segment2      Segment3
---------------------------------------------
  a1      fld1         fld2          per
  a2      cmp1         hcd4          klp 
SQL> /
RRR , KKK
ZZZ , PPP
LLL , NNN
我有另一个表,它的结构如下:(这里的段1和段2是列)

输出:

rowid       ColumnName
------------------------------
   1        Segment1    
   2        Segment2
appId     Segment1     Segment2      Segment3
---------------------------------------------
  a1      fld1         fld2          per
  a2      cmp1         hcd4          klp 
SQL> /
RRR , KKK
ZZZ , PPP
LLL , NNN
我需要编写一个查询,从第一个表中读取“ColumnName”值,并在第二个表中检索列值

这意味着,从表1中,我将知道表2中可用的列是什么,从表2中,我将知道针对这些列存储的数据是什么

如果我不清楚,请告诉我。
此查询位于Oracle SQL中,下面的PL SQL块是否有助于满足您的需求

BEGIN
FOR iter IN (
                SELECT column_name
                FROM all_tab_columns
                WHERE upper(table_name) = 'table1'
                AND UPPER(column_name) LIKE 'SEGMENT%'
            )       
        LOOP
            SELECT iter.column_name INTO temp_table FROM table1
            dbms_output.put_line(temp_table.column_name);
        END LOOP;
END;
/

假设您有如下表:

SQL> select * from someTable;

   COLUMN1    COLUMN2    COLUMN3
---------- ---------- ----------
         1          2          3
         2          4          6
         3          6          9

SQL> select * from tableOfColumns;

COLUMNN
-------
column1
column3
SQL> declare
  2      type    tListOfResults is table of varchar2(1000);
  3      vSQL    varchar2(1000);
  4      vResult tListOfResults ;
  5  begin
  6      select 'select ' || listagg (columnName, ' || '', '' || ') within group (order by columnName) || ' from someTable'
  7      into vSQL
  8      from tableOfColumns;
  9      --
 10      execute immediate vSQL bulk collect into vResult;
 11      if vResult.count() > 0 then
 12          for i in vResult.first .. vResult.last loop
 13              dbms_output.put_line(vResult(i));
 14          end loop;
 15      end if;
 16  end;
 17  /
1, 3
2, 6
3, 9

PL/SQL procedure successfully completed.
您可能需要以下内容:

SQL> select * from someTable;

   COLUMN1    COLUMN2    COLUMN3
---------- ---------- ----------
         1          2          3
         2          4          6
         3          6          9

SQL> select * from tableOfColumns;

COLUMNN
-------
column1
column3
SQL> declare
  2      type    tListOfResults is table of varchar2(1000);
  3      vSQL    varchar2(1000);
  4      vResult tListOfResults ;
  5  begin
  6      select 'select ' || listagg (columnName, ' || '', '' || ') within group (order by columnName) || ' from someTable'
  7      into vSQL
  8      from tableOfColumns;
  9      --
 10      execute immediate vSQL bulk collect into vResult;
 11      if vResult.count() > 0 then
 12          for i in vResult.first .. vResult.last loop
 13              dbms_output.put_line(vResult(i));
 14          end loop;
 15      end if;
 16  end;
 17  /
1, 3
2, 6
3, 9

PL/SQL procedure successfully completed.

如注释中所述,您需要一个带有动态sql的PLSQL块。请参见下面的示例:

表:

create table table1 (row_id number,
                     ColumnName varchar2(100)) 

create table table2 (appId     number,   
                     Segment1  varchar2(100),
                     Segment2  varchar2(100),    
                     Segment3  varchar2(100));

 Insert all   
    into TABLE1 (ROW_ID, COLUMNNAME)  Values    (1, 'Segment1')
    into TABLE1 (ROW_ID, COLUMNNAME)  Values    (2, 'Segment2')
    into TABLE2 (APPID, SEGMENT1, SEGMENT2, SEGMENT3)  Values  (1, 'RRR', 'KKK', 'MMM')
    into TABLE2 (APPID, SEGMENT1, SEGMENT2, SEGMENT3)  Values  (2, 'ZZZ', 'PPP', 'QQQ')
    into TABLE2 (APPID, SEGMENT1, SEGMENT2, SEGMENT3)  Values  (3, 'LLL', 'NNN', 'DDD')
 select * from dual;
代码:

输出:

rowid       ColumnName
------------------------------
   1        Segment1    
   2        Segment2
appId     Segment1     Segment2      Segment3
---------------------------------------------
  a1      fld1         fld2          per
  a2      cmp1         hcd4          klp 
SQL> /
RRR , KKK
ZZZ , PPP
LLL , NNN

SQL语句中的动态列几乎总是一个坏主意。通常有一种方法可以避免此类问题,并构建一个更简单的解决方案

但是,如果这是您真正需要在SQL中运行动态SQL的极少数情况之一,那么您需要安装并运行类似于我的开源项目的东西

例如:

create table table1 as
select 1 id, 'Segment1' columnName from dual union all
select 2 id, 'Segment2' columnName from dual;

create table table2 as
select 'a1' appId, 'fld1' Segment1, 'fld2' Segment2, 'per' Segment3 from dual union all
select 'a2' appId, 'cmp1' Segment1, 'hcd4' Segment2, 'klp' Segment3 from dual;

select * from table(method4.dynamic_query(
    q'[
        select
              'select appID, '
            ||listagg(columnName, ',') within group (order by id)
            ||' from table2'
            sql_statement
        from table1
    ]'
));

APPID   SEGMENT1   SEGMENT2
-----   --------   --------
a1      fld1       fld2
a2      cmp1       hcd4
以这种方式运行有很多缺点。代码复杂、速度慢,并且有一些奇怪的行为。有关其工作原理的说明,请参阅
Adrian Billington.

对第一个表的查询是什么。。i、 e您根据什么选择表1的输出是分段1还是分段2?我需要读取表1中的所有ColumnName值并从表2中检索它们的值。表1中“ColumnName”列的值应与表2中的列名映射,并应返回RDBMS的值?请添加一个标记,以指定您是否正在使用
mysql
postgresql
sql server
oracle
db2
-或其他完全相同的功能。您需要一个PLSQL块来满足您的要求。您将需要编写一个动态sql来执行soA。在您的答案中,您需要对您提供的sql的工作原理进行一些描述。我在上面的脚本中看到了语法错误。“部分识别规则(铁路图):…此错误似乎与Oracle代码无关;您是如何运行它的?完整的错误消息是什么?