gpt4 book ai didi

sql-server - 一个存储过程中的多个单独的 SQL 查询

转载 作者:行者123 更新时间:2023-12-03 01:55:32 25 4
gpt4 key购买 nike

我需要许多(16)个不同的查询来同时获取行数。我正在运行 16 个不同的连接,但我认为我可以组合为 1 个存储过程。

我遇到了麻烦,因为它在数据读取器上给出了错误(系统超出范围异常(exception)):

Count2 = Convert.ToInt32(objDRL("Row_Count2").ToString). 

这是我的代码:

Dim strConn As String = "Data Source=myDataSource"
Dim Conn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand("adminStats", Conn)
Cmd.CommandType = CommandType.StoredProcedure
Dim objDRL As SqlDataReader
Cmd.Parameters.Add(New SqlParameter("@campDate", "June 2014"))
Conn.Open()
objDRL = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If objDRL.HasRows Then
While objDRL.Read()
Count1 = Convert.ToInt32(objDRL("Row_Count1"))
Count2 = Convert.ToInt32(objDRL("Row_Count2"))
Count3 = Convert.ToInt32(objDRL("Row_Count3"))
Count4 = Convert.ToInt32(objDRL("Row_Count4"))
Count5 = Convert.ToInt32(objDRL("Row_Count5"))
Count6 = Convert.ToInt32(objDRL("Row_Count6"))
Count7 = Convert.ToInt32(objDRL("Row_Count7"))
Count8 = Convert.ToInt32(objDRL("Row_Count8"))
Count9 = Convert.ToInt32(objDRL("Row_Count9"))
Count10 = Convert.ToInt32(objDRL("Row_Count10"))
Count11 = Convert.ToInt32(objDRL("Row_Count11"))
Count12 = Convert.ToInt32(objDRL("Row_Count12"))
Count13 = Convert.ToInt32(objDRL("Row_Count13"))
Count14 = Convert.ToInt32(objDRL("Row_Count14"))
Count15 = Convert.ToInt32(objDRL("Row_Count15"))


End While
Else

End If

link1.Text = Count1.ToString
link2.Text = Count2.ToString
link3.Text = Count3.ToString
link4.Text = Count4.ToString
Link5.Text = Count5.ToString
Link6.Text = Count6.ToString
Link7.Text = Count7.ToString
Link8.Text = Count8.ToString
Link9.Text = Count9.ToString
Link10.Text = Count10.ToString
Link11.Text = Count11.ToString
Link12.Text = Count12.ToString
Link13.Text = Count13.ToString
Link14.Text = Count14.ToString
Link15.Text = Count15.ToString

Conn.Close()

存储过程:

CREATE PROCEDURE adminStats 
@campDate VARCHAR(20)
AS
BEGIN
SELECT COUNT(*) AS Row_Count1 FROM Customer
SELECT COUNT(*) AS Row_Count2 FROM campRegistration WHERE campDate = @campDate
SELECT COUNT(*) AS Row_Count3 FROM campRegistration WHERE paidFull = 'True'

SELECT COUNT(*) AS Row_Count4 FROM campRegistration WHERE shirtSize = 'S'
SELECT COUNT(*) AS Row_Count5 FROM campRegistration WHERE shirtSize = 'M'
SELECT COUNT(*) AS Row_Count6 FROM campRegistration WHERE shirtSize = 'L'
SELECT COUNT(*) AS Row_Count7 FROM campRegistration WHERE shirtSize = 'XL'
SELECT COUNT(*) AS Row_Count8 FROM campRegistration WHERE shirtSize = 'XXL'

SELECT COUNT(*) AS Row_Count9 FROM campRegistration WHERE Staff = 'True'

SELECT COUNT(*) AS Row_Count10 FROM campRegistration WHERE days = '1'
SELECT COUNT(*) AS Row_Count11 FROM campRegistration WHERE days = '2'
SELECT COUNT(*) AS Row_Count12 FROM campRegistration WHERE days = '3'
SELECT COUNT(*) AS Row_Count13 FROM campRegistration WHERE days = '12'
SELECT COUNT(*) AS Row_Count14 FROM campRegistration WHERE days = '23'
SELECT COUNT(*) AS Row_Count15 FROM campRegistration WHERE days = '123'
END
GO

最佳答案

尝试更改您对此的查询 -

CREATE PROCEDURE adminStats 
@campDate varChar(20)
AS
BEGIN

SELECT
(SELECT COUNT(1) FROM dbo.Customer) AS Row_Count1
, COUNT(CASE WHEN campDate = @campDate THEN 1 END) AS Row_Count2
, COUNT(CASE WHEN paidFull = 'True' THEN 1 END) AS Row_Count3
, COUNT(CASE WHEN shirtSize = 'S' THEN 1 END) AS Row_Count4
, COUNT(CASE WHEN shirtSize = 'M' THEN 1 END) AS Row_Count5
, COUNT(CASE WHEN shirtSize = 'L' THEN 1 END) AS Row_Count6
, COUNT(CASE WHEN shirtSize = 'XL' THEN 1 END) AS Row_Count7
, COUNT(CASE WHEN shirtSize = 'XXL' THEN 1 END) AS Row_Count8
, COUNT(CASE WHEN Staff = 'True' THEN 1 END) AS Row_Count9
, COUNT(CASE WHEN [days] = '1' THEN 1 END) AS Row_Count10
, COUNT(CASE WHEN [days] = '2' THEN 1 END) AS Row_Count11
, COUNT(CASE WHEN [days] = '3' THEN 1 END) AS Row_Count12
, COUNT(CASE WHEN [days] = '12' THEN 1 END) AS Row_Count13
, COUNT(CASE WHEN [days] = '23' THEN 1 END) AS Row_Count14
, COUNT(CASE WHEN [days] = '123' THEN 1 END) AS Row_Count15
FROM dbo.campRegistration
WHERE [days] IN ('1', '2', '3', '12', '23', '123')
OR shirtSize IN ('S', 'M', 'L', 'XL', 'XXL')
OR Staff = 'True'
OR paidFull = 'True'
OR campDate = @campDate

END

有关重复扫描的更多信息,请阅读此 topic .

关于sql-server - 一个存储过程中的多个单独的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628842/

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