• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1269
  • Last Modified:

[Microsoft][SQL Native Client]Invalid character value for cast specification

I have an old code in VC++ and MFC which is creating command to call a stored procedure like this:

CString spText;
      spText.Format ( "{call GetMailData(%d,%d,%s ,%s,%d)}", activityRecordData.entityNumber,
            activityRecordData.rttiTypeID, activityRecordData.addrTypeID, activityRecordData.corrMethID,
            activityRecordData.docGroupID)

Now while executing I am receiving error  "[Microsoft][SQL Native Client]Invalid character value for cast specification" even though the SP works fine in SQL Client.

The reason I think is %s because if I remove %s variables from the code and comment varchar parameters in Stored procedure, it runs properly. Any way to pass the string variables properly?





To run the SP:

GetMailData 3040047,10,HOME,SMTP,3060

notice that I am not passing HOME and SMTP in quotes..

ALTER PROCEDURE [dbo].[GetMailData]  
   @EntNo int/* IN must - entity Number*/,
   @rttiTypeID int/* IN must - entity type*/,
   @addrTypeID varchar(4)/* IN optional, address type Id (Home, Offc, etc.)*/,
   @corrMethID varchar(4)/* IN optional, correspondence method ID*/,
   @docGroupNo int/* IN must - document group number*/
WITH RECOMPILE

Open in new window

0
rbhargaw
Asked:
rbhargaw
  • 3
  • 2
1 Solution
 
jkrCommented:
Could you post the declaration of 'activityRecordData'?
0
 
rbhargawAuthor Commented:
const t_activityRecordData &activityRecordData

typedef struct {
      int holdingID;
      int refHoldingID;
      int refHoldingType;
      int docGroupID;
      char spName [MAXBUFLEN];
      int entityNumber;
      short rttiTypeID;
      char addrTypeID [CODE_LEN+1];
      char corrMethID [CODE_LEN+1];
} t_activityRecordData;
0
 
rbhargawAuthor Commented:
Hey jkr,

 I think I got this working..I tried this command by adding single quotes around ' %s' and it worked..WTH :)

 "{call GetMailData(%d,%d,'%s' ,'%s',%d)}"

and the debug watch shows this call {GetMailData(3040047,10,'HOME' ,'MAIL',3060)}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
Weird... but if it works.
0
 
sarabandeCommented:
string literals always need to be quoted.

you need to make sure that the strings don't have a single quote ' character in the text. if so they must be escaped by doubling them.

Sara
0
 
rbhargawAuthor Commented:
I added the single quote around '%s' and the solution worked
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now