0

我有一个包含以下数据的平面文件:

productID,productName,cost
1234,Plasme TV,15000
1234,DVDPlayer,Mixer Grinder,7000

此示例基于商店提供的优惠,即购买等离子电视的人将免费获得价值 7000 的 DVDPlayer 和 Mixer Grinder。因此,商店希望将此平面文件转储到 productID 相同的表中,并且该表应如下所示:

productID    productName              cost
1234         Plasma TV                15000
1234         DVDPlayer,Mixer Grinder  7000

也就是说,在产品列的第一行 Plasma TV 应该出现,在第二行的 productName 列中,DVDPlayer 和 Mixer Grinder 应该出现。

任何人都可以帮我吗?

为此,我需要使用 T-SQL 编写存储过程。

谢谢

4

2 回答 2

2

在逗号分隔的文件中的字段中使用逗号并不好。可能有一些方法可以使用花哨的格式文件来解决这个问题。我的建议是将数据加载到具有单个字符列且没有分隔符的临时表中。

然后,您可以将决赛桌加载为:

insert into t(productID, productName, cost)
    select cast(left(st.line, 4) as int) as ProductId,
           substring(st.line, 6, len(st.line) - 5 - charindex(',', reverse(st.line))) as ProductName,
           cast(right(st.line, charindex(',', reverse(st.line))-1) as float) as cost
    from stagingTable st;

这是使用字符串操作从每一行中提取不同的字段。

于 2013-06-30T06:08:45.920 回答
0

您的数据库设计远非最佳。我在这里真正推荐的是您考虑更新存储表。

“1234,DVDPlayer,Mixer Grinder,7000” – 这如何告诉您具体是哪个 DVDPlayer 以及具体是哪个 Grinder?如果商店有不止一种 DVD 播放器型号的库存怎么办?

如果您无法更新表,那么至少考虑存储产品 ID 而不是产品名称。

于 2013-07-01T15:01:12.187 回答