使用Sql过程中由分隔符分隔的数据

时间:2015-06-11 12:23:40

标签: sql sql-server sql-server-2008 stored-procedures

我有一个Sql过程,它接收一个字符串作为参数。 现在我需要执行的任务是参数包含由两种不同类型的分隔符分隔的数据,即":" ",& #34;

数据库中有两列 ID

     sample Data: "10:0,11:1,12:3,13:4,15:5,16:6"
     In This case          Ids are: 10,11,12,13,14,15,16
     and their respective  values are: 1,2,3,4,5,6 

现在我想在DB中插入这些值。 你能建议一个解决方案吗?

3 个答案:

答案 0 :(得分:2)

IF OBJECT_ID('tempdb..#Test') IS NOT NULL 
DROP TABLE #Test
GO

CREATE TABLE #Test(ID INT,Val INT)

DECLARE @t table (val varchar(50))
INSERT INTO @t (val)values ('10:0,11:1,12:3,13:4,15:5,16:6')


;WITH CTE AS (
SELECT   
     Split.a.value('.', 'VARCHAR(100)') AS String  
 FROM  (SELECT   
         CAST ('<M>' + REPLACE([val], ',', '</M><M>') + '</M>' AS XML) AS String  
     FROM  @t) AS A CROSS APPLY String.nodes ('/M') AS Split(a))
     INSERT INTO #Test 
     select SUBSTRING(String,0,CHARINDEX(':',String)),REVERSE(SUBSTRING(reverse(String),0,CHARINDEX(':',reverse(String)))) from cte 

     select * from #test

答案 1 :(得分:1)

您可以使用以下功能,它将处理spli的两个分隔符

CREATE FUNCTION dbo.MultipleSplitStrings
(
   @List       NVARCHAR(MAX),
   @Separator1 Varchar(100),
   @Separator2 Varchar(100)
)
RETURNS  TABLE 
AS

   RETURN 
   (  
      SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(REPLACE(@List, ISNULL(@Separator1,''), '</i><i>') , ISNULL(@Separator2,''), '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );

GO  

Select * From dbo.MultipleSplitStrings ('10:0,11:1,12:3,13:4,15:5,16:6',',',':')

结果:

item 
10
0
11
1
12
3
13
4
15
5
16
6

答案 2 :(得分:0)

您可以使用以下功能:

CREATE FUNCTION Split (
      @InputString                  VARCHAR(8000),
      @Delimiter                    VARCHAR(50)
)

RETURNS @Items TABLE (
      Item                          VARCHAR(8000)
)

AS
BEGIN
      IF @Delimiter = ' '
      BEGIN
            SET @Delimiter = ','
            SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
      END

      IF (@Delimiter IS NULL OR @Delimiter = '')
            SET @Delimiter = ','

      DECLARE @Item                 VARCHAR(8000)
      DECLARE @ItemList       VARCHAR(8000)
      DECLARE @DelimIndex     INT

      SET @ItemList = @InputString
      SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      WHILE (@DelimIndex != 0)
      BEGIN
            SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
            INSERT INTO @Items VALUES (@Item)

            -- Set @ItemList = @ItemList minus one less item
            SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
            SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      END -- End WHILE

      IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
      BEGIN
            SET @Item = @ItemList
            INSERT INTO @Items VALUES (@Item)
      END

      -- No delimiters were encountered in @InputString, so just return @InputString
      ELSE INSERT INTO @Items VALUES (@InputString)

      RETURN

END -- End Function
GO

CREATE TABLE #Test
(
    Item NVARCHAR(1000)
)

INSERT INTO #Test
SELECT * FROM Split('10:0,11:1,12:3,13:4,15:5,16:6', ':')

SELECT f.* FROM #Test t
CROSS APPLY Split(t.Item, ',') f

DROP TABLE #Test