SQL Server 2008 中 PIVOT 的更新
有没有办法在 SQL Server 2008 中对 PIVOTed 表执行更新,其中更改会传播回源表(假设没有聚合)?
Is there a way to perform updates on a PIVOTed table in SQL Server 2008 where the changes propagate back to the source table, assuming there is no aggregation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PIVOT
始终需要在数据透视子句中使用聚合函数。因此总是存在聚合。
所以,不,它不能更新。
您可以根据该语句在视图上放置
INSTEAD OF TRIGGER
,这样您就可以使任何视图可更新。示例此处
PIVOT
s always require an aggregate function in the pivot clause.Thus there is always aggregation.
So, no, it cannot be updatable.
You CAN put an
INSTEAD OF TRIGGER
on a view based on the statement and thus you can make any view updatable.Example here
仅当旋转列形成唯一标识符时,这才真正起作用。 让我们以 Buggy 为例; 这是原始表:
我们希望将其转换为如下所示的表:
为了创建数据透视表,您需要执行以下操作:
因此,您的数据透视表位于
##Pivoted. 现在,您对其中一个时间字段执行更新:
现在
##Pivoted
具有 1980 年 11 月 16 日发生的任务的更新版本的时间,我们希望将其保存回原始表,因此我们使用UNPIVOT
:您会注意到我修改了 Buggy 的示例以删除按星期几进行的聚合。 这是因为如果执行任何类型的聚合,就无法返回和更新。 如果我更新 SUNHours 字段,我如何知道要更新哪个星期日的时间? 这仅在没有聚合的情况下才有效。 我希望这有帮助!
This will only really work if the pivoted columns form a unique identifier. So let's take Buggy's example; here is the original table:
and we want to pivot it into a table that looks like this:
In order to create the pivot, you would do something like this:
So then you have your pivoted table in
##Pivoted
. Now you perform an update to one of the hours fields:Now
##Pivoted
has an updated version of the hours for a task that took place on 11/16/1980 and we want to save that back to the original table, so we use anUNPIVOT
:You'll notice that I modified Buggy's example to remove aggregation by day-of-week. That's because there's no going back and updating if you perform any sort of aggregation. If I update the SUNHours field, how do I know which Sunday's hours I'm updating? This will only work if there is no aggregation. I hope this helps!
这只是一个猜测,但是您可以将查询放入视图中然后更新它吗?
this is just a guess, but can you make the query into a view and then update it?
我不认为这是可能的,但如果您发布有关您试图解决的实际问题的具体信息,某人可能能够为您提供一些有关不同处理方法的建议。
I don't believe that it is possible, but if you post specifics about the actual problem that you're trying to solve someone might be able to give you some advice on a different approach to handling it.