上執行計數我從IT_Cases_List
選擇distinct user
和存儲它在arraystaff()
。 從這個數組,我會再調用一個Stored Procedure
計數沒有該用戶,(arraystaff(i))
和循環它參加的情況下,直到arraystaff.length-1調用存儲過程到陣列
但問題是,計數不相符。
Sub getStaff(ByVal month As String)
If Not con.State = ConnectionState.Closed Then
con.Open()
End If
Dim s As String = "select distinct Attended_by from IT_Cases_List where month(Resolution_date) ='" & month & "' "
s = s & "And Year(Resolution_date) ='" & ddyear.SelectedValue & "' and Attended_by is not null "
cmd = New SqlCommand(s, con)
da = New SqlDataAdapter
ds = New DataSet
da.SelectCommand = cmd
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
staffcount = ds.Tables(0).Rows.Count
ReDim arrstaff(staffcount - 1)
For Me.i = 0 To staffcount - 1
arrstaff(i) = ds.Tables(0).Rows(i).Item("Attended_by")
Next
getCases()
End If
End Sub
Sub getCases()
If con.State = ConnectionState.Closed Then
con.Open()
End If
ReDim arrdata(arrstaff.Length - 1, 0)
For Me.i = 0 To arrstaff.Length - 1
cmd = New SqlCommand("get_cases", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ename", SqlDbType.VarChar).Value = arrstaff(i)
cmd.Parameters.Add("@Yr", SqlDbType.VarChar).Value = ddyear.SelectedValue
cmd.Parameters.Add("month", SqlDbType.VarChar).Value = m
cmd.ExecuteNonQuery()
da = New SqlDataAdapter()
da.SelectCommand = cmd
ds = New DataSet
da.Fill(ds)
If Not IsDBNull(ds.Tables(0).Rows(i).Item("NoCase")) Then
arrdata(i, 0) = ds.Tables(0).Rows(i).Item("NoCase")
End If
Next
cmd = New SqlCommand("delete from cases_Temp", con)
cmd.ExecuteNonQuery()
con.Close()
End Sub
,這是我的存儲過程
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[get_cases](
@Ename varchar(100),
@Yr varchar(50),
@month varchar(30))
AS
BEGIN
declare @NoCase as int
select @NoCase=COUNT(*)
from IT_Cases_List
where Attended_by= @Ename and month(Resolution_date) [email protected] and
Year(Resolution_date)[email protected] and Attended_by is not null
insert into cases_temp(Ename,NoCase)
values(@Ename,@NoCase)
select * from cases_Temp
end
我不知道我做了什麼錯。 任何幫助將不勝感激。
UPDATE
好了,我只調用一次getcases
但我仍然有同樣的問題。
這是我所得到的,當我運行程序:
如果我從數據庫中獲取COUNT(*),總沒有的情況下我應該得到的是132,而總案例我得到的程序是157(7 + 7 + 20 + 20 + 49 + 49 + 5)
如果我運行從SQL查詢,這是我應該得到
我注意到計數值得到重複(7,7,20,20,49,49,5)而不是(7,20,49,5,10,27,13)
有人可以告訴我我做錯了什麼?
爲什麼不在1個存儲過程/查詢中完成所有操作?不明白你爲什麼需要循環? – Macros
你的第一個查詢是開放的SQL注入。如同在第二個查詢中所做的那樣,推薦使用該查詢的參數。 –