简体   繁体   中英

Transforming data taken from Access in SQL Server 2008 R2

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:

  1. Duplicating every row but inserting a / before the ID and the Description (closing tag)

  2. 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?

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

SQLFiddle DEMO

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

SQLFiddle DEMO

Also, single row might also work for you?

SELECT '<' + ID + '><' + Name + '></' + Name + '></' + ID + '>' AS Tag FROM Table1

SQLFiddle DEMO

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM