while using table valued parameter how to pass multiple parameter together to stored prcocedure

前端 未结 3 1095
Happy的楠姐
Happy的楠姐 2021-01-23 22:15

I have a table-valued parameter like this

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

My stored procedure looks like this:

ALTER PROCEDURE [dbo].[T         


        
相关标签:
3条回答
  • 2021-01-23 22:25

    There are some examples of how to do this at http://msdn.microsoft.com/en-us/library/bb675163%28v=vs.110%29.aspx (see the section titled "Passing a Table-Valued Parameter to a Stored Procedure ").

    The simplest thing would seem to be filling a DataTable with the values the user selected and passing that to the stored procedure for the @locations parameter.

    Perhaps something along the lines of (note I don't have VB.NET installed, so treat this as an outline of how it should work, not necessarily as code that will work straight away):

    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)
    
    0 讨论(0)
  • 2021-01-23 22:39
    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)
    
    0 讨论(0)
  • 2021-01-23 22:42

    Table types SQL SERVER 2008

    if you version of sql server > = 2008.

    0 讨论(0)
提交回复
热议问题