SQL在拆分字符串后将数据插入到其他表中

时间:2013-08-16 07:38:53

标签: sql sql-server

我有一张表WCA

ID TYPE    ..
1  *1*3*5*
2  *1*5*
..

现在我想将数据移动到新表WCA_TYPE

ID WCA_ID TYPE
1    1     1
2    1     3
3    1     5
4    2     1
5    2     5
..

这里的ID是自动增加。

如何在MS SQL服务器中编写sql以将类型从旧表拆分为多类型&将其插入新表中。

4 个答案:

答案 0 :(得分:2)

您可以使用递归cte - 例如

CREATE TABLE #WCA_TYPE
(ID         INT     IDENTITY(1, 1)  PRIMARY KEY
,WCA_ID     INT
,TYPE       INT);

WITH sampleData(WCA_ID, TYPE) AS
(
    SELECT
     *
    FROM (  VALUES   ('1', '1*3*5')
                    ,('2', '1*5')
         ) nTab(nCol1, nCol2)
)
,rep(WCA_ID, item, delim) AS
(
    SELECT
     WCA_ID
    ,TYPE item
     ,'*' delim
    FROM sampleData

    UNION ALL

    SELECT
     WCA_ID
    ,LEFT(item, CHARINDEX(delim, item, 1) - 1) item
    ,delim
    FROM rep
    WHERE (CHARINDEX(delim, item, 1) > 0)

    UNION ALL

    SELECT
     WCA_ID
    ,RIGHT(item, LEN(item) - CHARINDEX(delim, item, 1)) item
    ,delim
    FROM rep
    WHERE (CHARINDEX(delim, item, 1) > 0)
)
INSERT #WCA_TYPE
(TYPE
,WCA_ID)
SELECT
 item
,WCA_ID
FROM rep
WHERE (CHARINDEX(delim, item, 1) = 0)
ORDER BY WCA_ID
OPTION (MAXRECURSION 0);

SELECT * FROM #WCA_TYPE;

答案 1 :(得分:2)

DECLARE @CurID INT, @MaxID INT, @t VARCHAR(200)

SELECT @CurID = 1, @MaxID = MAX(ID) FROM WCA

WHILE @CurID <= @MaxID
BEGIN
  SELECT @t = TYPE
  FROM WCA
  WHERE ID = @CurID

  ;WITH Pieces([Pos], [start], [stop]) AS (
      SELECT 1, 1, CHARINDEX('*', @t)
      UNION ALL
      SELECT [Pos] + 1, [stop] + 1, CHARINDEX('*', @t, [stop] + 1)
      FROM Pieces
      WHERE [stop] > 0
    )
    INSERT INTO WCA_TYPE(WCA_ID, TYPE)
    SELECT @CurID, T.Value
    FROM
    ( SELECT [Pos], SUBSTRING(@t, start, CASE WHEN [stop] > 0 THEN [stop]-[start] ELSE 4000 END) AS [Value]
      FROM Pieces
    ) T 
    WHERE T.Value <> '' 


  SET @CurID = @CurID + 1
END

SELECT *
FROM WCA_TYPE

look this fiddle

答案 2 :(得分:2)

假设您的类型列总是有3位数字,您可以使用它。

INSERT INTO wca_type (WCA_id,type)  
   SELECT 1,SUBSTRING(CONVERT(nvarchar(MAX),type),1,1) FROM wca    
   UNION ALL   
   SELECT 1,SUBSTRING(CONVERT(nvarchar(MAX),type),2,1) FROM wca  
   UNION ALL   
   SELECT 1,SUBSTRING(CONVERT(nvarchar(MAX),type),3,1) FROM wca

答案 3 :(得分:1)

WITH CTE AS
(
  select id,Type,0 as startPos, 
          CHARINDEX('*',TYPE)-1 as endPos from WCA
  UNION ALL
  select id,Type,endPos+2 as startPos, 
          CHARINDEX('*',TYPE,endPos+2)-1 as endPos from CTE
  where CHARINDEX('*',TYPE,endPos+2)>0
)

INSERT INTO WCA_TYPE (WCA_ID, TYPE)
select ID,
        CASE WHEN EndPos>0 
        THEN
        Substring(Type,StartPos,EndPos-StartPos+1) 
        else
        Type
        end as Type
from CTE 
where EndPos<>0

SQLFiddle Select demo