Solved

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

Posted on 2011-02-10
6
1,205 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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