【问题标题】:Can't test nulls in Oracle Pro*C无法在 Oracle Pro*C 中测试空值
【发布时间】:2019-10-03 10:06:01
【问题描述】:

我是 Proc Oracle 和 C 的初学者。当我基于 Oracle Pro Proc 文档练习示例时,我无法测试使用指标的 Null,参考 Oracle Pro*c guide link(我不确定我的代码是正确的)。

Oracle 文档写入:

6.2.4 测试 NULL 您可以在 WHERE 子句中使用指示变量来测试 NULL,如以下示例所示:

EXEC SQL SELECT ename, sal INTO :emp_name, :salary 来自雇员 WHERE :commission INDICATOR :ind_comm IS NULL ...

我的代码如下:

Locations table:

+-------------+---------+---------+
| Location_Id | Address | Country |
+-------------+---------+---------+
|           1 | London  |  London |
|           2 | US      |    null |
+-------------+---------+---------+


void connect()
{
    /* Connect to ORACLE. */
    strcpy(username, "hr");
    strcpy(password, "hr");

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

void testNull()
{
    short country_ind = -1;
    char address[20];
    char *country = "country";
    EXEC SQL SELECT ADDRESS INTO :address FROM LOCATIONS
    WHERE :country INDICATOR :country_ind is null;
    /*
Current, it always output address is London instead of US
 */
        printf("address :%d\n", address);
    }

void main()
{
    connect();
    testNull();
}

【问题讨论】:

  • 您能否重新表述一下您的 SQL 查询,似乎它们丢失了运算符,此外,我没有看到您的查询与表之间的对应关系
  • @Gar,我更新了你的评论,谢谢。我在 Oracle Pro*c 中写 sql

标签: c oracle oracle-pro-c


【解决方案1】:

这里没有“指明”指标的使用(无意的双关语)

我会这样写的

void testNull()
{
    short country_ind = -1;
    char address[20];
    char *country = "country";
    EXEC SQL SELECT ADDRESS INTO :address FROM LOCATIONS
    WHERE country  is null;
    /*
Current, it always output address is London instead of US
 */
        printf("address :%d\n", address);
    }

如果之后需要检查地址是否为空,您可以使用地址指示符

如果你想让“国家”成为一个变量 那么你的代码应该是这样的:

void testNull()
{
    short country_ind = 0;   // the word "country" is not null, but the value of the field can be depending on the query/data
     char address[20];
   char *country = "country";
EXEC SQL SELECT ADDRESS INTO :address FROM LOCATIONS
WHERE :country INDICATOR :country_ind is null;
/*
Current, it always output address is London instead of US
 */
        printf("address :%d\n", address);
    }

【讨论】:

  • 感谢您的帮助。第一种方法工作正常,但第二种方法不起作用。我将继续使用第一种方法以避免花费时间
  • 欢迎!如果适合您的需要,请不要犹豫接受答案。
猜你喜欢
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
相关资源
最近更新 更多