select a specific column from a ref cursor
2014-09-19 10:45
441 查看
From stackoverflow
Table and sample data (COLUMN1 has the numbers 1 - 10):
create table table1(column1 number, column2 date, column3 varchar2(1000), column4 clob); insert into table1 select level, sysdate, level, level from dual connect by level <= 10; commit;
Package with a procedure that opens a ref cursor and selects everything:
create or replace package test_pkg is type cur_Table1 is ref cursor return table1%rowtype; procedure sp1(p_cursor in out cur_table1); end; / create or replace package body test_pkg is procedure sp1(p_cursor in out cur_table1) is begin open p_cursor for select column1, column2, column3, column4 from table1; end; end; /
PL/SQL block that reads COLUMN1 data from the ref cursor:
--Basic steps are: call procedure, convert cursor, describe and find columns, --then fetch rows and retrieve column values. -- --Each possible data type for COLUMN1 needs to be added here. --Currently only NUMBER is supported. declare v_cursor sys_refcursor; v_cursor_number number; v_columns number; v_desc_tab dbms_sql.desc_tab; v_position number; v_typecode number; v_number_value number; begin --Call procedure to open cursor test_pkg.sp1(v_cursor); --Convert cursor to DBMS_SQL cursor v_cursor_number := dbms_sql.to_cursor_number(rc => v_cursor); --Get information on the columns dbms_sql.describe_columns(v_cursor_number, v_columns, v_desc_tab); --Loop through all the columns, find COLUMN1 position and type for i in 1 .. v_desc_tab.count loop if v_desc_tab(i).col_name = 'COLUMN1' then v_position := i; v_typecode := v_desc_tab(i).col_type; --Pick COLUMN1 to be selected. if v_typecode = dbms_types.typecode_number then dbms_sql.define_column(v_cursor_number, i, v_number_value); --...repeat for every possible type. end if; end if; end loop; --Fetch all the rows, then get the relevant column value and print it while dbms_sql.fetch_rows(v_cursor_number) > 0 loop if v_typecode = dbms_types.typecode_number then dbms_sql.column_value(v_cursor_number, v_position, v_number_value); dbms_output.put_line('Value: '||v_number_value); --...repeat for every possible type end if; end loop; end; /
相关文章推荐
- Move vim cursor to a specific column
- SSH框架-unexpected token: * near line 1, column 8 [select * from tb_chaper where course_id = 2];报错解决方法
- select rows by values in a column from Dataframe
- 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选
- unexpected token: * near line 1, column 8 [select * from t_one_buy where 1=1 order by create_date de
- 安卓E/CursorWindow: Failed to read row 0, column 0 from a CursorWindow which has 0 rows, 5 columns解决方案
- unexpected token: * near line 1, column 8 [select * from t_user where 1=1]
- SELECT TOP column FROM table [ORDER BY column [DESC]]
- E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 4 rows, 2 columns.
- Android中出现:Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 5 columns异常
- PLS-00402: alias required in SELECT list of cursor to avoid duplicate column
- PL/SQL cursor 游标 where条件接收形参为VARCHAR 不能正常执行的问题 SELECT ContactNameC FROM MTContact WHERE objectno
- mysql中select column_name from Information_schema.columns where table_Name = 'test'出现多个字段的问题
- unexpected token: * near line 1, column 8 [select * from tb_cat]
- Select Column Name from a Table
- : Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 9 columns.解决
- Failed to read row 0, column -1 from a CursorWindow which h
- JDBC连接mysql,TOMCAT错误: Cannot convert value '0000-00-00 00:00:00' from column 10 to TIMESTAMP
- PLSQL中显示Cursor、隐示Cursor、动态Ref Cursor区别
- Mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。