存储过程总是从代码中获取第一个参数

use*_*851 -2 vb.net sql-server

我有一个这样的存储过程:

ALTER PROCEDURE [dbo].[T_TransactionSummary]  
 @locations nvarchar
AS
    BEGIN 
..............
.............
AND (Location_tbl.Locid IN (@locations))
Run Code Online (Sandbox Code Playgroud)

我的locid字段是整数这个locid来自我的listbox.if我选择一个项目1 locid将来.如果我选​​择2项目2 locid将来..我有一个ListBox填充@locations参数(一个整数),我把这个列表框值这样

cnt = LSTlocations.SelectedItems.Count
 Dim list As New List(Of Integer)
        Dim locid As 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)
                list.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
        cmd23.Parameters.Add("@locations", SqlDbType.Int).Value = String.Join(",", list)
        da.SelectCommand = cmd23
        da.Fill(ds)  
Run Code Online (Sandbox Code Playgroud)

现在我从listbox的locationid只传递给存储过程1,2,3.但存储过程总是取第一个值(我的意思是在这种情况下取​​1).

Aar*_*and 7

首先,您绝对需要为参数定义一个长度 ...您当前拥有的是在第一个字符处被截断的字符串.

DECLARE @locations NVARCHAR;
SET @locations = '1,2,3';
SELECT @locations;
Run Code Online (Sandbox Code Playgroud)

结果:

1
Run Code Online (Sandbox Code Playgroud)

你需要说

@locations VARCHAR(MAX)
Run Code Online (Sandbox Code Playgroud)

您不需要NVARCHAR存储以逗号分隔的整数列表.(我假设你可能有很长的整数列表,但也许MAX可能会有8000.)

然后,您不能说IN (@locations)- 这将无法正常工作,您将收到有关转换'1,2,3...'为int 的错误消息,或者它只是找不到值 - 即与整个字符串而不是集合进行比较.所以你可以用动态SQL做到这一点,例如

SET @sql = @sql + ' WHERE locations IN (' + @locations + ') ...;';
Run Code Online (Sandbox Code Playgroud)

但这充满了各种其他问题,包括可维护性和SQL注入的风险.我强烈推荐使用表值参数.基本上你创建一个这样的类型:

CREATE TYPE dbo.Integers AS TABLE(Item INT PRIMARY KEY);
Run Code Online (Sandbox Code Playgroud)

然后以这种方式使用参数:

@locations dbo.Integers READONLY
Run Code Online (Sandbox Code Playgroud)

你可以说:

WHERE EXISTS (SELECT 1 FROM @locations WHERE Item = Location_tbl.Locid)
Run Code Online (Sandbox Code Playgroud)

在VB.Net代码中,将列表框选择填充到DataTable(而不是int或字符串)中,并将DataTable作为参数传递给SqlDbType.Structured.我这里有一些例子,但它们是C#:

http://www.sqlperformance.com/2012/08/t-sql-queries/splitting-strings-now-with-less-t-sql

在MSDN上还有大量有关TVP的文档.

  • @AaronBertrand:我同意.你的答案看起来很完整. (2认同)
  • @ user2878851这不是"为我编写所有代码"论坛.特别是当您首先只包含原始代码的一小部分时.您是否看到我如何更改`@ locations`变量的使用?也许我应该把它命名为不同的东西?您是否查看了我提供的任何链接以使用我的答案来帮助您学习如何使用这些技术? (2认同)