sql中的exec语句,用于逗号分隔值

时间:2017-09-05 09:48:31

标签: sql-server csv tsql

我在下面创建了一个SP。我必须将逗号分隔值传递给@ID参数。我已经给出了分割功能。只有当我给一个值作为ID时,它才能正常工作。请帮助我如何更改SP的exec语句,将逗号分隔值(如''123',456)赋予SP

EXEC usp_2090_AppRej '54862','',''

ALTER PROCEDURE [dbo].[usp_2090_AppRej] 
@ID VARCHAR(100)
,@Comments VARCHAR(1000) = ''
,@ReturnMessage VARCHAR(500) OUTPUT

DECLARE @tblBPM TABLE (ids INT)
INSERT INTO @tblBPM
SELECT items
FROM dbo.Split(@ID, ',')

1 个答案:

答案 0 :(得分:0)

您正在使用SQL Server尝试以下操作。

  

创建数据库功能

docker-compose -f docker-compose.yml up --build --abort-on-container-exit
  

创建SP

ALTER FUNCTION [dbo].[fnSplit]
    (
      @sInputList VARCHAR(max) -- List of delimited items
      ,
      @sDelimiter VARCHAR(max) = ',' -- delimiter that separates items
    )
RETURNS @List TABLE ( item VARCHAR(max) )
    BEGIN
        DECLARE @sItem VARCHAR(max)
        WHILE CHARINDEX(@sDelimiter, @sInputList, 0) <> 0 
            BEGIN
                SELECT  @sItem = RTRIM(LTRIM(SUBSTRING(@sInputList, 1,
                                                       CHARINDEX(@sDelimiter,
                                                              @sInputList, 0)
                                                       - 1))) ,
                        @sInputList = RTRIM(LTRIM(SUBSTRING(@sInputList,
                                                            CHARINDEX(@sDelimiter,
                                                              @sInputList, 0)
                                                            + LEN(@sDelimiter),
                                                            LEN(@sInputList))))

                IF LEN(@sItem) > 0 
                    INSERT  INTO @List
                            SELECT  @sItem
            END

        IF LEN(@sInputList) > 0 
            INSERT  INTO @List
                    SELECT  @sInputList -- Put the last item in
        RETURN
    END