Does ADODB fail on output parameters with optional input parameters?

霸气de小男生 提交于 2019-12-10 23:15:29

问题


I have a stored procedure in a SQL Server 2008 R2 database with an optional input parameter and an output parameter like this:

CREATE PROCEDURE [dbo].[spCreateTicket]
(
    @TrackingCode varchar(25),
    @EmailAddress varchar(250) = null,
    @Ticket varchar(1000),
    @UserID int,
    @TicketID int output
)
AS
    SET NOCOUNT ON

    INSERT INTO dbTicket (TrackingCode, EmailAddress, Ticket, UserID)
        SELECT 
            @TrackingCode, @EmailAddress, @Ticket, @UserID

    SELECT @TicketID = SCOPE_IDENTITY()

    RETURN @TicketID

When I invoke the stored procedure without the optional parameter in SSMS it works fine:

DECLARE @TicketID int

EXEC [dbo].[spCreateTicket]
    @TrackingCode = 'xOCDUv289u403k5h24s5869vK',
    @Ticket = 'Something broke!',
    @UserID = 64307,
    @TicketID = @TicketID OUTPUT

But when I try the same thing through ADODB in ASP classic like this:

SET cmd = Server.CreateObject ("ADODB.Command")
cmd.ActiveConnection = HelpDeskConnection
cmd.CommandText = "spCreateTicket"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Append cmd.CreateParameter ("@TrackingCode",adVarChar,adParamInput,25,RandomString(25))
cmd.Parameters.Append cmd.CreateParameter ("@Ticket",adVarChar,adParamInput,1000,Ticket)
cmd.Parameters.Append cmd.CreateParameter ("@UserID",adInteger,adParamInput, ,Session("UserID"))
cmd.Parameters.Append cmd.CreateParameter ("@TicketID",adInteger,adParamOutput)
cmd.Execute
TicketID = cmd.Parameters("@TicketID")

it does nothing, @TicketID is left Empty, and the ADODB.Connection contains this NativeError 8162:

"The formal parameter \"@UserID\" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output."

I use ADODB to call stored procedures without optional parameters all the time. Is this a known defect in ADODB, or do I need to do something particular to get it to work when there's an output parameter?


回答1:


Turns out the ADODB default is to ignore the parameter names and simply pass them to the sproc in the order appended.

Hence, in the example given, @Ticket was passed to the sproc as @EmailAddress, @UserID was passed as @Ticket, and no output parameter was passed at all!

If you want ADODB to match parameters by name you have to set the ADODB.Command .NamedParameters = True.



来源:https://stackoverflow.com/questions/39185679/does-adodb-fail-on-output-parameters-with-optional-input-parameters

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