0

我有一个像这样的表值参数

CREATE TYPE dbo.Loc AS TABLE(Lo integer);

我的存储过程如下所示:

ALTER PROCEDURE [dbo].[T_TransactionSummary]  
                        @startDate datetime,  
                        @endDate datetime,
                        @locations dbo.Loc readonly
              ..........................
              ...........................
WHERE     (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate) 
AND (Location_tbl.Locid IN (select Lo from @locations))

我有一个包含多个项目的列表框。我可以从我的列表框中选择多个项目。如何将多个 Locationid 传递给我的存储过程?

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 不兼容

为什么我会收到此错误?

4

1 回答 1

-1

您可以传递多个逗号分隔的 locationid。使用http://sommarskog.se/arrays-in-sql-2008.html建议的方法

于 2013-11-04T09:06:11.227 回答