Tsql 在存储过程中将逗号分隔的字符串作为参数传递

Tsql 在存储过程中将逗号分隔的字符串作为参数传递,tsql,sql-server-2008-r2,Tsql,Sql Server 2008 R2,我正在编写一个存储过程,它根据传入的参数值调用嵌套存储过程 我试图传递到子过程中的参数之一是逗号分隔的值字符串。我在主过程中声明一个varchar并设置它 我的主要存储过程如下所示: Declare @StatusString varchar(150) Set @StatusString = N'''OPEN,'',''CLOSED'',''PENDING''' 这将@Status字符串变量作为参数传递到我的嵌套存储过程中 然后,我的嵌套存储过程会 select from table wher

我正在编写一个存储过程,它根据传入的参数值调用嵌套存储过程

我试图传递到子过程中的参数之一是逗号分隔的值字符串。我在主过程中声明一个varchar并设置它

我的主要存储过程如下所示:

Declare @StatusString varchar(150)
Set @StatusString = N'''OPEN,'',''CLOSED'',''PENDING'''
这将@Status字符串变量作为参数传递到我的嵌套存储过程中

然后,我的嵌套存储过程会

select from table where table.column in (@StatusString)

然而,我没有得到任何结果。看起来要么我没有正确地传递参数,要么我没有正确地编写Select语句。我缺少什么?

SQL中的问题是不能在字符串中使用逗号作为in子句中的分隔符。例如,OPEN、CLOSED和PENDING是单个字符串,而不是4个字段

我知道只有两种可以接受的方法可以做到这一点

创建一个表值函数,该函数接受字符串并返回一个记录集。然后可以将该记录集馈送到临时表中,并对值执行联接,或者可以将该记录集用作in子句,即Select*from中的WHERE列fn@StatusString.

创建自定义数据类型并将自定义数据类型传递给存储过程


请注意上面没有列出字符串连接。

SQL中的问题是不能在字符串中使用逗号作为in子句中的分隔符。例如,OPEN、CLOSED和PENDING是单个字符串,而不是4个字段

    CREATE FUNCTION Split
    (
      @delimited nvarchar(max),
      @delimiter nvarchar(100)
    ) RETURNS @t TABLE
    (
    -- Id column can be commented out, not required for sql splitting string
      id int identity(1,1), -- I use this column for numbering splitted parts
      val nvarchar(max)
    )
    AS
    BEGIN
      declare @xml xml
      set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

      insert into @t(val)
      select
        r.value('.','varchar(max)') as item
      from @xml.nodes('//root/r') as records(r)

      RETURN
    END
    GO


    Declare @StatusString varchar(150)
    Set @StatusString = N'OPEN,CLOSED,PENDING'
--if you want them with quotes
--Set @StatusString = N'''OPEN'',''CLOSED'',''PENDING'''
    declare @t table (val nvarchar(100))
      insert into @t select * from dbo.split(@StatusString,',')
    select from table where table.column in (select val from @t)
我知道只有两种可以接受的方法可以做到这一点

创建一个表值函数,该函数接受字符串并返回一个记录集。然后可以将该记录集馈送到临时表中,并对值执行联接,或者可以将该记录集用作in子句,即Select*from中的WHERE列fn@StatusString.

创建自定义数据类型并将自定义数据类型传递给存储过程


请注意上面没有列出字符串连接。

SQL中的问题是不能在字符串中使用逗号作为in子句中的分隔符。例如,OPEN、CLOSED和PENDING是单个字符串,而不是4个字段

    CREATE FUNCTION Split
    (
      @delimited nvarchar(max),
      @delimiter nvarchar(100)
    ) RETURNS @t TABLE
    (
    -- Id column can be commented out, not required for sql splitting string
      id int identity(1,1), -- I use this column for numbering splitted parts
      val nvarchar(max)
    )
    AS
    BEGIN
      declare @xml xml
      set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

      insert into @t(val)
      select
        r.value('.','varchar(max)') as item
      from @xml.nodes('//root/r') as records(r)

      RETURN
    END
    GO


    Declare @StatusString varchar(150)
    Set @StatusString = N'OPEN,CLOSED,PENDING'
--if you want them with quotes
--Set @StatusString = N'''OPEN'',''CLOSED'',''PENDING'''
    declare @t table (val nvarchar(100))
      insert into @t select * from dbo.split(@StatusString,',')
    select from table where table.column in (select val from @t)
我知道只有两种可以接受的方法可以做到这一点

创建一个表值函数,该函数接受字符串并返回一个记录集。然后可以将该记录集馈送到临时表中,并对值执行联接,或者可以将该记录集用作in子句,即Select*from中的WHERE列fn@StatusString.

创建自定义数据类型并将自定义数据类型传递给存储过程


请注意上面没有列出字符串连接。

SQL中的问题是不能在字符串中使用逗号作为in子句中的分隔符。例如,OPEN、CLOSED和PENDING是单个字符串,而不是4个字段

    CREATE FUNCTION Split
    (
      @delimited nvarchar(max),
      @delimiter nvarchar(100)
    ) RETURNS @t TABLE
    (
    -- Id column can be commented out, not required for sql splitting string
      id int identity(1,1), -- I use this column for numbering splitted parts
      val nvarchar(max)
    )
    AS
    BEGIN
      declare @xml xml
      set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

      insert into @t(val)
      select
        r.value('.','varchar(max)') as item
      from @xml.nodes('//root/r') as records(r)

      RETURN
    END
    GO


    Declare @StatusString varchar(150)
    Set @StatusString = N'OPEN,CLOSED,PENDING'
--if you want them with quotes
--Set @StatusString = N'''OPEN'',''CLOSED'',''PENDING'''
    declare @t table (val nvarchar(100))
      insert into @t select * from dbo.split(@StatusString,',')
    select from table where table.column in (select val from @t)
我知道只有两种可以接受的方法可以做到这一点

创建一个表值函数,该函数接受字符串并返回一个记录集。然后可以将该记录集馈送到临时表中,并对值执行联接,或者可以将该记录集用作in子句,即Select*from中的WHERE列fn@StatusString.

创建自定义数据类型并将自定义数据类型传递给存储过程


请注意,上面没有列出字符串连接。

最好经历许多困难。编写单个泛型函数来拆分字符串是一次性的开销。我的建议是使用该选项并同时传递行号,这样函数将生成两列。此外,在几乎任何工作中,绕过控件都是一个大禁忌。这些控件的存在是有原因的。绕过这些障碍的唯一方法是通过字符串连接的动态sql,这是我极力反对的。制作函数并不是一件很繁重的工作。最好经历许多困难。编写单个泛型函数来拆分字符串是一次性的开销。我的建议是使用该选项并同时传递行号,这样函数将生成两列。此外,在几乎任何工作中,绕过控件都是一个大禁忌。这些控件的存在是有原因的。绕过这些障碍的唯一方法是通过字符串连接的动态sql,这是我极力反对的。制作函数并不是一件很繁重的工作。最好经历许多困难。编写单个泛型函数来拆分字符串是一次性的开销。我的建议是使用该选项并同时传递行号,这样函数将生成两列。此外,在几乎任何工作中,绕过控件都是一个大禁忌。这些控件的存在是有原因的。绕过这些障碍的唯一方法是通过字符串连接的动态sql,这是我极力反对的
    CREATE FUNCTION Split
    (
      @delimited nvarchar(max),
      @delimiter nvarchar(100)
    ) RETURNS @t TABLE
    (
    -- Id column can be commented out, not required for sql splitting string
      id int identity(1,1), -- I use this column for numbering splitted parts
      val nvarchar(max)
    )
    AS
    BEGIN
      declare @xml xml
      set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

      insert into @t(val)
      select
        r.value('.','varchar(max)') as item
      from @xml.nodes('//root/r') as records(r)

      RETURN
    END
    GO


    Declare @StatusString varchar(150)
    Set @StatusString = N'OPEN,CLOSED,PENDING'
--if you want them with quotes
--Set @StatusString = N'''OPEN'',''CLOSED'',''PENDING'''
    declare @t table (val nvarchar(100))
      insert into @t select * from dbo.split(@StatusString,',')
    select from table where table.column in (select val from @t)

有很多工作要做的功能。最好经历许多困难。编写单个泛型函数来拆分字符串是一次性的开销。我的建议是使用该选项并同时传递行号,这样函数将生成两列。此外,在几乎任何工作中,绕过控件都是一个大禁忌。这些控件的存在是有原因的。绕过这些障碍的唯一方法是通过字符串连接的动态sql,这是我极力反对的。制作函数并不是一件很繁重的工作。在你的问题下看我的评论在你的问题下看我的评论在你的问题下看我的评论在你的问题下看我的评论