Return 来自 SQL 服务器存储过程的数据使用 ASP 页面
Return data from SQL Server Stored Proc using ASP page
我有一个 .asp 页面,我正在向其传递数据,需要将信息发送到 SQL 服务器存储过程,获取 return 值,然后将其传递回主页。我目前有发送数据的页面并且可以验证这一点,因为存储过程是一个插入并且 select。所以我看到正在插入数据。出于某种原因,select 部分没有传回页面。
这是 asp 页面正在将数据发送到的存储过程:
ALTER PROCEDURE [dbo].[Insertpurchase]
@UserID bigint,
@purchaseID bigint,
@MemberID bigint,
@DependentID bigint,
@ServiceDate varchar(20),
@purchaseDate varchar(20),
@purchaseNumber bigint,
@Amount varchar(20),
@Status varchar(20)
AS
DECLARE @purchaseNo bigint
SET @purchaseNo = (SELECT [LastNumber] FROM [purchaseAdmin] where [id] = @purchaseID );
DECLARE @day varchar(20)
DECLARE @month varchar(20)
DECLARE @year varchar(20)
DECLARE @ServiceOFDate varchar(20)
DECLARE @purchaseOFDate varchar(20)
set @ServiceOFDate = SUBSTRING(@ServiceDate, 1, 2)+'/'+SUBSTRING(@ServiceDate, 3, 2)+'/'+SUBSTRING(@ServiceDate, 5, 4);
set @purchaseOFDate = SUBSTRING(@purchaseDate, 1, 2)+'/'+SUBSTRING(@purchaseDate, 3, 2)+'/'+SUBSTRING(@purchaseDate, 5, 4);
insert into [NewPurchase] (
[UserID]
,[purchaseID]
,[MemberID]
,[DependentID]
,[ServiceDate]
,[purchaseDate]
,[purchaseNumber]
,[Amountpurchase]
,[Status]
) values ( @UserID, @purchaseID , @MemberID , @DependentID , CONVERT(datetime,@ServiceOFDate) , CONVERT(datetime,@purchaseOFDate) , (@purchaseNo+1) , @Amount , @Status ) ;
select (@purchaseNo+1) as purchases
这是 asp 页面。我正在使用此线程“Using Stored Procedure in Classical ASP .. execute and get results”
中“Lankymart”的代码
<%
Dim conn_string, row, rows, ary_data
conn_string = "Provider=SQLOLEDB; Data Source = (Local) ; Initial Catalog = Testonline ; User Id = sa; Password=Test123"
Set objCommandSec = CreateObject("ADODB.Command")
With objCommandSec
.ActiveConnection = conn_string
.CommandType = 4
.CommandText = "dbo.Insertpurchase"
.Parameters.Append .CreateParameter("@UserID", 200, 1, 10, 1)
.Parameters.Append .CreateParameter("@purchaseID", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@MemberID", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@DependentID", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@ServiceDate", 200, 1, 10, "01012020")
.Parameters.Append .CreateParameter("@purchaseDate", 200, 1, 50, "01012020")
.Parameters.Append .CreateParameter("@purchaseNumber", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@Amount", 200, 1, 50, "")
.Parameters.Append .CreateParameter("@Status", 200, 1, 50, "")
Set rs = .Execute()
If Not rs.EOF Then ary_data = rs.GetRows()
Call rs.Close()
Set rs = Nothing
End With
Set objCommandSec = Nothing
'Command and Recordset no longer needed as ary_data contains our data.
If IsArray(ary_data) Then
' Iterate through array
rows = UBound(ary_data, 2)
For row = 0 to rows
objOutFile.WriteLine row
' Return our row data
' Row N column 2 (index starts from 0)
Call Response.Write(ary_data(0, row) & "")
Next
Else
' Nothing returned
Call Response.Write("No data returned")
End If
%>
我需要 asp 页面使用存储过程 select 中的数据“Response.Write”,但它似乎没有获取数据。任何帮助将不胜感激。
谢谢
存储过程中有两个语句,每个语句 return 一个“结果”。 insert 语句的结果不会是 table 数据,而是一条说明插入了多少行的消息。当你调用Set rs = .Execute()
时,rs
的值将包含第一个“结果”,这将是insert
给出的结果,而不是select
给出的结果.
有两种方法可以解决这个问题。第一个是告诉 SQL 不要 return 插入语句的“结果”,这样你唯一得到的就是 table 来自 select 的数据陈述。您可以通过将 set nocount on
语句添加到过程的开头来执行此操作。
您也可以在程序的开头添加一个 begin
,在过程的结尾也添加一个 end
,这是一个常见的模式(但不是必需的,所以如果您不喜欢也没关系):
ALTER PROCEDURE [dbo].[Insertpurchase]
@UserID bigint,
@purchaseID bigint,
@MemberID bigint,
@DependentID bigint,
@ServiceDate varchar(20),
@purchaseDate varchar(20),
@purchaseNumber bigint,
@Amount varchar(20),
@Status varchar(20)
AS
begin
set nocount on; -- < tell SQL not to return the "rows affected"
DECLARE @purchaseNo bigint
-- the rest of your procedure code
end
或者,您可以通过使用 rs.NextRecordSet
请求下一组结果来告诉记录集您不需要第一个结果
只做其中之一,不能同时做!
我有一个 .asp 页面,我正在向其传递数据,需要将信息发送到 SQL 服务器存储过程,获取 return 值,然后将其传递回主页。我目前有发送数据的页面并且可以验证这一点,因为存储过程是一个插入并且 select。所以我看到正在插入数据。出于某种原因,select 部分没有传回页面。
这是 asp 页面正在将数据发送到的存储过程:
ALTER PROCEDURE [dbo].[Insertpurchase]
@UserID bigint,
@purchaseID bigint,
@MemberID bigint,
@DependentID bigint,
@ServiceDate varchar(20),
@purchaseDate varchar(20),
@purchaseNumber bigint,
@Amount varchar(20),
@Status varchar(20)
AS
DECLARE @purchaseNo bigint
SET @purchaseNo = (SELECT [LastNumber] FROM [purchaseAdmin] where [id] = @purchaseID );
DECLARE @day varchar(20)
DECLARE @month varchar(20)
DECLARE @year varchar(20)
DECLARE @ServiceOFDate varchar(20)
DECLARE @purchaseOFDate varchar(20)
set @ServiceOFDate = SUBSTRING(@ServiceDate, 1, 2)+'/'+SUBSTRING(@ServiceDate, 3, 2)+'/'+SUBSTRING(@ServiceDate, 5, 4);
set @purchaseOFDate = SUBSTRING(@purchaseDate, 1, 2)+'/'+SUBSTRING(@purchaseDate, 3, 2)+'/'+SUBSTRING(@purchaseDate, 5, 4);
insert into [NewPurchase] (
[UserID]
,[purchaseID]
,[MemberID]
,[DependentID]
,[ServiceDate]
,[purchaseDate]
,[purchaseNumber]
,[Amountpurchase]
,[Status]
) values ( @UserID, @purchaseID , @MemberID , @DependentID , CONVERT(datetime,@ServiceOFDate) , CONVERT(datetime,@purchaseOFDate) , (@purchaseNo+1) , @Amount , @Status ) ;
select (@purchaseNo+1) as purchases
这是 asp 页面。我正在使用此线程“Using Stored Procedure in Classical ASP .. execute and get results”
中“Lankymart”的代码<%
Dim conn_string, row, rows, ary_data
conn_string = "Provider=SQLOLEDB; Data Source = (Local) ; Initial Catalog = Testonline ; User Id = sa; Password=Test123"
Set objCommandSec = CreateObject("ADODB.Command")
With objCommandSec
.ActiveConnection = conn_string
.CommandType = 4
.CommandText = "dbo.Insertpurchase"
.Parameters.Append .CreateParameter("@UserID", 200, 1, 10, 1)
.Parameters.Append .CreateParameter("@purchaseID", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@MemberID", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@DependentID", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@ServiceDate", 200, 1, 10, "01012020")
.Parameters.Append .CreateParameter("@purchaseDate", 200, 1, 50, "01012020")
.Parameters.Append .CreateParameter("@purchaseNumber", 200, 1, 50, 1)
.Parameters.Append .CreateParameter("@Amount", 200, 1, 50, "")
.Parameters.Append .CreateParameter("@Status", 200, 1, 50, "")
Set rs = .Execute()
If Not rs.EOF Then ary_data = rs.GetRows()
Call rs.Close()
Set rs = Nothing
End With
Set objCommandSec = Nothing
'Command and Recordset no longer needed as ary_data contains our data.
If IsArray(ary_data) Then
' Iterate through array
rows = UBound(ary_data, 2)
For row = 0 to rows
objOutFile.WriteLine row
' Return our row data
' Row N column 2 (index starts from 0)
Call Response.Write(ary_data(0, row) & "")
Next
Else
' Nothing returned
Call Response.Write("No data returned")
End If
%>
我需要 asp 页面使用存储过程 select 中的数据“Response.Write”,但它似乎没有获取数据。任何帮助将不胜感激。 谢谢
存储过程中有两个语句,每个语句 return 一个“结果”。 insert 语句的结果不会是 table 数据,而是一条说明插入了多少行的消息。当你调用Set rs = .Execute()
时,rs
的值将包含第一个“结果”,这将是insert
给出的结果,而不是select
给出的结果.
有两种方法可以解决这个问题。第一个是告诉 SQL 不要 return 插入语句的“结果”,这样你唯一得到的就是 table 来自 select 的数据陈述。您可以通过将 set nocount on
语句添加到过程的开头来执行此操作。
您也可以在程序的开头添加一个 begin
,在过程的结尾也添加一个 end
,这是一个常见的模式(但不是必需的,所以如果您不喜欢也没关系):
ALTER PROCEDURE [dbo].[Insertpurchase]
@UserID bigint,
@purchaseID bigint,
@MemberID bigint,
@DependentID bigint,
@ServiceDate varchar(20),
@purchaseDate varchar(20),
@purchaseNumber bigint,
@Amount varchar(20),
@Status varchar(20)
AS
begin
set nocount on; -- < tell SQL not to return the "rows affected"
DECLARE @purchaseNo bigint
-- the rest of your procedure code
end
或者,您可以通过使用 rs.NextRecordSet
只做其中之一,不能同时做!