Wednesday, March 21, 2012

Problem passing variables as parameters to extended stored procedure

Hello all,

I have written an XP for SQL Server 2000 SP2. It performs as expected if I call the XP with literal values for the parameters, however when I wrap the XP call into a regular stored procedure, only the first character of each input string is seen by the XP! Here are the relevant code snippets:

C++ Extended Stored Procedure:

(Basically all this code is doing is retrieving the parameters and printing them back out)

srv_paraminfo(srvproc, 1, &bType, &uMaxLen, &uLen, NULL, &bNull);

param1 = new BYTE[uLen + 1];

srv_paraminfo(srvproc, 1, &bType, &uMaxLen, &uLen, param1, &bNull);

param1[uLen] = '\0';

srv_paraminfo(srvproc, 2, &bType, &uMaxLen, &uLen, NULL, &bNull);

param2 = new BYTE[uLen + 1];

srv_paraminfo(srvproc, 2, &bType, &uMaxLen, &uLen, param2, &bNull);

param2[uLen] = '\0';

srv_paraminfo(srvproc, 3, &bType, &uMaxLen, &uLen, NULL, &bNull);

param3 = new BYTE[uLen + 1];

srv_paraminfo(srvproc, 3, &bType, &uMaxLen, &uLen, param3, &bNull);

param3[uLen] = '\0';

sprintf(msgText, "Params received by xp: %s, %s, %s", param1, param2, param3);

srv_sendmsg( srvproc, SRV_MSG_ERROR, 0,(DBTINYINT)0, (DBTINYINT)0,NULL,0,0,msgText,SRV_NULLTERM);

srv_senddone(srvproc, SRV_DONE_ERROR, (DBUSMALLINT)0, (DBINT)0);

Calling the XP with literal values:

EXECxp_mytest 'one','two','three'

Output:

Params received by xp: one, two, three

Calling XP via a stored procedure:

create procedure sp_mytest

(

@.myvar1 nvarchar(200),

@.myvar2 nvarchar(50),

@.myvar3 nvarchar(50)

)

as BEGIN

PRINT @.myvar1

PRINT @.myvar2

PRINT @.myvar3

EXECxp_mytest @.myvar1, @.myvar2, @.myvar3

END

EXEC sp_mytest 'one','two','three'

Output:

one

two

three

Params received by xp: o,t,t

Any insight or assistance is greatly appreciated!!!

Your definition of sp_mytest implies that its input parameters are not unicode. The first 0 character implies end-of-string. You pass unicode parameters to it from within sp_mytest (you declare them as nvarchar), and these characters are 2-byte with second byte zero, that's why you get back only the first character from the string.

Either change your sp_mytest definition to use varchar instead of nvarchar, or your xp_mytest definition to use wchar instead of char.

No comments:

Post a Comment