SQL Server - 使用当前的 GetDate 过滤器创建架构绑定索引视图
我想创建以下索引视图:
CREATE VIEW [Cic].[vwMarker] WITH SCHEMABINDING
AS
Select
SubId,
marker.EquipmentID,
marker.ReadTime,
marker.CdsLotOpside,
marker.CdsLotBackside,
marker.CdteLotOpside,
marker.CdTeLotBackside
From dbo.Marker
Where dbo.Marker.ReadTime >= Convert(dateTime,'10/5/2011',120)
GO
CREATE UNIQUE CLUSTERED INDEX IX_vwMarker_ReadTime_EquipmentID
ON Cic.vwMarker (ReadTime, EquipmentID);
这工作正常。但是,我真正想做的是仅在此视图中包含截至查询视图的当前日期/时间为止两天或更晚的行。我找不到方法来执行此操作,因为我无法在Where谓词中使用GetDate(),因为它是不确定的。换句话说,我想做这样的事情,但不能:
Where dbo.Marker.ReadTime >= Convert(dateTime,DateAdd(dd,-2,GetDate()) ,120)
有没有办法解决这个问题?
I want to create the following indexed view:
CREATE VIEW [Cic].[vwMarker] WITH SCHEMABINDING
AS
Select
SubId,
marker.EquipmentID,
marker.ReadTime,
marker.CdsLotOpside,
marker.CdsLotBackside,
marker.CdteLotOpside,
marker.CdTeLotBackside
From dbo.Marker
Where dbo.Marker.ReadTime >= Convert(dateTime,'10/5/2011',120)
GO
CREATE UNIQUE CLUSTERED INDEX IX_vwMarker_ReadTime_EquipmentID
ON Cic.vwMarker (ReadTime, EquipmentID);
This works fine. However, what I would really like to do is to only include rows in this view that are two days old or newer, as of the current date/time the view is queried. I can't find a way to do this because I cannot use GetDate() in the Where predicate because it is non-deterministic. In other words, I'd like to do something like this, but cannot:
Where dbo.Marker.ReadTime >= Convert(dateTime,DateAdd(dd,-2,GetDate()) ,120)
Is there a way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIK 您不会绕过 SCHEMABINDING 要求的确定性函数。你总是会收到错误
如果 Marker 只是一个表,我不确定索引视图相对于基础表 (ReadTime, EquipmentID) 上具有相同聚集索引的表的普通视图是否具有任何性能优势>
但是,如果“Marker”本身是一个组合,例如
VIEW
,或者如果您不想更改 Marker 表上的聚集索引,那么您可能会考虑以下操作:GetDate
过滤器。Sql Fiddle 示例在这里
即类似:
AFAIK you aren't going to get around the deterministic function for the SCHEMABINDING requirement. You'll always receive the error
If Marker is just a single table, I'm not sure that an indexed view would have any performance benefit over a normal view against the table with the same clustered index on the underlying table of
(ReadTime, EquipmentID)
However, if "Marker" is itself a composite such as a
VIEW
, OR if you don't want to change the Clustered Index on the Marker table, then you might consider something like:GetDate
filter.Sql Fiddle example here
i.e. Something like: