如何在存储过程的子句中传递逗号分隔值

时间:2017-08-21 08:59:19

标签: sql sql-server stored-procedures parameters

我的条款是#ff0000,'#000' 。但问题是我必须将值传递给存储过程作为参数,以根据此颜色代码获取颜色名称。

这是我的存储过程

@ColorCode NVARCHAR(150)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * 
    FROM color 
    WHERE code IN (@ColorCode)

我想使用参数为IN子句传递这些值。我怎么能这样做?

请建议

1 个答案:

答案 0 :(得分:0)

试试这个

CREATE PROCEDURE Usp_split 
(
@ColorCode NVARCHAR(150)
) 
AS 
  BEGIN 
      -- SET NOCOUNT ON added to prevent extra result sets from 
      -- interfering with SELECT statements. 
      SET nocount ON; 

      DECLARE @SPlitDAta TABLE 
        ( 
           colorcode NVARCHAR(150) 
        ) 

      INSERT INTO @SPlitDAta 
      SELECT @ColorCode 

      SELECT * 
      FROM   color 
      WHERE  code IN (SELECT split.a.value('.', 'VARCHAR(1000)') 
                      FROM   (SELECT Cast('<S>' + Replace(colorcode, ',', 
                                          '</S><S>') 
                                          + '</S>' AS XML) AS ColorCode 
                              FROM   @SPlitDAta) AS A 
                             CROSS apply colorcode.nodes('/S') AS Split(a)) 
  END