3

我有一张桌子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

4 回答 4

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
于 2013-08-16T08:01:24.297 回答
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;
于 2013-08-16T07:50:33.110 回答
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

看这个小提琴

于 2013-08-16T07:59:01.473 回答
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 选择演示

于 2013-08-16T08:08:23.040 回答