0

大家好,我一直在尝试将以下代码的结果插入名为 dbo.filemetadata.Source 的列中,这是代码:

WITH CTE AS 
(
  SELECT 
    rn, 
    ROW_NUMBER() OVER (PARTITION BY rn ORDER BY (SELECT  NULL)) filepartno,
    Split.a.value('.', 'VARCHAR(100)') AS filepart  
  FROM  
  (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT  NULL)) rn,
     CAST ('<M>' + REPLACE(FilePath, '\', '</M><M>') + '</M>' AS XML) AS String  
    FROM  FileMetadata
  ) AS A 
  CROSS APPLY String.nodes ('/M') AS Split(a)
)
SELECT  FilePart AS A

FROM CTE C
  JOIN (SELECT  MAX(filepartno) maxfilepartno, rn 
        FROM CTE 
        GROUP BY rn) C2 ON C.rn = C2.rn AND C.filepartno = C2.maxfilepartno-1;
4

1 回答 1

3

您可以像普通查询一样在 CTE 中组合 INSERT 和 SELECT:

WITH CTE AS 
(
 ....
)
INSERT INTO ...
SELECT  FilePart AS A

FROM CTE ....

已更新 FORUPDATE语句

WITH CTE AS 
(
 ....
)
UPDATE FileMetaData 
SET SOURCE = FilePart

FROM FileMetaData INNER JOIN CTE ON ....
于 2013-05-07T18:19:09.607 回答