Jump to content

SQL query error in C code - please assist

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
rakabos

rakabos

    Newbie

  • Members
  • Pip
  • 4 posts
Hi guys

I have a C program that runs a SQL query and prints the information to a text file. Now the select looks at about 30 columns or so and prints fine except for one of the entries.

Column 6 contains 20 characters, and column 7 contains 20 characters.

Now instead of printing it like that, it prints out the first 14 characters for column 6 and stops there, then column 7 is printed with the last 6 characters from column 6 and then by the characters from column 7.

When i run the select using normal sql, it works fine, but using the C program this happens. Everything is declared in the code using 20 characters.

Anyone ever come accross this?

Cheers

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Hey rakabos, it would help us greatly to see the code you are using to execute your SQL query.

#3
rakabos

rakabos

    Newbie

  • Members
  • Pip
  • 4 posts
It's a lot but here goes:

1. The sql query
sprintf(sSQL,"SELECT CMF.account_no, " 
		     "CMF.hierarchy_id, " 
		     "rtrim(CIAM.external_id), " 
		     "CMF.currency_code, " 
		     "CMF.language_code, " 
		     "CMF.account_category, " 
		     "CMF.bill_period, " 
		     "CMF.pay_method, " 
		     "CMF.bill_fmt_opt, " 
		     "CMF.bill_disp_meth, " 
		     "CMF.sales_code, " 
		     "CMF.bill_name_pre, " 
		     "CMF.bill_fname, " 
		     "CMF.bill_minit, " 
		     "CMF.bill_lname, " 
		     "CMF.bill_title, " 
		     "CMF.bill_address1, " 
		     "CMF.bill_address2, " 
		     "CMF.bill_city, " 
		     "CMF.bill_state, " 
		     "CMF.bill_zip, " 
		     "CMF.bill_country_code, " 
		     "CMF.cust_address1, " 
		     "CMF.cust_address2, " 
		     "CMF.cust_address3, " 
		     "CMF.cust_city, " 
		     "CMF.cust_state, " 
		     "CMF.cust_zip, " 
		     "CMF.cust_country_code, " 
		     "CMF.bill_franchise_tax_code, " 
		     "CMF.cust_bank_sort_code, " 
		     "CMF.cust_bank_acc_num, " 
		     "CMF.cust_bank_acc_name, " 
		     "CMF.bank_agency_code, " 
		     "TO_CHAR(BIS.prev_cutoff_date, 'YYYYMMDD HH24:MI:SS'), " 
		     "TO_CHAR(BIS.cutoff_date, 'YYYYMMDD HH24:MI:SS'), " 
		     "TO_CHAR(BIS.statement_date, 'YYYYMMDD HH24:MI:SS'), " 
		     "TO_CHAR(BIS.prep_date, 'YYYYMMDD HH24:MI:SS'), " 
		     "TO_CHAR(BIS.payment_due_date, 'YYYYMMDD HH24:MI:SS'), " 
		     "BIS.bill_ref_no, " 
		     "BIS.bill_ref_resets, " 
		     "BIS.ext_bill_ref_no, " 
		     "BIS.ext_bill_ref_resets, " 
		     "BIS.bill_num_range_code, " 
		     "BIS.include_nrc, " 
		     "BIS.include_rc, " 
		     "BIS.include_adj, " 
		     "BIS.include_usage, " 
		     "BIS.include_bmf, " 
		     "BIS.interim_bill_flag, " 
		     "CMF.bill_hold_code, " 
		     "TO_CHAR( CMF.date_active, 'YYYYMMDD HH24:MI:SS'), " 
		     "TO_CHAR( CMF.date_inactive, 'YYYYMMDD HH24:MI:SS'), " 
		     "FCJ.order_by, " 
		     "FCJ.agency_code, " 
		     "rtrim(FCJ.pay_day), " 
		     "FCJ.robinson_flag, " 
		     "CMF.contact1_name, " 
		     "FCJ.no_factura, " 
		     "FCJ.f_enmascarada_flag, " 
		     "FCJ.f_domiciliacion_bloqueada, " 
             "CMF.default_ccard_id, " 
             "CMF.default_ccard_id_serv, " 
		     "FCJ.enmascarada_num, " 
		     "CMF.cust_phone1, "  
		     "CMF.cust_email, " 
		     "FCJ.vat_number, "		      
		     "FCJ.print_vat_number_flag, "	
		     "CMF.ssn, " 	
		     "CMF.bill_company " 	        
		     "FROM CMF,CUSTOMER_ID_ACCT_MAP CIAM,FC_BILL_INVOICE_SEQUENTIAL BIS,FC_CMF_JOIN FCJ " 
		     "WHERE BIS.ext_bill_ref_no = %d AND " 
		     "BIS.ext_bill_ref_resets = %d AND " 
		     "BIS.bill_num_range_code = %d AND " 
		     "BIS.account_no = CMF.account_no AND " 
		     "CMF.account_no = CIAM.account_no AND " 
		     "CMF.account_no = FCJ.account_no AND " 
		     "CIAM.external_id_type = 2 AND " 
		     "CIAM.is_current = 1", 
		      ext_bill_ref_no,ext_bill_ref_resets,bill_num_range_code );

2. The printout
sprintf( lsBuffer,
		"%-5.5s%12.12s%3.3s%-6.6s%-48.48s%-24.24s"
	        "%6d%-12.12s  %08.08d%-5.5s%-79.79s%-20.20s%-8.8s  %6d%-8.8s%-8.8s"
		"%-75.75s%-75.75s%-75.75s%-26.26s%-28.28s%-16.16s%6d"
		"%-75.75s%-75.75s%-75.75s%-26.26s%-28.28s%-16.16s%6d"
		"%6d%c%2d%-10.10s%-100.100s%6d%-40.40s%c%16.16s%2.2s%-56.56s%d%-60.60s%-20.20s%-2.2d%-75.75s"
		"%-15.15s%d"
		"\n",
		lpFactura->bill_zip,
		sCodigoOrdenacion2,
		sCodigoRegistro,
		"",
		"",
		"",
		lpFactura->numero_lineas_de_negocio,
		sNumeroFactura,
      		atoi(lpFactura->external_id),
		lpFactura->bill_name_pre,
		sStringaGenerica,
		lpFactura->ssn,
		lsFecha_emision,
		LUGAR_EMISION_FACTURA,
		lsFecha_inicio,
		lsFecha_fin,
		lpFactura->bill_address1,
		lpFactura->bill_address2,
		lsCargo,
		lpFactura->bill_city,
		lpFactura->bill_state,
		lpFactura->bill_zip,
		lpFactura->bill_country_code,
		lpFactura->cust_address1,
		lpFactura->cust_address2,
		lpFactura->cust_address3,
		lpFactura->cust_city,
		lpFactura->cust_state,
		lpFactura->cust_zip,
		lpFactura->cust_country_code,
		lpFactura->currency_code,
		lcFormatoFactura,
		lpFactura->bill_fmt_opt,
		"",
		"",
		lpFactura->language_code,
		lpFactura->bill_title,
		lcTipoDocumento,
		Fen_Isa_Format_moneda((lpFactura->TotalFactura + lpFactura->TotalImpuestos), lpFactura->implied_decimal, lsTotalFactura),
		PREFIJO_NUMERO_FACTURA,/*** SAP Preffix for account_number ***/
		lsBill_company, 
		lpFactura->robinson_flag,
		sNombreCliente,
		lpFactura->cust_phone1,
 		lpFactura->bill_disp_meth,
        lpFactura->cust_email,
        lpFactura->VAT_number,
        lpFactura->print_vat_number_flag);


So from the above "ssn" is supposed to print a person's social security nr, and bill_company prints the company's name...

Here is what my output looks like however

ssn: 861213081408Khume's
bill_company: Khume's tuck shop

It should be:
ssn: 8612130814088
bill_company: Khume's tuck shop

So why would a print out mix up the two columns, all the other columns print ok...

Edited by Jaan, 14 September 2009 - 04:25 AM.
Please use code tags when you are posting your codes!


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Does Social security nr have a null termination?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
rakabos

rakabos

    Newbie

  • Members
  • Pip
  • 4 posts
Does the ssn have a null termination - im not 100% sure, is this set in the database?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Possibly. It could be set by your database connection library, too.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
rakabos

rakabos

    Newbie

  • Members
  • Pip
  • 4 posts
So any suggestions plz?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Create a short validation function to verify that all strings are null-terminated at the length they are expected to be.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog