Execute Stored Procedure from Classic ASP

青春壹個敷衍的年華 提交于 2019-12-28 13:54:49

问题


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.


回答1:


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.




回答2:


Don't forget to set the Direction to Output:

objCommandSec.Parameters(3).Direction = 2



回答3:


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


来源:https://stackoverflow.com/questions/2689736/execute-stored-procedure-from-classic-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!