再次感谢。请在下面找到我的示例数据。
INSERT INTO ##land_title( LandId, titleid, CreatedDateTime, CreatedUserID )
SELECT LandID, Land_TitleID, getdate(),'xyz'
FROM #Testdata as sd
OUTPUT inserted or SCOPE_IDENTITY()
CREATE TABLE #Testdata
(
LandID INT,
ReferenceID INT,
Land_TitleID VARCHAR(MAX)
)
INSERT #Testdata SELECT 1, 9, '18,20,22'
INSERT #Testdata SELECT 2, 8, '17,19'
INSERT #Testdata SELECT 3, 7, '13,19,20'
INSERT #Testdata SELECT 4, 6, ''
INSERT #Testdata SELECT 9, 11, '1,2,3,4'
Select * from #Testdata
;WITH tmp(LandID, ReferenceID, DataItem, Land_TitleID) AS
(
SELECT
LandID,
ReferenceID,
LEFT(Land_TitleID, CHARINDEX(',', Land_TitleID + ',') - 1),
STUFF(Land_TitleID, 1, CHARINDEX(',', Land_TitleID + ','), '')
FROM #Testdata
UNION all
SELECT
LandID,
ReferenceID,
LEFT(Land_TitleID, CHARINDEX(',', Land_TitleID + ',') - 1),
STUFF(Land_TitleID, 1, CHARINDEX(',', Land_TitleID + ','), '')
FROM tmp
WHERE
Land_TitleID > ''
)
SELECT
LandID,
ReferenceID,
DataItem
FROM tmp
ORDER BY LandID
谢谢
R