我有一个表@tbl_rawData
,其中从一个非常动态的文件(没有固定的列位置)中加载数据,因此我只在一个列中加载了整个文件。
我需要获取所需的列以及每列的对应数据。
我对ID
表中维护的customername
,prodname
,regency
和@tbl_collist
列感兴趣,但将来可能需要更多列我会在@tbl_collist
中插入更多列名称。
declare @tbl_rawData table (fileData varchar(max))
insert into @tbl_rawData
(fileData)
values
('#Columns: ID|customername|addresss|agency|prodname|valuation|regency|ceptam|agan|yaha|citr'),
('11|jeev|a-161 kv|criu|uio|1800000|abap|yes|no|1000|200|'),
('12|poon|b-278 abv|criu|uio|50000|pyt|yes|no|2700|300|')
declare @tbl_collist table (colName varchar(100))
insert into @tbl_collist
(colName)
values
('ID'),
('customername'),
('prodname'),
('regency')
我想要类似下面的输出。
id customername prodname regency
11 jeev uio abap
12 poon uio pyt
答案 0 :(得分:0)
获取dbo.DelimitedSplit8K的副本,然后:
-- Sample Data
declare @tbl_rawData table (fileData varchar(max));
insert into @tbl_rawData (fileData)
values
('#Columns: ID|customername|addresss|agency|prodname|valuation|regency|ceptam|agan|yaha|citr'),
('11|jeev|a-161 kv|criu|uio|1800000|abap|yes|no|1000|200|'),
('12|poon|b-278 abv|criu|uio|50000|pyt|yes|no|2700|300|');
declare @tbl_collist table (colName varchar(100))
insert into @tbl_collist (colName)
values('ID'),('customername'),('prodname'),('regency');
-- Solution
WITH Split AS
(
SELECT s.ItemNumber
FROM @tbl_rawData AS t
CROSS APPLY dbo.DelimitedSplit8K(t.fileData,'|') AS s
JOIN @tbl_collist AS c ON s.Item = c.colName
OR (ItemNumber = 1 AND c.colName = 'ID')
WHERE t.fileData LIKE '#%'
)
SELECT
ID = MAX(IIF(s.ItemNumber = 1, s.Item, NULL)),
customername = MAX(IIF(s.ItemNumber = 2, s.Item, NULL)),
prodname = MAX(IIF(s.ItemNumber = 5, s.Item, NULL)),
regency = MAX(IIF(s.ItemNumber = 7, s.Item, NULL))
FROM @tbl_rawData AS t
CROSS APPLY dbo.DelimitedSplit8K(t.fileData,'|') AS s
WHERE t.fileData NOT LIKE '#%'
AND s.ItemNumber IN (SELECT split.ItemNumber FROM split)
GROUP BY t.fileData;
返回:
ID customername prodname regency
--- ------------- --------- -----------
11 jeev uio abap
12 poon uio pyt