免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1945 | 回复: 9
打印 上一主题 下一主题

问大家一个proc的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-13 14:47 |只看该作者 |倒序浏览


  1. exec sql select a into :b from table

复制代码


其中a可能有查出多个记录(个数不固定),我想将查出的记录放到变量b里,请问这个变量b我该怎么声明?

谢谢大家先。

论坛徽章:
0
2 [报告]
发表于 2003-02-13 15:36 |只看该作者

问大家一个proc的问题。

:b随便, 再定义一个数组, 把:b值赋给每个成员

论坛徽章:
0
3 [报告]
发表于 2003-02-13 15:49 |只看该作者

问大家一个proc的问题。

数组的成员?你是说数组的元素吧。

嗯,还是不太明白你的意思,取出的a可是个结果集啊,不是单一的一条数据,怎么赋给b啊?

哥哥?

论坛徽章:
0
4 [报告]
发表于 2003-02-13 16:08 |只看该作者

问大家一个proc的问题。

/*
*  sample2.pc
*
*  This program connects to ORACLE, declares and opens a cursor,
*  fetches the names, salaries, and commissions of all
*  salespeople, displays the results, then closes the cursor.
*/

#include <stdio.h>;
#include <string.h>;
#include <sqlca.h>;
#include <stdlib.h>;
#include <sqlda.h>;
#include <sqlcpr.h>;

#define UNAME_LEN      20
#define PWD_LEN        11

/*
* Use the precompiler typedef'ing capability to create
* null-terminated strings for the authentication host
* variables. (This isn't really necessary--plain char *'s
* would work as well. This is just for illustration.)
*/
typedef char asciiz[PWD_LEN];

EXEC SQL TYPE asciiz IS CHARZ(PWD_LEN) REFERENCE;
asciiz     username;
asciiz     password;

struct emp_info
{
    asciiz     emp_name;
    float      salary;
    float      commission;
};

void sql_error(msg)
    char *msg;
{
    char err_msg[512];
    size_t buf_len, msg_len;

    EXEC SQL WHENEVER SQLERROR CONTINUE;

    printf("\n%s\n", msg);

/* Call sqlglm() to get the complete text of the
* error message.
*/
    buf_len = sizeof (err_msg);
    sqlglm(err_msg, &amp;buf_len, &amp;msg_len);
    printf("%.*s\n", msg_len, err_msg);

    EXEC SQL ROLLBACK RELEASE;
    exit(EXIT_FAILURE);
}

void main()
{
    struct emp_info *emp_rec_ptr;

/* Allocate memory for emp_info struct. */
    if ((emp_rec_ptr =
        (struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
    {
        fprintf(stderr, "Memory allocation error.\n";
        exit(EXIT_FAILURE);
    }

/* Connect to ORACLE. */
    strcpy(username, "SCOTT";
    strcpy(password, "TIGER";

    EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--";

    EXEC SQL CONNECT :username IDENTIFIED BY :password;
    printf("\nConnected to ORACLE as user: %s\n", username);

/* Declare the cursor. All static SQL explicit cursors
* contain SELECT commands. 'salespeople' is a SQL identifier,
* not a (C) host variable.
*/
    EXEC SQL DECLARE salespeople CURSOR FOR
        SELECT ENAME, SAL, COMM
            FROM EMP
            WHERE JOB LIKE 'SALES%';

/* Open the cursor. */
    EXEC SQL OPEN salespeople;

/* Get ready to print results. */
    printf("\n\nThe company's salespeople are--\n\n";
    printf("Salesperson   Salary   Commission\n";
    printf("-----------   ------   ----------\n";

/* Loop, fetching all salesperson's statistics.
* Cause the program to break the loop when no more
* data can be retrieved on the cursor.
*/
    EXEC SQL WHENEVER NOT FOUND DO break;

    for (;
    {
        EXEC SQL FETCH salespeople INTO :emp_rec_ptr;
        printf("%s %9.2f %12.2f\n", emp_rec_ptr->;emp_name,
                emp_rec_ptr->;salary, emp_rec_ptr->;commission);
    }

/* Close the cursor. */
    EXEC SQL CLOSE salespeople;

    printf("\nArrivederci.\n\n";

    EXEC SQL COMMIT WORK RELEASE;
    exit(EXIT_SUCCESS);
}

论坛徽章:
0
5 [报告]
发表于 2003-02-13 16:16 |只看该作者

问大家一个proc的问题。

爷们,不用游标能实现吗?有例程吗?

论坛徽章:
0
6 [报告]
发表于 2003-02-13 16:19 |只看该作者

问大家一个proc的问题。

爷们,不用游标能实现吗?有例程吗?

论坛徽章:
0
7 [报告]
发表于 2003-02-13 16:41 |只看该作者

问大家一个proc的问题。

个数不确定,不用游标不行!

论坛徽章:
0
8 [报告]
发表于 2003-02-13 16:44 |只看该作者

问大家一个proc的问题。

不用游标也可以实现,如果你知道选择返回的结果的最大记录数,则可以简单的使用宿主数组,示例如下:
char emp_name[50][20];
int    emp_number[50];
float salary[50];

EXEC SQL SELECT ENAME, EMPNO, SAL
         INTO :emp_name, :emp_number, :salary
         FROM EMP
         WHERE SAL >; 1000;

如果不知道选择返回的结果的最大记录数,可能就要使用游标了或者定义一个尽可能大的数组了!!

oracle的ProC C++ Precompiler中的第八章 宿主数组中有介绍,可以从oracle的网站上下载!

论坛徽章:
0
9 [报告]
发表于 2003-02-13 16:48 |只看该作者

问大家一个proc的问题。

[quote]原帖由 "ddmmdd"]个数不确定,不用游标不行![/quote 发表:


那您老说说如果个数是确定的,比如个数为10

那您怎么实现啊?不用游标啊!

ps:我认为个数不是主要问题,可以取出来嘛!

论坛徽章:
0
10 [报告]
发表于 2003-02-13 17:06 |只看该作者

问大家一个proc的问题。

原帖由 "ilmare" 发表:
EXEC SQL SELECT ENAME, EMPNO, SQL
         INTO :emp_name, :emp_number, :salary
         FROM EMP
         WHERE SAL >; 1000;

如果不知道选择返回的结果的最大记录数,可能就要使用游标了或者定义一..........



说的对!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP