更新 UNPIVOTted SQL 表

发布于 2024-07-30 00:42:37 字数 1086 浏览 2 评论 0原文

我一直在努力反对这一点,我确信我只是错过了一些明显的东西,但是......

我在客户的数据库中有一个表,基本上是:

Item_Set_Key int
Item_1       bit
Notes_1      nvarchar(80)
Item_2       bit
Notes_2      nvarchar(80)
Item_3       bit
Notes_3      nvarchar(80)
...

每条记录中有 99 个项目,并且没有改变模式不是一个选项(涉及其他外部考虑因素)。

然而,为了以任何远程类似于智能的方式向用户显示它,我们必须像这样取消它(通过视图):

SELECT i.Item_Set_Key, i.Item_Number, i.Selected, i.Item, i2.Notes, i2.Note
FROM (
SELECT Item_Set_Key, SUBSTRING (Item, 6, 2) AS Item_Number, Selected, Item
    FROM Item_Set
    UNPIVOT (Selected FOR Item IN
        (Item_1, Item_2, Item_3, Item_4, Item_5, ...)
    ) as u
) AS i
LEFT JOIN (
SELECT Item_Set_Key, SUBSTRING (Note, 7, 2) AS Item_Number, Notes
    FROM Item_Set
    UNPIVOT (Notes FOR Note IN
        (Notes_1, Notes_2, Notes_3, Notes_4, Notes_5, ...)
    ) as n
) AS i2 ON i2.Item_Set_Key = i.Item_Set_Key
    AND i2.Item_Number = i.Item_Number

我将其标准绑定到网格。 但是,我对如何为其构建 UpdateCommand 感到困惑,因为文本必须显式命名 SET 中的列,但 Item 和 Note 列中的列名称是动态的,我可以不只是设置所有列,因为每条记录仅包含一个项目/注释对的数据。

有想法吗?

I've been beating my head against this, and I'm sure I'm just missing something obvious, but...

I have a table in a customer's database, that's basically:

Item_Set_Key int
Item_1       bit
Notes_1      nvarchar(80)
Item_2       bit
Notes_2      nvarchar(80)
Item_3       bit
Notes_3      nvarchar(80)
...

There's 99 items in each record, and no changing the schema is not an option (other external considerations involved).

However, in order to display it in anything remotely resembling intelligence to the user, we have to UNPIVOT it (via a View) like this:

SELECT i.Item_Set_Key, i.Item_Number, i.Selected, i.Item, i2.Notes, i2.Note
FROM (
SELECT Item_Set_Key, SUBSTRING (Item, 6, 2) AS Item_Number, Selected, Item
    FROM Item_Set
    UNPIVOT (Selected FOR Item IN
        (Item_1, Item_2, Item_3, Item_4, Item_5, ...)
    ) as u
) AS i
LEFT JOIN (
SELECT Item_Set_Key, SUBSTRING (Note, 7, 2) AS Item_Number, Notes
    FROM Item_Set
    UNPIVOT (Notes FOR Note IN
        (Notes_1, Notes_2, Notes_3, Notes_4, Notes_5, ...)
    ) as n
) AS i2 ON i2.Item_Set_Key = i.Item_Set_Key
    AND i2.Item_Number = i.Item_Number

I do the standard bind of that to the grid. However, I'm at something of a loss as to how to build the UpdateCommand for it, because the text would have to explicitly name the column in the SET, but the column names are dynamic in the Item and Note columns, and I can't just set all of the columns because each record would only have data for one item/note pair.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-08-06 00:42:37

您将无法依赖数据绑定将更改发送回原始数据透视表中的数据库。 相反,您需要将每个更新捕获为单个“工作单元”。 例如,项目 id=92,值=“Tom”。 也许之前第 92 项的值是“Joe”。 您此处的工作单元是更改第 92 项的值。

当用户通过用户界面进行更改时,您可以将每个 UOW 批处理在一起并保留它们,直到它们准备好单击“保存”。 当用户请求保存时,每个捕获的 UOW 都会针对数据库“播放”。 搜索“命令”模式和/或 Jeremy Miller 的一些文章。

另一个想法是你说你不能改变架构,但也许你真的可以。 考虑创建一个未透视格式的真实表。 然后将当前表替换为使用 PIVOT 命令的视图。 实际上,您以更好的设计存储数据,但对于现有应用程序,您可以将其旋转回来。 除非您需要在旋转设计中进行更新,否则这可能会起作用。

最后一个选择是您可以简单地维护两个物理表,然后编写一些复杂的合并操作来定期同步它们。

You won't be able to rely on data-binding to send the changes back to the database in the original pivoted table. Instead you need to capture each update as a single "unit of work." For example, item id=92, value="Tom". Perhaps previously item 92's value was "Joe." Your unit of work here is to change the value for item 92.

As the user makes changes via the user interface you can batch together each UOW and hold on to them until they're ready to click save. When the user asks for the save, each captured UOW is "played" against the database. Search around for the "command" pattern and/or some of Jeremy Miller's articles.

Another thought is you said you can't change the schema but maybe you really can. Consider creating a real table that is in the unpivoted format. Then replace the current table with a view that uses the PIVOT command. In effect you store the data in a better design but for the existing application you PIVOT it back. This could work unless you need to do updates while it's in the pivoted design.

Last option is you could simply maintain two physical tables and then write some complex merge operation to synchronize them periodically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文