Even before fetching the values into sFirstName
, you print them. To start with, as you didn't initialise it, it prints garbage value. Also if you feel the cursor fetch is interrupted, before *break*ing the LOOP, use sqlca's sqlerrm to print the error message. Like sqlca.sqlerrm.sqlerrmc
Then, your OPEN CURSOR
call would fail, since the query has syntax errors.
So, you need to either modify the cursor as below, or modify the query correctly.
We have to check the status of the OPEN cursor
, before proceeding, else FETCH
would again fail, and the results may be unpredictable. So, please check the sqlca.sqlcode
after every EXEC SQL
call.
Also, we need to handle NULL
, we can use NVL()
in queries, if Indicator variables are not used
exec sql declare dbGuest cursor for
Select G.Guest_ID,
G.Last_Name,
G.First_Name,
C.Item_ID,
C.Item_Name,
C.Quantity,
C.Unit_Price,
C.Trans_Date,
H.Hotel_Name,
H.Hotel_ID,
SI.Taxable
From Hotel H, Charge C, Stay S, Guest G, Sales_Item SI
Where C.Stay_ID=S.Stay_ID
And H.Hotel_ID=S.Hotel_ID
And G.Guest_ID=S.Guest_ID
And SI.Item_ID=C.Item_ID;
Non aggregated columns can be used only with aggregate functions. So either Remove the Grouping or add MAX()
to other columns.
Add the below to your declarations
int temp_sales_id = -999;
int first_iter = 1;
int total_nQuantity = 0;
float total_nUnitPrice = 0.0;
Then,
exec sql open dbGuest;
/* Lets check the status of the OPEN statement before proceeding , else exceptions would be suppressed */
if(sqlca.sqlcode !=0) // If anything went wrong or we read past eof, stop the loop
{
printf("Error while opening Cursor <%d><%s>\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
break;
}
for(;;)
{
//exec sql fetch dbGuest into :nGuest_ID, :sLastName, :sFirstName, :sHotelName, :sCheckInDate, :nDays, :sRoom;
exec sql fetch dbGuest into :sLastName, :sFirstName, :nItemID, :sItemName, :nQuantity, :nUnitPrice, :sTransDate, :sHotelName, :nHotelID;
/* Check for No DATA FOUND */
if(sqlca.sqlcode == 100 || sqlca.sqlcode == 1403) // If anything went wrong or we read past eof, stop the loop
{
printf("CURSOR is empty after all fetch");
break;
}
/* Check for other errors */
else if(sqlca.sqlcode != 0)
{
printf("Error while fetching from Cursor <%d><%s>\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
break;
}
if(first_iter) {
printf("%s %s %s %s %d\n","Charge Summary for:", sFirstName.arr, sLastName.arr, " Guest_ID:", nGuest_ID);
first_iter = 0;
}
// Do the crazy stuff to end the C-Strings
sLastName.arr[sLastName.len] = 0;
sFirstName.arr[sFirstName.len] = 0;
sItemName.arr[sItemName.len] = 0;
sTransDate.arr[sTransDate.len] = 0;
sHotelName.arr[sHotelName.len] = 0;
if(temp_sales_id == -999 || temp_sales_id != nItemID)
{
/* First Item or Sales Item has Changed (next sales id)*/
temp_sales_id = nItemID;
// Print out the information for this guest
printf("%s %d %s %s \n", "Sales_Item: ", nItemID, " - ", sItemName.arr);
printf("%d %s %s %d %d \n", nHotelID, " ", sHotelName.arr, " ",sTransDate.arr, " ", nQuantity, " ", nUnitPrice);
total_nQuantity += nQuantity;
total_nUnitPrice += nUnitPrice;
}
if (temp_sales_id != nItemID) {
/* Printing total for Current Sale id */
/* If you want to Sum all the sale id together take this finally */
printf("Total Quantity <%d> Total Extended Price <%g>\n",total_nQuantity,total_nUnitPrice);
total_nUnitPrice = 0;
total_nQuantity = 0;
}
if(temp_sales_id == -999 || temp_sales_id == nItemID) {
printf("%d %s %s %d %d \n", nHotelID, " ", sHotelName.arr, " ",sTransDate.arr, " ", nQuantity, " ", nUnitPrice);
}
}
// close the cursor and end the program
exec sql close dbGuest ;