Reputation: 10374
I have an Access table which contains two columns: [ID] and [Name]. There are in over 3000 rows of data here. Currently I have copied and pasted the table into a .txt file which is where it needs to live.
Example of how the data looks:
A0001 esfesef fgrdgdrd
A0002 fsefgeses
A0003 safefesf
A0004 vsdrdsrfgsd
A0006 adfaasadsa
A0007 sefse gdsgfd
A0008 wafregsre grdsgr
A0009 sdfsfs ffd
A0010 sfesfe grgrds
A0011 sdf fsfgdsf
A0012 esdgf gsg
A0013 gdrs as gsdfd
A0014 ddsfgb gtrew asfd
A0015 ghdssdf
A0016 sadsaf
A0017 sjhdhj ahumds shsh
I need a way of programmatically doing the following:
Duplicating every row but inserting a /
before the ID and the Description (closing tag)
Wrapping every record in <>
tags
So this is how the data would look:
<A0001><esfesef fgrdgdrd>
</A0001></esfesef fgrdgdrd>
<A0002><fsefgeses>
</A0002></fsefgeses>
<A0003><safefesf>
</A0003></safefesf>
<A0004><vsdrdsrfgsd>
</A0004></vsdrdsrfgsd>
<A0006><adfaasadsa>
</A0006></adfaasadsa>
<A0007><sefse gdsgfd>
</A0007></sefse gdsgfd>
<A0008><wafregsre grdsgr>
</A0008></wafregsre grdsgr>
<A0009><sdfsfs ffd>
</A0009></sdfsfs ffd>
<A0010><sfesfe grgrds>
</A0010></sfesfe grgrds>
<A0011><sdf fsfgdsf>
</A0011></sdf fsfgdsf>
<A0012><esdgf gsg>
</A0012></esdgf gsg>
<A0013><gdrs as gsdfd>
</A0013></gdrs as gsdfd>
<A0014><ddsfgb gtrew asfd>
</A0014></ddsfgb gtrew asfd>
<A0015><ghdssdf>
</A0015></ghdssdf>
<A0016><sadsaf>
</A0016></sadsaf>
<A0017><sjhdhj ahumds shsh>
</A0017></sjhdhj ahumds shsh>
I can put the data into an Excel spreadsheet and connect to this through SQL Server 2008 R2 to perform the query and then copy the results out to a .txt (which is where the data ultimately needs to reside).
Does anyone have any ideas of transforming this data so I do not have to update each record in the .txt file?
Upvotes: 1
Views: 79
Reputation: 123849
I'm not sure why you want to go to the trouble of dumping the data to Excel and then processing it in SQL Server because you can do the same thing right in Access itself:
SELECT lineOut
FROM
(
SELECT ID, 1 AS Seq, "<" & ID & "><" & [Name] & ">" AS lineOut
FROM Table1
UNION ALL
SELECT ID, 2 AS Seq, "</" & [Name] & "></" & ID & ">" AS lineOut
FROM Table1
ORDER BY 1, 2
)
Just save that query and then export it as a text file. (Note that I swapped the order of the closing tags to make them match properly.)
Upvotes: 1
Reputation: 18559
What you asked can be done like this:
WITH CTE AS
(
SELECT
ID, 1 AS RN, '<' + ID + '><' + Name + '>' AS Tag
FROM Table1
UNION
SELECT
ID, 2 AS RN, '</' + ID + '></' + Name + '>' AS Tag
FROM Table1
)
SELECT Tag FROM CTE
ORDER BY ID,RN
However if those are to be valid xml tags you might actually want to switch the order of tags in closing row?
WITH CTE AS
(
SELECT
ID, 1 AS RN, '<' + ID + '><' + Name + '>' AS Tag
FROM Table1
UNION
SELECT
ID, 2 AS RN, '</' + Name + '></' + ID + '>' AS Tag
FROM Table1
)
SELECT Tag FROM CTE
ORDER BY ID,RN
Also, single row might also work for you?
SELECT '<' + ID + '><' + Name + '></' + Name + '></' + ID + '>' AS Tag FROM Table1
Upvotes: 1