无法在 Oracle Pro*C 中测试空值



我是关于pro c oracle和C的初学者。Oracle Pro*c指南链接(我不确定我的代码是否正确(。

oracle文档写:

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

exec sql select select ename,sal to:emp_name,:薪金 来自EMP 地点:佣金指标:ind_comm为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: %sn", 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 :%dn", address);
    }
void main()
{
    connect();
    testNull();
}

在这里没有"指示"指示器的使用(意外双关(

我会以这种方式写

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 :%dn", address);
    }

,如果需要检查之后是否为null

如果您希望"国家"成为变量那么您的代码应该是:

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 :%dn", address);
    }

相关内容

  • 没有找到相关文章

最新更新