我有一个这样的存储过程:
ALTER PROCEDURE [dbo].[T_TransactionSummary]
@startDate datetime,
@endDate datetime,
-- @locations integer
@locations dbo.Loc readonly
AS
BEGIN
DECLARE @fromDate datetime = convert(varchar(10), @startDate, 121) + ' 00:00:00.000';
DECLARE @toDate datetime = convert(varchar(10), @endDate, 121) + ' 11:59:59.999';
SELECT COUNT(*) AS CarCount, Location_tbl.LocName
, REPLACE(CONVERT(varchar(11), Transaction_tbl.dtime, 113), ' ', '-') AS Date_time
, CASE Transaction_tbl.Compl WHEN 'True' THEN 'Complementary'
WHEN 'False' THEN 'Non-Complementary' ELSE 'Other' END AS Expr1
, VType_tbl.Vtype, CASE WHEN (VType_tbl.Vtype = 'Normal' AND Transaction_tbl.Compl = 'False') THEN COUNT(*)
* 50 WHEN (VType_tbl.Vtype = 'VIP' AND Transaction_tbl.Compl = 'False') THEN COUNT(*) * 100 ELSE 0 END AS Total,
CASE WHEN Transaction_tbl.Paid = 'False' THEN 0 ELSE SUM(Transaction_tbl.PAmount) END AS CIH
FROM Transaction_tbl INNER JOIN
Location_tbl ON Transaction_tbl.Locid = Location_tbl.Locid LEFT OUTER JOIN
VType_tbl ON Transaction_tbl.vtid = VType_tbl.vtid LEFT OUTER JOIN
Make_tbl ON Transaction_tbl.mkid = Make_tbl.mkid LEFT OUTER JOIN
Color_tbl ON Transaction_tbl.Colid = Color_tbl.colid LEFT OUTER JOIN
PlateSource_tbl ON Transaction_tbl.PSID = PlateSource_tbl.PSID
WHERE (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate)
--AND (Location_tbl.Locid IN (@locations))
AND (Location_tbl.Locid IN (select Lo from @locations))
GROUP BY Location_tbl.LocName, REPLACE(CONVERT(varchar(11), Transaction_tbl.dtime, 113), ' ', '-')
, VType_tbl.Vtype, Transaction_tbl.Compl, Transaction_tbl.Paid
ORDER BY Location_tbl.LocName, Date_time DESC, VType_tbl.Vtype, Transaction_tbl.Compl
END
我的locid列是来自我的列表框的这个locid的整数。如果我选择一个项目,1个locid将会来。如果我选择2项2,locid将会来..我有一个ListBox,它填充了@locations参数(一个整数),我把这个列表框值像这样
cnt = LSTlocations.SelectedItems.Count
Dim dtlist As New DataTable()
Dim locid As Integer
If cnt > 0 Then
For i = 0 To cnt - 1
dtlist.Columns.Add(locid, GetType(Integer))
Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
dtlist.Rows.Add(locid)
Next
End If
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("T_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
Dim tvp1 As SqlParameter = cmd23.Parameters.AddWithValue("@locations", dtlist)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)
但我得到的错误是这样的:
操作数类型冲突:表类型与int
不兼容
我知道错误来自“AND (Location_tbl.Locid IN (select Lo from @locations))
”我试图将多个参数传递给@locations,这就是为什么我会收到错误..如果有人知道怎么做的话。请帮助我找出..