Oracle SQL (Pro*C):预先知道 select 的行数
Oracle SQL (Pro*C): knowing number of rows of a select beforehand
在 Pro*C 文件中,我有一个像这样的提取模式:
EXEC SQL BEGIN DECLARE SECTION;
typedef struct my_row_t
{
unsigned field1;
unsigned field2;
} my_row;
EXEC SQL END DECLARE SECTION;
void do_query()
{
EXEC SQL BEGIN DECLARE SECTION;
struct my_row* buffer;
unsigned buffer_size;
EXEC SQL END DECLARE SECTION;
buffer_size = /* some tied upper bound on the number of rows to fetch */;
buffer = (my_row*)malloc(buffer_size * sizeof(my_row));
// Create my_cursor based on some select query
// ...
// and now:
EXEQ SQL FOR :buffer_size FETCH my_cursor INTO :buffer;
unsigned number_of_fetched_rows = sqlca.sqlerrd[2];
// At this point, with just one fetch I have all the rows
}
问题是,有时您无法估算出合适的上限。我说的是不会 return 超过几千行的查询,但我不想为例如 5000 行分配存储,当针对特定输入的特定查询只会给出你500.
有没有一种方法可以在执行第一次提取之前(例如,刚打开游标后)以有效的方式知道要提取的行数,而无需执行任何 count(*)
查询(对于性能原因)?
我的目标是以尽可能高效的方式一次性“获取所有”。
我觉得用PL/SQLtables/records可以做到,但我不知道如何“保存”或提取PL/SQLtable的内容在我自己的缓冲区中使用 Pro*C 语法。
Is there a way to know in an efficient way, before doing your first fetch (for example, just after opening the cursor), the number of rows to fetch, without executing any count(*) query (for performance reasons)?
不幸的是,none。
在 Pro*C 文件中,我有一个像这样的提取模式:
EXEC SQL BEGIN DECLARE SECTION;
typedef struct my_row_t
{
unsigned field1;
unsigned field2;
} my_row;
EXEC SQL END DECLARE SECTION;
void do_query()
{
EXEC SQL BEGIN DECLARE SECTION;
struct my_row* buffer;
unsigned buffer_size;
EXEC SQL END DECLARE SECTION;
buffer_size = /* some tied upper bound on the number of rows to fetch */;
buffer = (my_row*)malloc(buffer_size * sizeof(my_row));
// Create my_cursor based on some select query
// ...
// and now:
EXEQ SQL FOR :buffer_size FETCH my_cursor INTO :buffer;
unsigned number_of_fetched_rows = sqlca.sqlerrd[2];
// At this point, with just one fetch I have all the rows
}
问题是,有时您无法估算出合适的上限。我说的是不会 return 超过几千行的查询,但我不想为例如 5000 行分配存储,当针对特定输入的特定查询只会给出你500.
有没有一种方法可以在执行第一次提取之前(例如,刚打开游标后)以有效的方式知道要提取的行数,而无需执行任何 count(*)
查询(对于性能原因)?
我的目标是以尽可能高效的方式一次性“获取所有”。
我觉得用PL/SQLtables/records可以做到,但我不知道如何“保存”或提取PL/SQLtable的内容在我自己的缓冲区中使用 Pro*C 语法。
Is there a way to know in an efficient way, before doing your first fetch (for example, just after opening the cursor), the number of rows to fetch, without executing any count(*) query (for performance reasons)?
不幸的是,none。