我有一个表格,下面的结构
PickupPointCode LocationCode ClientCode
1 LOC1 Client1/Client2
2 LOC2 Client3/Client4/Client5
3 LOC3 Client6
所需的输出
PickupPointCode LocationCode ClientCode
1 LOC1 Client1
1 LOC1 Client2
2 LOC2 Client3
2 LOC2 Client4
2 LOC2 Client5
3 LOC3 Client6
使用SQL serevr 2005我编写了以下查询来完成这项工作
;WITH cte AS (
SELECT
PickupPointCode
,LocationCode
,CAST('<i>' + REPLACE(ClientCode, '/', '</i><i>') + '</i>' AS XML) AS ClientCodes
FROM <TABLE NAME>)
SELECT
PickupPointCode
,LocationCode
,x.i.value('.', 'VARCHAR(MAX)') AS ClientCode
FROM cte
CROSS APPLY ClientCodes.nodes('//i') x(i)
但现在我必须使用 SQL SERVER 2000 做同样的事情。我怎么能这样做?
谢谢
答案 0 :(得分:1)
基本思想是将每行乘以与CLIENT一样多的次数。然后,提取字符串的相应部分。
查看一些适用于SQL2005 + here的示例查询 您将需要使用包含SQL2000中的序列号的额外表。
CREATE TABLE dbo.Numbers
(
N INT NOT NULL PRIMARY KEY
);
GO
DECLARE @rows AS INT;
SET @rows = 1;
INSERT INTO dbo.Numbers VALUES(1);
WHILE(@rows <= 1000)
BEGIN
INSERT INTO dbo.Numbers SELECT N + @rows FROM dbo.Numbers;
SET @rows = @rows * 2;
END
这是您可以加入的数字表。以下是适用于SQL 2000的查询。
SELECT PickupPointCode, LocationCode,
SUBSTRING(ClientCode, Numbers.N,
CHARINDEX('/', ClientCode + '/', Numbers.N) - Numbers.N) AS ClientCode
FROM <TABLE_NAME>
JOIN Numbers ON Numbers.N <= DATALENGTH(ClientCode) + 1
AND SUBSTRING('/' + ClientCode, Numbers.N, 1) = '/'