#include EXEC SQL INCLUDE sqlca;
int main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
struct ST_OUTPUT
{
long id;
char name[201];
char name2[256];
} m_output = {0};
EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT TO DB_t;
EXEC SQL DECLARE C CURSOR FOR
SELECT ID, NAME, NAME2
FROM BACK.TEST_A
WITH UR;
EXEC SQL OPEN C;
if (SQLCODE != 0)
{
printf("~~~~~~ERROR [SQLCODE=%d]\n", SQLCODE);
return -1;
}
while(1)
{
EXEC SQL FETCH C INTO :m_output;
if (SQLCODE != 0)
{
printf("~~~~~~ERROR [SQLCODE=%d]\n", SQLCODE);
break;
}
else
{
printf("~~~~~~[%d][%s][%s]\n", m_output.id, m_output.name, m_output.name2);
}
}
EXEC SQL CLOSE C;
EXEC SQL CONNECT RESET;
return 0;
}
///测试测试
[back@localhost tmp]$ db2 prep main.sqc bindfile
LINE MESSAGES FOR main.sqc
------ --------------------------------------------------------------------
SQL0060W The "C" precompiler is in progress.
SQL0091W Precompilation or binding was ended with "0"
errors and "0" warnings.
[back@localhost tmp]$
[back@localhost tmp]$ db2 bind main.bnd
LINE MESSAGES FOR main.bnd
------ --------------------------------------------------------------------
SQL0061W The binder is in progress.
SQL0091N Binding was ended with "0" errors and "0" warnings.
[back@localhost tmp]$
[back@localhost tmp]$ db2 GRANT EXECUTE ON PACKAGE main to public
DB20000I The SQL command completed successfully.
[back@localhost tmp]$
[back@localhost tmp]$ gcc -c -g -I/home/db2inst1/sqllib/include main.c
[back@localhost tmp]$
[back@localhost tmp]$ gcc -lstdc++ -lm -L/home/db2inst1/sqllib/lib -ldb2 -o test main.o
/usr/bin/ld: warning: libstdc++.so.5, needed by /home/db2inst1/sqllib/lib/libdb2.so, may conflict with libstdc++.so.6
[back@localhost tmp]$
[back@localhost tmp]$ ./test
~~~~~~[1][abc_abc][abc_abc]
~~~~~~[2][we_dddd][we_dddd]
~~~~~~[3][dgfh_ryuyu][dgfh_ryuyu]
~~~~~~[5][wewerertty][]
~~~~~~ERROR [SQLCODE=100]
[back@localhost tmp]$收起