0

我一直在寻找解决方案,但找不到直接的答案。

我希望我的 oracle select 语句根据逗号分隔列。所以,假设我有一个这样的查询:

从人员中选择 *

Results:
Name Age 
Bob  4
Sam  5
Joe  7

我希望结果是:

Bob,4
Sam,5
Joe,7

我怎样才能做到这一点?

4

3 回答 3

1

使用串联 ||,并选择它们之间的逗号值。

SELECT NAME || ',' || AGE
FROM
(
    SELECT 'BOB' NAME, 4 AGE FROM DUAL
    UNION ALL
    SELECT 'SAM' NAME, 5 AGE FROM DUAL
    UNION ALL
    SELECT 'JOE' NAME, 7 AGE FROM DUAL
)
于 2012-12-11T22:37:22.477 回答
0

这是正确的查询:

SELECT NAME||','||AGE FROM PEOPLE;
于 2012-12-12T13:01:32.160 回答
0

根据AskTom

ops$tkyte@8i> create or replace procedure dump_table_to_csv( p_tname in varchar2,
  2                                                 p_dir   in varchar2,
  3                                                 p_filename in varchar2 )
  4  is
  5      l_output        utl_file.file_type;
  6      l_theCursor     integer default dbms_sql.open_cursor;
  7      l_columnValue   varchar2(4000);
  8      l_status        integer;
  9      l_query         varchar2(1000)
 10                      default 'select * from ' || p_tname;
 11      l_colCnt        number := 0;
 12      l_separator     varchar2(1);
 13      l_descTbl       dbms_sql.desc_tab;
 14  begin
 15      l_output := utl_file.fopen( p_dir, p_filename, 'w' );
 16      execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' 
';
 17  
 18      dbms_sql.parse(  l_theCursor,  l_query, dbms_sql.native );
 19      dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
 20  
 21      for i in 1 .. l_colCnt loop
 22          utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' 
);
 23          dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
 24          l_separator := ',';
 25      end loop;
 26      utl_file.new_line( l_output );
 27  
 28      l_status := dbms_sql.execute(l_theCursor);
 29  
 30      while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
 31          l_separator := '';
 32          for i in 1 .. l_colCnt loop
 33              dbms_sql.column_value( l_theCursor, i, l_columnValue );
 34              utl_file.put( l_output, l_separator || l_columnValue );
 35              l_separator := ',';
 36          end loop;
 37          utl_file.new_line( l_output );
 38      end loop;
 39      dbms_sql.close_cursor(l_theCursor);
 40      utl_file.fclose( l_output );
 41  
 42      execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
 43  exception
 44      when others then
 45          execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
 46          raise;
 47  end;
 48  /

Procedure created.

ops$tkyte@8i> exec dump_table_to_csv( 'emp', '/tmp', 'tkyte.emp' );
于 2012-12-11T22:34:06.133 回答