SQL存储过程IN子句varchar数组

时间:2013-07-29 11:47:09

标签: c# sql sql-server in-clause

我有一个c#程序,想要给存储过程一个参数。我的c#程序中的字符串就像“'食物','衣服','血'”。

ALTER PROCEDURE [dbo].[GetLocations]
    @longMax float =100,
    @longMin float=0,
    @latMax float=100,
    @latMin float=0,
    @categoryFilter char(200) = ''
AS
BEGIN
    SET NOCOUNT ON;

    SELECT *
    FROM dbo.Locations
    WHERE longitude BETWEEN @longMin AND @longMax
      AND latitude BETWEEN @latMin AND @latMax
      AND subCategory in (@categoryFilter) 
END

结果应该给我3个条目,但结果是空的。我试图用两个以上的参数来逃避参数'但结果没有改变。

它适用于其他列的int值,但不适用于chars。

如果有人可以提供帮助,那就太好了:)

6 个答案:

答案 0 :(得分:3)

您的结果设置为空白,因为in clause适用于数据集。 而你传递的单个字符串为200个单词。 您的查询实际上是在比较subCategory和categoryFilter

您可以通过提供以下设置在管理工作室中明确尝试

SELECT *
FROM dbo.Locations
WHERE longitude BETWEEN @longMin AND @longMax
AND latitude BETWEEN @latMin AND @latMax
AND subCategory in ('abc','bcd','cde');

abc-你的第一个类别过滤器,类似地bcd你的第二个,依此类推。 如果您在@categoryfilter中有逗号(,)分隔的数据集,那么首先拆分该字符串,然后使用IN Clause

答案 1 :(得分:2)

我认为您需要拆分字符串参数 -

DECLARE @categoryFilter VARCHAR(200) = 'Food,Clothes,Blood'

SELECT t.c.value('.', 'VARCHAR(50)')
FROM (
     SELECT ID = CAST ('<M>' + REPLACE(@categoryFilter, ',', '</M><M>') + '</M>' AS XML)
) r 
CROSS APPLY ID.nodes ('/M') t(c)

您的查询 -

ALTER PROCEDURE [dbo].[GetLocations] 

@longMax FLOAT = 100,
@longMin FLOAT = 0,
@latMax FLOAT = 100,
@latMin FLOAT = 0,
@categoryFilter VARCHAR(200) = 'Food,Clothes,Blood'

AS
BEGIN
     SET NOCOUNT ON;

     SELECT *
     FROM dbo.Locations
     WHERE longitude BETWEEN @longMin AND @longMax
          AND latitude BETWEEN @latMin AND @latMax
          AND (
               @categoryFilter = '' 
               OR 
               subCategory IN (
                    SELECT t.c.value('.', 'VARCHAR(50)')
                    FROM (
                         SELECT ID = CAST ('<M>' + REPLACE(@categoryFilter, ',', '</M><M>') + '</M>' AS XML)
                    ) r 
                    CROSS APPLY ID.nodes ('/M') t(c))
          )

END

答案 2 :(得分:1)

试试这个

ALTER PROCEDURE [dbo].[GetLocations]
@longMax float =100,
@longMin float=0,
@latMax float=100,
@latMin float=0,
@categoryFilter char(200) = ''
AS
BEGIN
    SET NOCOUNT ON;
   SET @categoryFilter =  REPLACE(@categoryFilter,',',''',''')

   DECLARE @QUERY VARCHAR(MAX)
   SET @QUERY = 'SELECT *
    FROM dbo.Locations
    WHERE longitude BETWEEN @longMin AND @longMax
      AND latitude BETWEEN @latMin AND @latMax
      AND subCategory in ('''+RTRIM(@categoryFilter)+''')'

    --PRINT (@QUERY) 

    EXEC (@QUERY) 


END

答案 3 :(得分:0)

你可以使用LIKE:

ALTER PROCEDURE [dbo].[GetLocations]
    @longMax float =100,
    @longMin float=0,
    @latMax float=100,
    @latMin float=0,
    @categoryFilter char(200) = ''
AS
BEGIN
    SET NOCOUNT ON;

    SELECT *
    FROM dbo.Locations
    WHERE longitude BETWEEN @longMin AND @longMax
      AND latitude BETWEEN @latMin AND @latMax
      AND @categoryFilter LIKE '%' + subCategory + '%'
END

答案 4 :(得分:0)

尝试修剪您的传入字符串 - &gt; RTRIM(@string)。

如果你正在使用char,你总是需要修剪。

答案 5 :(得分:0)

你可以尝试如下

ALTER PROCEDURE [dbo].[GetLocations]
    @longMax float =100,
    @longMin float=0,
    @latMax float=100,
    @latMin float=0,
    @categoryFilter char(200) = '',
    @Query varchar(2000)

AS
BEGIN
    SET NOCOUNT ON;

    Set @Query = 'SELECT *
            FROM dbo.Locations
           WHERE longitude BETWEEN @longMin AND @longMax
                  AND latitude BETWEEN @latMin AND @latMax
                  AND @categoryFilter IN (' + LTRIM(RTRIM(@categoryFilter)) + ')' 

    Exec(@Query)

END