views:

225

answers:

3

For some fantastic reason I find myself debugging a problem in a Classic ASP page (at least 10 years of my life lost in the last 2 days).

I'm trying to execute a stored procedure which contains some OUT parameters. The problem is that one of the OUT parameters is not being populated when the stored procedure returns. I can execute the stored proc from SQL management studio (this is 2008) and all the values are being set and returned exactly as expected.

declare @inVar1 varchar(255)
declare @inVar2 varchar(255)
declare @outVar1 varchar(255)
declare @outVar2 varchar(255)

SET @inVar2  = 'someValue'

exec theStoredProc @inVar1 , @inVar2 , @outVar1 OUT, @outVar2 OUT

print '@outVar1=' + @outVar1
print '@outVar2=' + @outVar2 

Works great. Fantastic. Perfect. The exact values that I'm expecting are being returned and printed out.

Right, since I'm trying to debug a Classic ASP page I copied the code into a VBScript file to try and narrow down the problem.

Here is what I came up with:

Set Conn = CreateObject("ADODB.Connection")
Conn.Open "xxx"

Set objCommandSec = CreateObject("ADODB.Command")
objCommandSec.ActiveConnection = Conn

objCommandSec.CommandType = 4
objCommandSec.CommandText = "theStoredProc "

objCommandSec.Parameters.Refresh

objCommandSec.Parameters(2) = "someValue"

objCommandSec.Execute

MsgBox(objCommandSec.Parameters(3))

Doesn't work. Not even a little bit. (Another ten years of my life down the drain) The third parameter is simply NULL - which is what I'm experiencing in the Classic ASP page as well.

Could someone shed some light on this? Am I completely daft for thinking that the classic ASP code would be the same as the VBScript code? I think it's using the same scripting engine and syntax so I should be ok, but I'm not 100% sure.

The result I'm seeing from my VBScript is the same as I'm seeing in ASP.

A: 

Don't forget to set the Direction to Output:

objCommandSec.Parameters(3).Direction = 2
Oded
Shouldn't the Refresh call do this automatically? Anyways it didn't make a difference
Jaco Pretorius
A: 

Another solution is here.

Set Conn = CreateObject("ADODB.Connection")
Conn.Open "xxx"

Set objCommandSec = CreateObject("ADODB.Command")
objCommandSec.ActiveConnection = Conn
objCommandSec.CommandType = 4
objCommandSec.CommandText = "theStoredProc "

objCommandSec.Parameters.Refresh

objCommandSec.parameters.append objCommandSec.createParameter("@inVar1", adVarChar, adParamInput, 255, "someValue1")
objCommandSec.parameters.append objCommandSec.createParameter("@inVar2", adVarChar, adParamInput, 255, "someValue2")
objCommandSec.parameters.append objCommandSec.createParameter("@outVar1", adVarChar, adParamReturnValue)
objCommandSec.parameters.append objCommandSec.createParameter("@outVar2", adVarChar, adParamReturnValue)
objCommandSec.execute , , adExecuteNoRecords
outVar1 = objCommandSec.parameters("@outVar1").value
outVar2 = objCommandSec.parameters("@outVar2").value

response.write outVar1
response.write outVar2
Alex Park
I tried this - I keep getting the message "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another."The parameter name, type, length, precision, scale and order are as follows (according to the db):@inVar1 int 4 10 0 1@inVar2 varchar 200 200 NULL 2@outVar1 int 4 10 0 3@outVar2 varchar 50 50 NULL 4
Jaco Pretorius
Therefore I used the following codeTherefore I used the following codeobjCommandSec.Parameters.append objCommandSec.CreateParameter("@inVar1",adInteger,adParamInput)objCommandSec.Parameters.append objCommandSec.CreateParameter("@inVar2",adVarChar,adParamInput,200, "load_test_10")objCommandSec.Parameters.append objCommandSec.CreateParameter("@outVar1",adInteger,adParamReturnValue)objCommandSec.Parameters.append objCommandSec.CreateParameter("@outVar2",adVarChar,adParamReturnValue, 50)
Jaco Pretorius
Ok, epic fail on comment formatting
Jaco Pretorius
+3  A: 

Try

With objCommandSec
 Set .ActiveConnection = Conn
 .CommandType = 4
 .CommandText = "theStoredProc"
 .Parameters.Append .CreateParameter("@inVar1", 200, 1, 255, VALUE1)
 .Parameters.Append .CreateParameter("@inVar2", 200, 1, 255, VALUE2)
 .Parameters.Append .CreateParameter("@outVar1", 200, 2, 255)
 .Parameters.Append .CreateParameter("@outVar2", 200, 2, 255)

 .Execute

 Response.Write .Parameters(3).Value
End With 

You should also avoid .Refresh if you know the parameter details as it involves a trip back to the server.

Alex K.
Return value is still blank :-(
Jaco Pretorius
This works for me: http://pastebin.com/RsqwzJ02 (from a .VBS)Are you perhaps running this after an On Error Resume Next ?
Alex K.
That example works for me, but if I modify the types of the variables in the stored proc it doesn't work - how would you access the one I posted there? http://pastebin.com/qr2jVHSA
Jaco Pretorius
Wow, the actual problem was with permissions on the stored proc, but your example helped me figure it out. Thanks
Jaco Pretorius
for ints the type is adinteger (3) instead of advarchar (200) and the size is 4 (4 bytes in a 32bit integer) so; .Parameters.Append .CreateParameter("@inVar1", 3, 1, 4, 123456) .Parameters.Append .CreateParameter("@outVar1", 3, 2, 4)
Alex K.