Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

CTE Parse

$
0
0

Currntly I concatenate XML tags and & lookup temp table to return the name at postition and assign the tag, then XmlTransform to add the customer id to all the parent & child records, works great, sometimes, server memory errors that I have no control over, I'm not the dba so this project has gone on too long, I'd like to abandon the XmlTransform and instead use a CTE or nested CTE for the below desired output

There is no parent child identifiers except that children follow the parents and the information row PID appears after the header row. I've tried recursive CTE's with no positive results, as I'm a novice at CTE. 

 

My thought was to not use a while loop, which I can fetch the first MSH and all the children up to the next MSH and do the deed, but it's preferred to use CTE instead.

There are many examples but they all appear to depend on an fk or other moniker to denote the children for a particular header MSH & PID personal Id row. Further this is actually a string MSH|LCA|20131112 that I'm using a CTE to parse into a table, rows& value at rowno. And, this is Calinx1.3 a subset of HL7 in case anyone's familiar.

declare @t TABLE (RecId INT NOT NULL ,[Name] VARCHAR(25) ,Value varchar(25));
declare RE @r TABLE (RecId INT NOT NULL,[Name] VARCHAR(25) ,Value varchar(25));
INSERT INTO @t VALUES(1,      'MSH', 'LCA|20131112');
INSERT INTO @t VALUES(2,      'PID', 'AHF000001');
INSERT INTO @t VALUES(3,      'OBR','1');
INSERT INTO @t VALUES(4,      'OBR','2');
INSERT INTO @t VALUES(5,      'OBX','1');
INSERT INTO @t VALUES(6,      'OBX','2');
INSERT INTO @t VALUES(7,      'MSH', 'LCA|20131112');
INSERT INTO @t VALUES(8,      'PID', 'AHF000002');
INSERT INTO @t VALUES(9,      'OBR','1');
INSERT INTO @t VALUES(10,     'OBX','1');
INSERT INTO @t VALUES(11,     'MSH', 'LCA|20131112');
INSERT INTO @t VALUES(12,     'PID', 'AHF000003');
INSERT INTO @t VALUES(13,     'OBR','1');
INSERT INTO @t VALUES(14,     'OBX','1');

/* desired output

RecIdName Value
 1MSH 20131112-LCA-AHF000001
 2PID AHF000001
 4OBR AHF000001
 5OBR AHF000001
 6OBX AHF000001
 7OBX AHF000001
 8MSH 20131112-LCA-AHF000002
 9PID AHF000002
 10OBR AHF000002
 11MSH 20131112-LCA-AHF000003
 12PID AHF000003
 13OBR AHF000003
 14OBX AHF000003

*/



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>