我创建了这样的表值参数:
CREATE TYPE dbo.ss AS TABLE(ss1 integer);
然后我像这样编写了我的存储过程:
ALTER PROCEDURE [dbo].[T_TransactionSummary]
@locations dbo.ss readonly
as
begin
...............
.............
AND (Location_tbl.Locid IN (select ss1 from @locations))
我的 locid 字段是整数,这个 locid 来自我的列表框。如果我选择一个项目,1 个 locid 会出现。如果我选择 2 个项目,2 个 locid 会出现。我有一个填充 @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
da.SelectCommand = cmd23
da.Fill(ds)
但我收到错误:尝试传递具有 2 列的表值参数,其中相应的用户定义的表类型需要 1 列。