1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Error Handling</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.15 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="ECPG - Embedded SQL in C"
HREF="ecpg.html"><LINK
REL="PREVIOUS"
TITLE="Using Descriptor Areas"
HREF="ecpg-descriptors.html"><LINK
REL="NEXT"
TITLE="Preprocessor Directives"
HREF="ecpg-preproc.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2015-02-02T21:03:01"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.15 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Using Descriptor Areas"
HREF="ecpg-descriptors.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="ecpg.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 33. <SPAN
CLASS="APPLICATION"
>ECPG</SPAN
> - Embedded <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> in C</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Preprocessor Directives"
HREF="ecpg-preproc.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="ECPG-ERRORS"
>33.8. Error Handling</A
></H1
><P
> This section describes how you can handle exceptional conditions
and warnings in an embedded SQL program. There are two
nonexclusive facilities for this.
<P
></P
></P><UL
><LI
><P
> Callbacks can be configured to handle warning and error
conditions using the <TT
CLASS="LITERAL"
>WHENEVER</TT
> command.
</P
></LI
><LI
><P
> Detailed information about the error or warning can be obtained
from the <TT
CLASS="VARNAME"
>sqlca</TT
> variable.
</P
></LI
></UL
><P>
</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ECPG-WHENEVER"
>33.8.1. Setting Callbacks</A
></H2
><P
> One simple method to catch errors and warnings is to set a
specific action to be executed whenever a particular condition
occurs. In general:
</P><PRE
CLASS="PROGRAMLISTING"
>EXEC SQL WHENEVER <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>action</I
></TT
>;</PRE
><P>
</P
><P
> <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> can be one of the following:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>SQLERROR</TT
></DT
><DD
><P
> The specified action is called whenever an error occurs during
the execution of an SQL statement.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>SQLWARNING</TT
></DT
><DD
><P
> The specified action is called whenever a warning occurs
during the execution of an SQL statement.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>NOT FOUND</TT
></DT
><DD
><P
> The specified action is called whenever an SQL statement
retrieves or affects zero rows. (This condition is not an
error, but you might be interested in handling it specially.)
</P
></DD
></DL
></DIV
><P>
</P
><P
> <TT
CLASS="REPLACEABLE"
><I
>action</I
></TT
> can be one of the following:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>CONTINUE</TT
></DT
><DD
><P
> This effectively means that the condition is ignored. This is
the default.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>GOTO <TT
CLASS="REPLACEABLE"
><I
>label</I
></TT
></TT
><BR><TT
CLASS="LITERAL"
>GO TO <TT
CLASS="REPLACEABLE"
><I
>label</I
></TT
></TT
></DT
><DD
><P
> Jump to the specified label (using a C <TT
CLASS="LITERAL"
>goto</TT
>
statement).
</P
></DD
><DT
><TT
CLASS="LITERAL"
>SQLPRINT</TT
></DT
><DD
><P
> Print a message to standard error. This is useful for simple
programs or during prototyping. The details of the message
cannot be configured.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>STOP</TT
></DT
><DD
><P
> Call <TT
CLASS="LITERAL"
>exit(1)</TT
>, which will terminate the
program.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>DO BREAK</TT
></DT
><DD
><P
> Execute the C statement <TT
CLASS="LITERAL"
>break</TT
>. This should
only be used in loops or <TT
CLASS="LITERAL"
>switch</TT
> statements.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>CALL <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> (<TT
CLASS="REPLACEABLE"
><I
>args</I
></TT
>)</TT
><BR><TT
CLASS="LITERAL"
>DO <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> (<TT
CLASS="REPLACEABLE"
><I
>args</I
></TT
>)</TT
></DT
><DD
><P
> Call the specified C functions with the specified arguments.
</P
></DD
></DL
></DIV
><P>
The SQL standard only provides for the actions
<TT
CLASS="LITERAL"
>CONTINUE</TT
> and <TT
CLASS="LITERAL"
>GOTO</TT
> (and
<TT
CLASS="LITERAL"
>GO TO</TT
>).
</P
><P
> Here is an example that you might want to use in a simple program.
It prints a simple message when a warning occurs and aborts the
program when an error happens:
</P><PRE
CLASS="PROGRAMLISTING"
>EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR STOP;</PRE
><P>
</P
><P
> The statement <TT
CLASS="LITERAL"
>EXEC SQL WHENEVER</TT
> is a directive
of the SQL preprocessor, not a C statement. The error or warning
actions that it sets apply to all embedded SQL statements that
appear below the point where the handler is set, unless a
different action was set for the same condition between the first
<TT
CLASS="LITERAL"
>EXEC SQL WHENEVER</TT
> and the SQL statement causing
the condition, regardless of the flow of control in the C program.
So neither of the two following C program excerpts will have the
desired effect:
</P><PRE
CLASS="PROGRAMLISTING"
>/*
* WRONG
*/
int main(int argc, char *argv[])
{
...
if (verbose) {
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
}
...
EXEC SQL SELECT ...;
...
}</PRE
><P>
</P><PRE
CLASS="PROGRAMLISTING"
>/*
* WRONG
*/
int main(int argc, char *argv[])
{
...
set_error_handler();
...
EXEC SQL SELECT ...;
...
}
static void set_error_handler(void)
{
EXEC SQL WHENEVER SQLERROR STOP;
}</PRE
><P>
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ECPG-SQLCA"
>33.8.2. sqlca</A
></H2
><P
> For more powerful error handling, the embedded SQL interface
provides a global variable with the name <TT
CLASS="VARNAME"
>sqlca</TT
>
(SQL communication area)
that has the following structure:
</P><PRE
CLASS="PROGRAMLISTING"
>struct
{
char sqlcaid[8];
long sqlabc;
long sqlcode;
struct
{
int sqlerrml;
char sqlerrmc[SQLERRMC_LEN];
} sqlerrm;
char sqlerrp[8];
long sqlerrd[6];
char sqlwarn[8];
char sqlstate[5];
} sqlca;</PRE
><P>
(In a multithreaded program, every thread automatically gets its
own copy of <TT
CLASS="VARNAME"
>sqlca</TT
>. This works similarly to the
handling of the standard C global variable
<TT
CLASS="VARNAME"
>errno</TT
>.)
</P
><P
> <TT
CLASS="VARNAME"
>sqlca</TT
> covers both warnings and errors. If
multiple warnings or errors occur during the execution of a
statement, then <TT
CLASS="VARNAME"
>sqlca</TT
> will only contain
information about the last one.
</P
><P
> If no error occurred in the last <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> statement,
<TT
CLASS="LITERAL"
>sqlca.sqlcode</TT
> will be 0 and
<TT
CLASS="LITERAL"
>sqlca.sqlstate</TT
> will be
<TT
CLASS="LITERAL"
>"00000"</TT
>. If a warning or error occurred, then
<TT
CLASS="LITERAL"
>sqlca.sqlcode</TT
> will be negative and
<TT
CLASS="LITERAL"
>sqlca.sqlstate</TT
> will be different from
<TT
CLASS="LITERAL"
>"00000"</TT
>. A positive
<TT
CLASS="LITERAL"
>sqlca.sqlcode</TT
> indicates a harmless condition,
such as that the last query returned zero rows.
<TT
CLASS="LITERAL"
>sqlcode</TT
> and <TT
CLASS="LITERAL"
>sqlstate</TT
> are two
different error code schemes; details appear below.
</P
><P
> If the last SQL statement was successful, then
<TT
CLASS="LITERAL"
>sqlca.sqlerrd[1]</TT
> contains the OID of the
processed row, if applicable, and
<TT
CLASS="LITERAL"
>sqlca.sqlerrd[2]</TT
> contains the number of
processed or returned rows, if applicable to the command.
</P
><P
> In case of an error or warning,
<TT
CLASS="LITERAL"
>sqlca.sqlerrm.sqlerrmc</TT
> will contain a string
that describes the error. The field
<TT
CLASS="LITERAL"
>sqlca.sqlerrm.sqlerrml</TT
> contains the length of
the error message that is stored in
<TT
CLASS="LITERAL"
>sqlca.sqlerrm.sqlerrmc</TT
> (the result of
<CODE
CLASS="FUNCTION"
>strlen()</CODE
>, not really interesting for a C
programmer). Note that some messages are too long to fit in the
fixed-size <TT
CLASS="LITERAL"
>sqlerrmc</TT
> array; they will be truncated.
</P
><P
> In case of a warning, <TT
CLASS="LITERAL"
>sqlca.sqlwarn[2]</TT
> is set
to <TT
CLASS="LITERAL"
>W</TT
>. (In all other cases, it is set to
something different from <TT
CLASS="LITERAL"
>W</TT
>.) If
<TT
CLASS="LITERAL"
>sqlca.sqlwarn[1]</TT
> is set to
<TT
CLASS="LITERAL"
>W</TT
>, then a value was truncated when it was
stored in a host variable. <TT
CLASS="LITERAL"
>sqlca.sqlwarn[0]</TT
> is
set to <TT
CLASS="LITERAL"
>W</TT
> if any of the other elements are set
to indicate a warning.
</P
><P
> The fields <TT
CLASS="STRUCTFIELD"
>sqlcaid</TT
>,
<TT
CLASS="STRUCTFIELD"
>sqlcabc</TT
>,
<TT
CLASS="STRUCTFIELD"
>sqlerrp</TT
>, and the remaining elements of
<TT
CLASS="STRUCTFIELD"
>sqlerrd</TT
> and
<TT
CLASS="STRUCTFIELD"
>sqlwarn</TT
> currently contain no useful
information.
</P
><P
> The structure <TT
CLASS="VARNAME"
>sqlca</TT
> is not defined in the SQL
standard, but is implemented in several other SQL database
systems. The definitions are similar at the core, but if you want
to write portable applications, then you should investigate the
different implementations carefully.
</P
><P
> Here is one example that combines the use of <TT
CLASS="LITERAL"
>WHENEVER</TT
>
and <TT
CLASS="VARNAME"
>sqlca</TT
>, printing out the contents
of <TT
CLASS="VARNAME"
>sqlca</TT
> when an error occurs. This is perhaps
useful for debugging or prototyping applications, before
installing a more <SPAN
CLASS="QUOTE"
>"user-friendly"</SPAN
> error handler.
</P><PRE
CLASS="PROGRAMLISTING"
>EXEC SQL WHENEVER SQLERROR CALL print_sqlca();
void
print_sqlca()
{
fprintf(stderr, "==== sqlca ====\n");
fprintf(stderr, "sqlcode: %ld\n", sqlca.sqlcode);
fprintf(stderr, "sqlerrm.sqlerrml: %d\n", sqlca.sqlerrm.sqlerrml);
fprintf(stderr, "sqlerrm.sqlerrmc: %s\n", sqlca.sqlerrm.sqlerrmc);
fprintf(stderr, "sqlerrd: %ld %ld %ld %ld %ld %ld\n", sqlca.sqlerrd[0],sqlca.sqlerrd[1],sqlca.sqlerrd[2],
sqlca.sqlerrd[3],sqlca.sqlerrd[4],sqlca.sqlerrd[5]);
fprintf(stderr, "sqlwarn: %d %d %d %d %d %d %d %d\n", sqlca.sqlwarn[0], sqlca.sqlwarn[1], sqlca.sqlwarn[2],
sqlca.sqlwarn[3], sqlca.sqlwarn[4], sqlca.sqlwarn[5],
sqlca.sqlwarn[6], sqlca.sqlwarn[7]);
fprintf(stderr, "sqlstate: %5s\n", sqlca.sqlstate);
fprintf(stderr, "===============\n");
}</PRE
><P>
The result could look as follows (here an error due to a
misspelled table name):
</P><PRE
CLASS="SCREEN"
>==== sqlca ====
sqlcode: -400
sqlerrm.sqlerrml: 49
sqlerrm.sqlerrmc: relation "pg_databasep" does not exist on line 38
sqlerrd: 0 0 0 0 0 0
sqlwarn: 0 0 0 0 0 0 0 0
sqlstate: 42P01
===============</PRE
><P>
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ECPG-SQLSTATE-SQLCODE"
>33.8.3. <TT
CLASS="LITERAL"
>SQLSTATE</TT
> vs. <TT
CLASS="LITERAL"
>SQLCODE</TT
></A
></H2
><P
> The fields <TT
CLASS="LITERAL"
>sqlca.sqlstate</TT
> and
<TT
CLASS="LITERAL"
>sqlca.sqlcode</TT
> are two different schemes that
provide error codes. Both are derived from the SQL standard, but
<TT
CLASS="LITERAL"
>SQLCODE</TT
> has been marked deprecated in the SQL-92
edition of the standard and has been dropped in later editions.
Therefore, new applications are strongly encouraged to use
<TT
CLASS="LITERAL"
>SQLSTATE</TT
>.
</P
><P
> <TT
CLASS="LITERAL"
>SQLSTATE</TT
> is a five-character array. The five
characters contain digits or upper-case letters that represent
codes of various error and warning conditions.
<TT
CLASS="LITERAL"
>SQLSTATE</TT
> has a hierarchical scheme: the first
two characters indicate the general class of the condition, the
last three characters indicate a subclass of the general
condition. A successful state is indicated by the code
<TT
CLASS="LITERAL"
>00000</TT
>. The <TT
CLASS="LITERAL"
>SQLSTATE</TT
> codes are for
the most part defined in the SQL standard. The
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> server natively supports
<TT
CLASS="LITERAL"
>SQLSTATE</TT
> error codes; therefore a high degree
of consistency can be achieved by using this error code scheme
throughout all applications. For further information see
<A
HREF="errcodes-appendix.html"
>Appendix A</A
>.
</P
><P
> <TT
CLASS="LITERAL"
>SQLCODE</TT
>, the deprecated error code scheme, is a
simple integer. A value of 0 indicates success, a positive value
indicates success with additional information, a negative value
indicates an error. The SQL standard only defines the positive
value +100, which indicates that the last command returned or
affected zero rows, and no specific negative values. Therefore,
this scheme can only achieve poor portability and does not have a
hierarchical code assignment. Historically, the embedded SQL
processor for <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> has assigned
some specific <TT
CLASS="LITERAL"
>SQLCODE</TT
> values for its use, which
are listed below with their numeric value and their symbolic name.
Remember that these are not portable to other SQL implementations.
To simplify the porting of applications to the
<TT
CLASS="LITERAL"
>SQLSTATE</TT
> scheme, the corresponding
<TT
CLASS="LITERAL"
>SQLSTATE</TT
> is also listed. There is, however, no
one-to-one or one-to-many mapping between the two schemes (indeed
it is many-to-many), so you should consult the global
<TT
CLASS="LITERAL"
>SQLSTATE</TT
> listing in <A
HREF="errcodes-appendix.html"
>Appendix A</A
>
in each case.
</P
><P
> These are the assigned <TT
CLASS="LITERAL"
>SQLCODE</TT
> values:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
>0 (<TT
CLASS="SYMBOL"
>ECPG_NO_ERROR</TT
>)</DT
><DD
><P
> Indicates no error. (SQLSTATE 00000)
</P
></DD
><DT
>100 (<TT
CLASS="SYMBOL"
>ECPG_NOT_FOUND</TT
>)</DT
><DD
><P
> This is a harmless condition indicating that the last command
retrieved or processed zero rows, or that you are at the end of
the cursor. (SQLSTATE 02000)
</P
><P
> When processing a cursor in a loop, you could use this code as
a way to detect when to abort the loop, like this:
</P><PRE
CLASS="PROGRAMLISTING"
>while (1)
{
EXEC SQL FETCH ... ;
if (sqlca.sqlcode == ECPG_NOT_FOUND)
break;
}</PRE
><P>
But <TT
CLASS="LITERAL"
>WHENEVER NOT FOUND DO BREAK</TT
> effectively
does this internally, so there is usually no advantage in
writing this out explicitly.
</P
></DD
><DT
>-12 (<TT
CLASS="SYMBOL"
>ECPG_OUT_OF_MEMORY</TT
>)</DT
><DD
><P
> Indicates that your virtual memory is exhausted. The numeric
value is defined as <TT
CLASS="LITERAL"
>-ENOMEM</TT
>. (SQLSTATE
YE001)
</P
></DD
><DT
>-200 (<TT
CLASS="SYMBOL"
>ECPG_UNSUPPORTED</TT
>)</DT
><DD
><P
> Indicates the preprocessor has generated something that the
library does not know about. Perhaps you are running
incompatible versions of the preprocessor and the
library. (SQLSTATE YE002)
</P
></DD
><DT
>-201 (<TT
CLASS="SYMBOL"
>ECPG_TOO_MANY_ARGUMENTS</TT
>)</DT
><DD
><P
> This means that the command specified more host variables than
the command expected. (SQLSTATE 07001 or 07002)
</P
></DD
><DT
>-202 (<TT
CLASS="SYMBOL"
>ECPG_TOO_FEW_ARGUMENTS</TT
>)</DT
><DD
><P
> This means that the command specified fewer host variables than
the command expected. (SQLSTATE 07001 or 07002)
</P
></DD
><DT
>-203 (<TT
CLASS="SYMBOL"
>ECPG_TOO_MANY_MATCHES</TT
>)</DT
><DD
><P
> This means a query has returned multiple rows but the statement
was only prepared to store one result row (for example, because
the specified variables are not arrays). (SQLSTATE 21000)
</P
></DD
><DT
>-204 (<TT
CLASS="SYMBOL"
>ECPG_INT_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>int</TT
> and the datum in
the database is of a different type and contains a value that
cannot be interpreted as an <TT
CLASS="TYPE"
>int</TT
>. The library uses
<CODE
CLASS="FUNCTION"
>strtol()</CODE
> for this conversion. (SQLSTATE
42804)
</P
></DD
><DT
>-205 (<TT
CLASS="SYMBOL"
>ECPG_UINT_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>unsigned int</TT
> and the
datum in the database is of a different type and contains a
value that cannot be interpreted as an <TT
CLASS="TYPE"
>unsigned
int</TT
>. The library uses <CODE
CLASS="FUNCTION"
>strtoul()</CODE
>
for this conversion. (SQLSTATE 42804)
</P
></DD
><DT
>-206 (<TT
CLASS="SYMBOL"
>ECPG_FLOAT_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>float</TT
> and the datum
in the database is of another type and contains a value that
cannot be interpreted as a <TT
CLASS="TYPE"
>float</TT
>. The library
uses <CODE
CLASS="FUNCTION"
>strtod()</CODE
> for this conversion.
(SQLSTATE 42804)
</P
></DD
><DT
>-207 (<TT
CLASS="SYMBOL"
>ECPG_NUMERIC_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>numeric</TT
> and the datum
in the database is of another type and contains a value that
cannot be interpreted as a <TT
CLASS="TYPE"
>numeric</TT
> value.
(SQLSTATE 42804)
</P
></DD
><DT
>-208 (<TT
CLASS="SYMBOL"
>ECPG_INTERVAL_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>interval</TT
> and the datum
in the database is of another type and contains a value that
cannot be interpreted as an <TT
CLASS="TYPE"
>interval</TT
> value.
(SQLSTATE 42804)
</P
></DD
><DT
>-209 (<TT
CLASS="SYMBOL"
>ECPG_DATE_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>date</TT
> and the datum in
the database is of another type and contains a value that
cannot be interpreted as a <TT
CLASS="TYPE"
>date</TT
> value.
(SQLSTATE 42804)
</P
></DD
><DT
>-210 (<TT
CLASS="SYMBOL"
>ECPG_TIMESTAMP_FORMAT</TT
>)</DT
><DD
><P
> The host variable is of type <TT
CLASS="TYPE"
>timestamp</TT
> and the
datum in the database is of another type and contains a value
that cannot be interpreted as a <TT
CLASS="TYPE"
>timestamp</TT
> value.
(SQLSTATE 42804)
</P
></DD
><DT
>-211 (<TT
CLASS="SYMBOL"
>ECPG_CONVERT_BOOL</TT
>)</DT
><DD
><P
> This means the host variable is of type <TT
CLASS="TYPE"
>bool</TT
> and
the datum in the database is neither <TT
CLASS="LITERAL"
>'t'</TT
> nor
<TT
CLASS="LITERAL"
>'f'</TT
>. (SQLSTATE 42804)
</P
></DD
><DT
>-212 (<TT
CLASS="SYMBOL"
>ECPG_EMPTY</TT
>)</DT
><DD
><P
> The statement sent to the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
server was empty. (This cannot normally happen in an embedded
SQL program, so it might point to an internal error.) (SQLSTATE
YE002)
</P
></DD
><DT
>-213 (<TT
CLASS="SYMBOL"
>ECPG_MISSING_INDICATOR</TT
>)</DT
><DD
><P
> A null value was returned and no null indicator variable was
supplied. (SQLSTATE 22002)
</P
></DD
><DT
>-214 (<TT
CLASS="SYMBOL"
>ECPG_NO_ARRAY</TT
>)</DT
><DD
><P
> An ordinary variable was used in a place that requires an
array. (SQLSTATE 42804)
</P
></DD
><DT
>-215 (<TT
CLASS="SYMBOL"
>ECPG_DATA_NOT_ARRAY</TT
>)</DT
><DD
><P
> The database returned an ordinary variable in a place that
requires array value. (SQLSTATE 42804)
</P
></DD
><DT
>-220 (<TT
CLASS="SYMBOL"
>ECPG_NO_CONN</TT
>)</DT
><DD
><P
> The program tried to access a connection that does not exist.
(SQLSTATE 08003)
</P
></DD
><DT
>-221 (<TT
CLASS="SYMBOL"
>ECPG_NOT_CONN</TT
>)</DT
><DD
><P
> The program tried to access a connection that does exist but is
not open. (This is an internal error.) (SQLSTATE YE002)
</P
></DD
><DT
>-230 (<TT
CLASS="SYMBOL"
>ECPG_INVALID_STMT</TT
>)</DT
><DD
><P
> The statement you are trying to use has not been prepared.
(SQLSTATE 26000)
</P
></DD
><DT
>-239 (<TT
CLASS="SYMBOL"
>ECPG_INFORMIX_DUPLICATE_KEY</TT
>)</DT
><DD
><P
> Duplicate key error, violation of unique constraint (Informix
compatibility mode). (SQLSTATE 23505)
</P
></DD
><DT
>-240 (<TT
CLASS="SYMBOL"
>ECPG_UNKNOWN_DESCRIPTOR</TT
>)</DT
><DD
><P
> The descriptor specified was not found. The statement you are
trying to use has not been prepared. (SQLSTATE 33000)
</P
></DD
><DT
>-241 (<TT
CLASS="SYMBOL"
>ECPG_INVALID_DESCRIPTOR_INDEX</TT
>)</DT
><DD
><P
> The descriptor index specified was out of range. (SQLSTATE
07009)
</P
></DD
><DT
>-242 (<TT
CLASS="SYMBOL"
>ECPG_UNKNOWN_DESCRIPTOR_ITEM</TT
>)</DT
><DD
><P
> An invalid descriptor item was requested. (This is an internal
error.) (SQLSTATE YE002)
</P
></DD
><DT
>-243 (<TT
CLASS="SYMBOL"
>ECPG_VAR_NOT_NUMERIC</TT
>)</DT
><DD
><P
> During the execution of a dynamic statement, the database
returned a numeric value and the host variable was not numeric.
(SQLSTATE 07006)
</P
></DD
><DT
>-244 (<TT
CLASS="SYMBOL"
>ECPG_VAR_NOT_CHAR</TT
>)</DT
><DD
><P
> During the execution of a dynamic statement, the database
returned a non-numeric value and the host variable was numeric.
(SQLSTATE 07006)
</P
></DD
><DT
>-284 (<TT
CLASS="SYMBOL"
>ECPG_INFORMIX_SUBSELECT_NOT_ONE</TT
>)</DT
><DD
><P
> A result of the subquery is not single row (Informix
compatibility mode). (SQLSTATE 21000)
</P
></DD
><DT
>-400 (<TT
CLASS="SYMBOL"
>ECPG_PGSQL</TT
>)</DT
><DD
><P
> Some error caused by the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
server. The message contains the error message from the
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> server.
</P
></DD
><DT
>-401 (<TT
CLASS="SYMBOL"
>ECPG_TRANS</TT
>)</DT
><DD
><P
> The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> server signaled that
we cannot start, commit, or rollback the transaction.
(SQLSTATE 08007)
</P
></DD
><DT
>-402 (<TT
CLASS="SYMBOL"
>ECPG_CONNECT</TT
>)</DT
><DD
><P
> The connection attempt to the database did not succeed.
(SQLSTATE 08001)
</P
></DD
><DT
>-403 (<TT
CLASS="SYMBOL"
>ECPG_DUPLICATE_KEY</TT
>)</DT
><DD
><P
> Duplicate key error, violation of unique constraint. (SQLSTATE
23505)
</P
></DD
><DT
>-404 (<TT
CLASS="SYMBOL"
>ECPG_SUBSELECT_NOT_ONE</TT
>)</DT
><DD
><P
> A result for the subquery is not single row. (SQLSTATE 21000)
</P
></DD
><DT
>-602 (<TT
CLASS="SYMBOL"
>ECPG_WARNING_UNKNOWN_PORTAL</TT
>)</DT
><DD
><P
> An invalid cursor name was specified. (SQLSTATE 34000)
</P
></DD
><DT
>-603 (<TT
CLASS="SYMBOL"
>ECPG_WARNING_IN_TRANSACTION</TT
>)</DT
><DD
><P
> Transaction is in progress. (SQLSTATE 25001)
</P
></DD
><DT
>-604 (<TT
CLASS="SYMBOL"
>ECPG_WARNING_NO_TRANSACTION</TT
>)</DT
><DD
><P
> There is no active (in-progress) transaction. (SQLSTATE 25P01)
</P
></DD
><DT
>-605 (<TT
CLASS="SYMBOL"
>ECPG_WARNING_PORTAL_EXISTS</TT
>)</DT
><DD
><P
> An existing cursor name was specified. (SQLSTATE 42P03)
</P
></DD
></DL
></DIV
><P>
</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="ecpg-descriptors.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ecpg-preproc.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Using Descriptor Areas</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ecpg.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Preprocessor Directives</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|