可以使用OUTPUT关键字在存储过程中定义一个输出参数来返回插入的行数。然后,使用Command对象的Parameters集合将输出参数绑定到变量上,并使用Execute方法进行执行。
示例代码:
存储过程:
CREATE PROCEDURE [dbo].[InsertData] @Name nvarchar(50), @Age int, @RowCount int OUTPUT AS BEGIN SET NOCOUNT ON;
INSERT INTO [dbo].Data VALUES(@Name,@Age)
SET @RowCount = @@ROWCOUNT END
VBScript代码:
Dim objConn, objCmd, objParam Dim strName, intAge, intRowCount
'初始化 strName = "John" intAge = 30
Set objConn = Server.CreateObject("ADODB.Connection") Set objCmd = Server.CreateObject("ADODB.Command")
objConn.Open "Provider=SQLOLEDB;Data Source=myServer;Initial Catalog=myDatabase;User ID=myUsername;Password=myPassword;" objCmd.ActiveConnection = objConn objCmd.CommandType = adCmdStoredProc objCmd.CommandText = "InsertData"
'定义输入参数 objCmd.Parameters.Append objCmd.CreateParameter("@Name", adVarChar, adParamInput, 50, strName) objCmd.Parameters.Append objCmd.CreateParameter("@Age", adInteger, adParamInput, , intAge)
'定义输出参数 Set objParam = objCmd.CreateParameter("@RowCount", adInteger, adParamOutput) objCmd.Parameters.Append objParam
'执行存储过程 objCmd.Execute
intRowCount = objParam.Value '获取返回值
'清理资源 Set objParam = Nothing Set objCmd = Nothing objConn.Close Set objConn = Nothing
'Response.Write intRowCount '打印返回值