在使用表值参数时如何将多个参数一起传递给存储过程
Posted
技术标签:
【中文标题】在使用表值参数时如何将多个参数一起传递给存储过程【英文标题】:while using table valued parameter how to pass multiple parameter together to stored prcocedure 【发布时间】:2013-11-04 07:24:56 【问题描述】:我有一个像这样的表值参数
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
If cnt > 0 Then
For i = 0 To cnt - 1
Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
next
end if
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
Dim tvp1 As SqlParameter =cmd23.Parameters.Add("@location", SqlDbType.Int).Value = locid
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)
但我遇到了错误..我正在 vb.net 中处理 Windows 窗体
【问题讨论】:
【参考方案1】:http://msdn.microsoft.com/en-us/library/bb675163%28v=vs.110%29.aspx 提供了一些如何执行此操作的示例(请参阅标题为“将表值参数传递给存储过程”的部分 ")。
最简单的事情似乎是用用户选择的值填充DataTable
,并将其传递给@locations
参数的存储过程。
可能类似于(注意我没有安装 VB.NET,因此将其视为它应该如何工作的大纲,不一定是可以立即工作的代码):
cnt = LSTlocations.SelectedItems.Count
' *** Set up the DataTable here: *** '
Dim locTable As New DataTable
locTable.Columns.Add("Lo", GetType(Integer))
If cnt > 0 Then
For i = 0 To cnt - 1
Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
' *** Add the ID to the table here: *** '
locTable.Rows.Add(locid)
next
end if
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
' *** Supply the DataTable as a parameter to the procedure here: *** '
Dim tvp1 As SqlParameter =cmd23.Parameters.AddWithValue("@location", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)
【讨论】:
在这一行:locTable.Rows.Add(row) 我收到错误:无法将“System.Int32[]”类型的对象转换为“System.IConvertible”类型。无法存储locTable.Rows.Add(locid)
。
非常感谢先生...现在工作正常...非常感谢您【参考方案2】:
Table types SQL SERVER 2008
如果你的 sql server 版本 > = 2008。
【讨论】:
【参考方案3】:cnt = LSTlocations.SelectedItems.Count
' *** Set up the DataTable here: *** '
Dim locTable As New DataTable
locTable.Columns.Add("Lo", GetType(Integer))
If cnt > 0 Then
For i = 0 To cnt - 1
Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
' *** Add the ID to the table here: *** '
locTable.Rows.Add(locid)
next
end if
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
' *** Supply the DataTable as a parameter to the procedure here: *** '
Dim tvp1 As SqlParameter =cmd23.Parameters.AddWithValue("@location", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)
【讨论】:
以上是关于在使用表值参数时如何将多个参数一起传递给存储过程的主要内容,如果未能解决你的问题,请参考以下文章
将DataTable传递给存储过程中的表值参数不起作用[关闭]
如何使用 JDBC 将表值参数(类数组参数)传递给 Microsoft SQL Server 2008 R2 中的存储过程? [复制]