定义执行存储过程的参数 - 经典ASP

时间:2014-05-19 20:14:07

标签: sql-server stored-procedures asp-classic jscript

我正在尝试更新一些旧功能。存储过程具有一个输入参数和两个输出参数。我尝试执行代码时收到以下错误:

Microsoft OLE DB Provider for ODBC Drivers error '80040e21'

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

/student.asp, line 30

存储过程:

ALTER PROCEDURE [Api].[GetKeyByAuthId]
    @AuthenticationId uniqueidentifier
,   @Key int = NULL OUTPUT
,   @type varchar(25) = NULL OUTPUT

直接通话效果很好:

USE [Development]
GO

DECLARE @return_value int,
        @Key int,
        @type varchar(25)

EXEC    @return_value = [Api].[GetKeyByAuthId]
        @AuthenticationId = '0550F579-DBDA-4C41-82B3-453841A6232E',
        @Key = @Key OUTPUT,
        @type = @type OUTPUT

SELECT  @Key as N'@Key',
        @type as N'@type'

SELECT  'Return Value' = @return_value

GO

身份验证ID是唯一标识符,通过页面上的查询字符串接收。这是我的代码,第30行是cmd.Execute:

var cmd = Server.CreateObject("ADODB.Command");
cmd.CommandText = "Api.GetKeyByAuthId";
cmd.CommandType = 4;   // Stored procedure


var p = cmd.Parameters;
p.Append(cmd.CreateParameter("@AuthenticationId", 72, 1));
p.Append(cmd.CreateParameter("@Key", 3, 2, 9));
p.Append(cmd.CreateParameter("@type", 200, 2, 25));

cmd("@AuthenticationId") = aid;

cmd.ActiveConnection = Conn;
cmd.Execute();

Key = cmd.Parameters("@Key");
type = cmd.Parameters("@type");

3 个答案:

答案 0 :(得分:1)

comments

中讨论您的数据类型后

您提到存储过程期望@KeyInt,但该字段的基础表数据类型为BigInt

  

警告

     

这种方法的问题在于从BigIntInt的转换是危险的(请参阅SQL - safely downcast BIGINT to INT)。大多数时候你会得到

     
Msg 8115, Level 16, State 2, Line 5
Arithmetic overflow error converting expression to data type int.
     

您可以使用

的组合来抑制异常      
SET ANSI_WARNINGS OFF -- Divide-by-zero and arithmetic overflow errors cause null values to be returned
SET ARITHABORT OFF    -- Don't terminate the query on Divide-by-zero and arithmetic overflow errors
SET ARITHIGNORE ON    -- Suppress errors from Divide-by-zero and arithmetic overflow errors

我的猜测是

Multiple-step OLE DB operation generated errors
您的存储过程中的

错误是由Arithetic overflow error引起的,因为您在@Key中检索的值太大而无法容纳Int数据类型。

如果可能,我会与您的开发人员交谈并解释情况,看看是否可以修改存储过程以将@Key作为BigInt传递。

答案 1 :(得分:0)

此时这两项修改对我有用:

p.Append(cmd.CreateParameter("@AuthenticationId", 200, 1, 37));

var KeyDb = cmd.CreateParameter("@Key", 131, 2);
KeyDb.NumericScale = 0;
KeyDb.Precision = 9;
p.Append(KeyDb);

答案 2 :(得分:-1)

你不能像这样执行吗?

var1 = ""
var1 = var1 & "USE [Development]"
var1 = var1 & " " & vbCrLf
var1 = var1 & "go"
var1 = var1 & " " & vbCrLf
var1 = var1 & "DECLARE @return_value INT, " & vbCrLf
var1 = var1 & "        @Key          INT, " & vbCrLf
var1 = var1 & "        @type         VARCHAR(25)"
var1 = var1 & " " & vbCrLf
var1 = var1 & "EXEC @return_value = [Api].[Getkeybyauthid] " & vbCrLf
var1 = var1 & "  @AuthenticationId = '0550F579-DBDA-4C41-82B3-453841A6232E', " & vbCrLf
var1 = var1 & "  @Key = @Key output, " & vbCrLf
var1 = var1 & "  @type = @type output"
var1 = var1 & " " & vbCrLf
var1 = var1 & "SELECT @Key  AS N'@Key', " & vbCrLf
var1 = var1 & "       @type AS N'@type'"
var1 = var1 & " " & vbCrLf
var1 = var1 & "SELECT 'Return Value' = @return_value"
var1 = var1 & " " & vbCrLf
var1 = var1 & "go "
cmd.execute(var1)