这两个oracle proc代码有什么不同?
start = 1, end = 2
。
发生错误时,PRINT_SQL_ERROR
打印sqlca.sqlcode
和sqlca.sqlerrm.sqlerrmc
。
第一个EXEC SQL PREPARE
和EXEC SQL EXECUTE
位于其中,用于语法。
每次都要执行“准备和执行”。
memset(iQuery, 0x00, sizeof(iQuery));
sprintf(iQuery, "INSERT INTO ACCT_BALANCE VALUES (:sno, :sno, SYSDATE, SYSDATE, :sno,
:sno, :sno, :sno, :sno, 'UPP', 'LOW', 'STT', SYSDATE, :sno, :sno, :sno, :sno, :sno,
:sno, :sno )");
for ( sno = start ; sno <= end ; sno ++ )
{
EXEC SQL AT :sAT PREPARE INSERT_STMT FROM :iQuery;
EXEC SQL AT :sAT
EXECUTE INSERT_STMT USING :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno,
:sno, :sno, :sno, :sno, :sno;
printf("[sno]=%d\n", sno);
if (sqlca.sqlcode != 0)
{
PRINT_SQL_ERROR("[ERROR] Insert Execute Failure!");
return -1;
}
EXEC SQL AT :sAT COMMIT;
}
输出为
[sno]=1
[sno]=2
SQL> SELECT COUNT(*) FROM ACCT_BALANCE;
COUNT(*)
---------------
2
1 row selected.
Elapsed: 00:00:00.01
其次,EXEC SQL PREPARE
位于语法外部,EXEC SQL EXECUTE
位于内部。
memset(iQuery, 0x00, sizeof(iQuery));
sprintf(iQuery, "INSERT INTO ACCT_BALANCE VALUES (:sno, :sno, SYSDATE, SYSDATE, :sno, :sno, :sno, :sno,
:sno, 'UPP', 'LOW', 'STT', SYSDATE, :sno, :sno, :sno, :sno, :sno, :sno, :sno )");
EXEC SQL AT :sAT PREPARE INSERT_STMT FROM :iQuery;
for ( sno = start ; sno <= end ; sno ++ )
{
EXEC SQL AT :sAT
EXECUTE INSERT_STMT USING :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno, :sno,
:sno, :sno;
printf("[sno]=%d\n", sno);
if (sqlca.sqlcode != 0)
{
PRINT_SQL_ERROR("[ERROR] Insert Execute Failure!");
return -1;
}
EXEC SQL AT :sAT COMMIT;
}
输出为
[sno]=1
[sno]=2
[ERROR] Insert Execute Failure!
SQLCODE : -1001
ERROR MSG : ORA-01001: invalid cursor
SQL> SELECT * FROM ACCT_BALANCE;
ACCT_BALANCE_ID BALANCE_TYPE_ID EFF_DATE EXP_DATE BALANCE CHARGE1 CHARGE2
--------------- --------------- ------------------- ------------------- --------------- --------------- ---------------
CYCLE_UPPER CYCLE_LOWER CYCLE_UPP CYCLE_LOW STATE STATE_DATE ACCT_ID SERV_ID
--------------- --------------- --------- --------- --------- ------------------- --------------- ---------------
ITEM_GROUP_ID OBJECT_TYPE_ID LOWER_BILLING_CYCLE_ID UPPER_BILLING_CYCLE_ID PRIORITY
--------------- --------------- ---------------------- ---------------------- ---------------
1 1 2018/07/28 00:07:33 2018/07/28 00:07:33 1 1 1
1 1 UPP LOW STT 2018/07/28 00:07:33 1 1
1 1 1 1 1
1 row selected.
Elapsed: 00:00:00.00
第二种方式有误吗?
我应该每次都使用PREPARE吗?