2013-10-21 29 views
-1

我創建表值參數是這樣的:操作數類型衝突:爲nvarchar是不相容,而使用TVP

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來自我的listbox.if我選擇一個項目1 locid會come.if我選擇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 
     da.SelectCommand = cmd23 
     da.Fill(ds) 

但我收到錯誤:試圖傳遞一個表值參數與2列(s)其中相應的用戶定義的表類型需要1列(s)。

回答

4

如果LocID是一個整數,那麼你的表類型應該是:

CREATE TYPE dbo.ss AS TABLE(ss1 INT); 
--------------------------------^^^ 

究竟爲什麼你使用NVARCHAR(5)在這裏?由於各種原因,這沒有意義。

另外,您需要停止將您的一組位置作爲加入到逗號分隔字符串中的整數列表傳遞。表值參數期望結構化集合,如DataTable

+0

sir ..in type i changed into int and code i changed to like this: cmd23.Parameters.Add(「@ locations」,SqlDbType.Int).Value = String.Join(「,」,list)但出現錯誤:無法將參數值從字符串轉換爲Int32。 – user2878851

+0

我怎麼可以像一個datatable通過..你可以顯示一次 – user2878851

+0

好吧,先生謝謝你的幫助..我會盡力找出 – user2878851

相關問題