开发者

How to split CSVs from one column to rows in a new table in MSSQL 2008 R2

Imagine the following (very bad) table design in MSSQL2008R2:

Table "Posts":
| Id (PK, int) | DatasourceId (PK, int) | QuotedPostIds (nvarchar(255)) | [...] 
|     1        |        1               |                               | [...] 
|     2        |        1               |           1                   | [...] 
|     2        |        2               |           1                   | [...] 
[...]
|   102322     |        2               |     123;45345;4356;76757      | [...] 

So, the column QuotedPostIds contains a semicolon-separated list of self-referencing PostIds (Kids, don't do that at home!). Since this design is ugly as a hell, I'd like to extract the values from the QuotedPostIds table to a new n:m relationship table like this:

Desired new table "QuotedPosts":
| QuotingPostId (int) | QuotedPostId (int) | DatasourceId (int) |
|        2            |        1           |         1          |
|        2            |        1           |         2          |
[...]
|      102322         |       123          |         2          |
|     开发者_开发知识库 102322         |      45345         |         2          |
|      102322         |       4356         |         2          |
|      102322         |      76757         |         2          |

The primary key for this table could either be a combination of QuotingPostId, QuotedPostId and DatasourceID or an additional artificial key generated by the database.

It is worth noticing that the current Posts table contains about 6,300,000 rows but only about 285,000 of those have a value set in the QuotedPostIds column. Therefore, it might be a good idea to pre-filter those rows. In any case, I'd like to perform the normalization using internal MSSQL functionality only, if possible.

I already read other posts regarding this topic which mostly dealt with split functions but neither could I find out how exactly to create the new table and also copying the appropriate value from the Datasource column, nor how to filter the rows to touch accordingly.

Thank you!

€dit: I thought it through and finally solved the problem using an external C# program instead of internal MSSQL functionality. Since it seems that it could have been done using Mikael Eriksson's suggestion, I will mark his post as an answer.


From comments you say you have a string split function that you you don't know how to use with a table.

The answer is to use cross apply something like this.

select P.Id,
       S.Value
from Posts as P
  cross apply dbo.Split(';', P.QuotedPostIds) as S
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜