Solved

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

Posted on 2011-02-10
6
1,197 Views
Last Modified: 2012-05-11
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
Comment
Question by:rbhargaw
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 34866792
Could you post the declaration of 'activityRecordData'?
0
 

Author Comment

by:rbhargaw
ID: 34866835
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
 

Accepted Solution

by:
rbhargaw earned 0 total points
ID: 34866857
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Expert Comment

by:jkr
ID: 34866918
Weird... but if it works.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34869881
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
 

Author Closing Comment

by:rbhargaw
ID: 34949705
I added the single quote around '%s' and the solution worked
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now