gpt4 book ai didi

sql - 从sql存储过程返回多个值

转载 作者:行者123 更新时间:2023-12-01 11:24:15 25 4
gpt4 key购买 nike

我正在尝试将 4 个变量从存储过程返回到 ASP.Net VB 脚本中的页面。它只返回第一个结果然后中断。我对 SQL 的了解有限。我尝试了以下方法:

SQL:

Alter PROCEDURE  [dbo].[spDashboardPaymentRequests]
@id integer
AS
SELECT COUNT(Receiptno) as requestsSent
FROM [demofeepay3].[dbo].[vwallrequests]
Where Orgid = @id

SELECT Sum(totamount) as requestTotal
FROM [demofee].[dbo].[vwallrequests]
Where Orgid = @id

SELECT Sum(totamount) as requestTotalPaid
FROM [demofee].[dbo].[vwallrequests]
Where Orgid = @id AND status = 'paid'

SELECT Sum(totamount) as requestTotalUnpaid
FROM [demo].[dbo].[vwallrequests]
Where Orgid = @id AND status = 'unpaid'

ASP.NET

Function RequestsSent()
Dim objCmd2 As sqlCommand
Dim objRdr2 As sqlDataReader
objCmd2 = New SqlCommand("spDashboardPaymentRequests", objConn)
objCmd2.CommandType = CommandType.StoredProcedure
objCmd2.Parameters.AddWithValue("@orgid", Session("orgid"))
objConn.Open()
objRdr2 = objCmd2.ExecuteReader
objRdr2.Read()
Session("RequestsSent") = objRdr2("requestsSent")
Session("RequestsTotal") = objRdr2("requestTotal")
Session("RequestsTotalPaid") = objRdr2("requestTotalPaid")
Session("RequestsTotalUnpaid") = objRdr2("requestTotalUnpaid")
objConn.Close()
End Function

最佳答案

您有多个 select,这意味着多个结果集。所以你必须通过它们:

objRdr2.Read()
Session("RequestsSent") = objRdr2("requestsSent")

objRdr2.NextResult()
objRdr2.Read()
Session("RequestsTotal") = objRdr2("requestTotal")

objRdr2.NextResult()
objRdr2.Read()
Session("RequestsTotalPaid") = objRdr2("requestTotalPaid")

objRdr2.NextResult()
objRdr2.Read()
Session("RequestsTotalUnpaid") = objRdr2("requestTotalUnpaid")

或者,您可以更改存储过程以返回一个包含多列的结果集:

Alter PROCEDURE  [dbo].[spDashboardPaymentRequests]
@id integer
AS
SELECT
(SELECT COUNT(Receiptno) FROM [demofeepay3].[dbo].[vwallrequests] Where Orgid = @id)
as requestsSent,

(SELECT Sum(totamount) FROM [demofee].[dbo].[vwallrequests] Where Orgid = @id)
as requestTotal,

(SELECT Sum(totamount) FROM [demofee].[dbo].[vwallrequests] Where Orgid = @id AND status = 'paid')
as requestTotalPaid,

(SELECT Sum(totamount) FROM [demo].[dbo].[vwallrequests] Where Orgid = @id AND status = 'unpaid')
as requestTotalUnpaid

关于sql - 从sql存储过程返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com