Chinaunix首页 | 论坛 | 博客
  • 博客访问: 246201
  • 博文数量: 14
  • 博客积分: 2015
  • 博客等级: 大尉
  • 技术积分: 902
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-27 16:02
文章分类

全部博文(14)

文章存档

2009年(5)

2008年(9)

我的朋友

分类: LINUX

2009-03-28 20:25:39

mysql中提供了一个G [g] 标志,放到sql语句后,可以使一行的每个列打印到单独的行。例如:
mysql> select * from user_msg limit 2\G;
*************************** 1. row ***************************
         uid: 1
   today_msg: 0
  today_recv: 1
recvmsgcount: 59
sendmsgcount: 0
     twoday1: 0
    treeday1: 0
     towday2: 0
    treeday2: 0
   todayfist: 0
*************************** 2. row ***************************
         uid: 2
   today_msg: 0
  today_recv: 1
recvmsgcount: 292
sendmsgcount: 2693
     twoday1: 0
    treeday1: 0
     towday2: 0
    treeday2: 0
   todayfist: 0
2 rows in set (0.02 sec)
 
oracle中没有类似的选项,不过,Tom Tkyte 大师开发了一个过程实现同样的功能:
create or replace procedure print_table(p_query in varchar2) AUTHID CURRENT_USER is
  l_theCursor   integer default dbms_sql.open_cursor;
  l_columnValue varchar2(4000);
  l_status      integer;
  l_descTbl     dbms_sql.desc_tab;
  l_colCnt      number;
  n             number;
begin
  dbms_sql.parse(l_theCursor, p_query, dbms_sql.native);
  dbms_sql.describe_columns(l_theCursor, l_colCnt, l_descTbl);
  for i in 1 .. l_colCnt loop
    dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);
  end loop;
  l_status := dbms_sql.execute(l_theCursor);
  n :=1;
  while (dbms_sql.fetch_rows(l_theCursor) > 0) loop
    dbms_output.put_line('******* '||n||' rows *******');
    for i in 1 .. l_colCnt loop
      dbms_sql.column_value(l_theCursor, i, l_columnValue);
      dbms_output.put_line(rpad(l_descTbl(i).col_name, 30) || ': ' || l_columnValue);
    end loop;
    dbms_output.put_line('------------------');
    n := n+1;
  end loop;
exception
  when others then
    dbms_sql.close_cursor(l_theCursor);
    RAISE;
end;
这里小小修改了下大师的过程,加上了行数的显示。
建议将上述过程建在sys帐号下,并且在sys下建一个公共同义词,这样所有其他用户都可以用print_table了
SQLPLUS>create public synonym print_table for sys.print_table;
Synonym created.
 
print_table('select * from emp where rownum<3');
******* 1 rows *******
EMPNO         : 7369
ENAME         : SMITH
JOB         : CLERK
MGR         : 7902
HIREDATE        : 17-DEC-80
SAL         : 800
COMM         :
DEPTNO         : 20
------------------
******* 2 rows *******
EMPNO         : 7499
ENAME         : ALLEN
JOB         : SALESMAN
MGR         : 7698
HIREDATE        : 20-FEB-81
SAL         : 1600
COMM         : 300
DEPTNO         : 30
------------------
PL/SQL procedure successfully completed.
阅读(2082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~