Return data from SQL Server stored procedure using ASP page [duplicate]

vfh0ocws  于 2023-10-15  发布在  SQL Server
关注(0)|答案(1)|浏览(152)

This question already has an answer here:

operation not allowed when the object is closed when running more advanced query (1 answer)
Closed 3 years ago.

I have an .asp page I am passing data to which needs to send info to a SQL server stored procedure, get the return value, and pass it back to the main page. I currently have the page sending the data and can verify this as the stored procedure is an insert and select. So I see the data being inserted. For some reason, the select part is not getting passed back to the page.

This is the stored procedure the asp page is sending data to:

  1. ALTER PROCEDURE [dbo].[Insertpurchase]
  2. @UserID bigint,
  3. @purchaseID bigint,
  4. @MemberID bigint,
  5. @DependentID bigint,
  6. @ServiceDate varchar(20),
  7. @purchaseDate varchar(20),
  8. @purchaseNumber bigint,
  9. @Amount varchar(20),
  10. @Status varchar(20)
  11. AS
  12. DECLARE @purchaseNo bigint
  13. SET @purchaseNo = (SELECT [LastNumber] FROM [purchaseAdmin] where [id] = @purchaseID );
  14. DECLARE @day varchar(20)
  15. DECLARE @month varchar(20)
  16. DECLARE @year varchar(20)
  17. DECLARE @ServiceOFDate varchar(20)
  18. DECLARE @purchaseOFDate varchar(20)
  19. SET @ServiceOFDate = SUBSTRING(@ServiceDate, 1, 2)+'/'+SUBSTRING(@ServiceDate, 3, 2)+'/'+SUBSTRING(@ServiceDate, 5, 4);
  20. set @purchaseOFDate = SUBSTRING(@purchaseDate, 1, 2)+'/'+SUBSTRING(@purchaseDate, 3, 2)+'/'+SUBSTRING(@purchaseDate, 5, 4);
  21. insert into [NewPurchase] (
  22. [UserID]
  23. ,[purchaseID]
  24. ,[MemberID]
  25. ,[DependentID]
  26. ,[ServiceDate]
  27. ,[purchaseDate]
  28. ,[purchaseNumber]
  29. ,[Amountpurchase]
  30. ,[Status]
  31. ) values ( @UserID, @purchaseID , @MemberID , @DependentID , CONVERT(datetime,@ServiceOFDate) , CONVERT(datetime,@purchaseOFDate) , (@purchaseNo+1) , @Amount , @Status ) ;
  32. select (@purchaseNo+1) as purchases

This is the asp page. I am using the code by "Lankymart" from this thread " Using Stored Procedure in Classical ASP .. execute and get results "

  1. <%
  2. Dim conn_string, row, rows, ary_data
  3. conn_string = "Provider=SQLOLEDB; Data Source = (Local) ; Initial Catalog = Testonline ; User Id = sa; Password=Test123"
  4. Set objCommandSec = CreateObject("ADODB.Command")
  5. With objCommandSec
  6. .ActiveConnection = conn_string
  7. .CommandType = 4
  8. .CommandText = "dbo.Insertpurchase"
  9. .Parameters.Append .CreateParameter("@UserID", 200, 1, 10, 1)
  10. .Parameters.Append .CreateParameter("@purchaseID", 200, 1, 50, 1)
  11. .Parameters.Append .CreateParameter("@MemberID", 200, 1, 50, 1)
  12. .Parameters.Append .CreateParameter("@DependentID", 200, 1, 50, 1)
  13. .Parameters.Append .CreateParameter("@ServiceDate", 200, 1, 10, "01012020")
  14. .Parameters.Append .CreateParameter("@purchaseDate", 200, 1, 50, "01012020")
  15. .Parameters.Append .CreateParameter("@purchaseNumber", 200, 1, 50, 1)
  16. .Parameters.Append .CreateParameter("@Amount", 200, 1, 50, "")
  17. .Parameters.Append .CreateParameter("@Status", 200, 1, 50, "")
  18. Set rs = .Execute()
  19. If Not rs.EOF Then ary_data = rs.GetRows()
  20. Call rs.Close()
  21. Set rs = Nothing
  22. End With
  23. Set objCommandSec = Nothing
  24. 'Command and Recordset no longer needed as ary_data contains our data.
  25. If IsArray(ary_data) Then
  26. ' Iterate through array
  27. rows = UBound(ary_data, 2)
  28. For row = 0 to rows
  29. objOutFile.WriteLine row
  30. ' Return our row data
  31. ' Row N column 2 (index starts from 0)
  32. Call Response.Write(ary_data(0, row) & "")
  33. Next
  34. Else
  35. ' Nothing returned
  36. Call Response.Write("No data returned")
  37. End If
  38. %>

I need the asp page to "Response.Write" with the data from the stored procedure select but it seems as if it's not getting the data. Any help would be appreciated.

Thanks

zbdgwd5y

zbdgwd5y1#

Inside the stored procedure you have two statements, and each statement will return a "result". The result of the insert statement will not be a table of data, but rather a message saying how many rows were inserted. When you call Set rs = .Execute() , the value of rs will contain the first "result", which will be the result given by the insert , not the result given by the select .

There are two ways to solve this. The first is to tell SQL not to return a "result" for the insert statement, so that the only thing you get back is the table of data from the select statement. You can do this by adding the set nocount on statement to the start of your procedure.

You might as well add a begin to the start, and an end to the end of the procedure too, it's a common pattern to follow (but it's not required, so if you don't like it, no problem):

  1. ALTER PROCEDURE [dbo].[Insertpurchase]
  2. @UserID bigint,
  3. @purchaseID bigint,
  4. @MemberID bigint,
  5. @DependentID bigint,
  6. @ServiceDate varchar(20),
  7. @purchaseDate varchar(20),
  8. @purchaseNumber bigint,
  9. @Amount varchar(20),
  10. @Status varchar(20)
  11. AS
  12. begin
  13. set nocount on; -- < tell SQL not to return the "rows affected"
  14. DECLARE @purchaseNo bigint
  15. -- the rest of your procedure code
  16. end

Alternatively, you can tell the record set that you don't want the first result, by asking for the next set of results, using rs.NextRecordSet

Do one or the other, not both!

展开查看全部

相关问题