FLUSH returns SQL error -391 (Help please)
Posted in 1994
Topics: Error Codes & Troubleshooting, Connectivity: ESQL/C, 4GL & Embedded SQL, Data Types & Schema Design
[This is somewhat long. I am trying to be as accurate as possible.] I am writing a program in ESQL/C that inserts data in a table in a MODE ANSI database. The columns are defined as either DECIMAL or CHAR and always NOT NULL. Since my program does not fill every column with a value, I am using a $SET DESCRIPTOR statement within a for loop to set each column to either " " (if it is a CHAR column) or "0" (if it is a DECIMAL column) in order to avoid problems with a column being NULL. After the for loop, I use other $SET DESCRIPTOR statements to fill some of the columns with meaningful data. Then I use $PUT and $FLUSH to insert the row. The problem is that sometimes the FLUSH command returns an SQL error 391. (Cannot insert a null into column column-name). Sometimes means that only one out of 21 records is inserted, sometimes 10. It is hard to pinpoint the column which causes the error because it is detected only when the record is written to the database. Here is an excerpt from my code: sprintf(sql, "INSERT INTO %s VALUES(?,?,?,...)", table_name); $PREPARE :sql_cmd FROM $sql; $DECLARE $curs CURSOR FOR :sql_cmd; $ALLOCATE DESCRIPTOR $desc WITH MAX 210; $OPEN $curs; $DESCRIBE :sql_cmd USING SQL DESCRIPTOR $desc /* insert default values to avoid problems with NULL */ $GET DESCRIPTOR $desc $ncols = COUNT; for (i=1; i<=ncols; i++) { $GET DESCRIPTOR $desc VALUE $i $type = TYPE; strcpy(data, " "); /* for CHAR columns */ strcpy(data, "0"); /* for DECIMAL columns */ $SET DESCRIPTOR $desc VALUE $i TYPE = $type, LENGTH = 2048, /* yes, "hardwired" */ INDICATOR = 0, DATA = $data; } /* Now, insert meaningful data in some of the columns */ $SET DESCRIPTOR $desc VALUE $i TYPE = 0, /* CHAR hardwired, even if col is DECIMAL */ LENGTH = 2048, INDICATOR = 0, DATA = $meaningful_data; $PUT $curs USING SQL DESCRIPTOR $desc; $FLUSH $curs; /* sometimes returns with SQL error 391 */ (SQLCODE is checked after each command. I have omitted this for the sake of brevity.) When I just set the default values and omit the second SET DESCRIPTOR command to insert the meaningful data, every row gets inserted. It seems that the second SET DESCRIPTOR fouls things up some of the time. Is it illegal or a bad idea to use $SET DESCRIPTOR more than once on a given column? Have I overlooked something obvious? Is there a better way to accomplish that what I am trying to do? I am using Informix Online 5.0 in case that matters. Any help is really appreciated. I am at the end of my wits... -Walter