Solved

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

Posted on 2011-02-10
6
1,202 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 86

Expert Comment

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

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 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