带索引的视图原因无法在更新中插入重复的键行

时间:2014-06-30 22:24:44

标签: sql-server sql-server-2012

我创建了一个视图,它连接表并在该视图上创建聚簇索引,以便我可以对多个表中所需的所有数据进行全文搜索。它工作得很好但是我得到了不能在具有唯一索引'IX_vPRs'的对象'dbo.vPRs'中插入重复的键行。重复键值为(47)。在proc:

中运行此语句时出错
UPDATE prHdr
SET PRStatus=3
WHERE PrId=@prId

我不明白为什么update语句在视图中插入一行(正如错误所解释的那样)。

有什么想法吗?

定义:

CREATE VIEW [dbo].[vPRs]
    WITH SCHEMABINDING
AS 
SELECT a.PrSeriesId,
       DivisionId [Division], 
       DateStart, 
       DateEnd, 
       Showroom, 
       VendorId, 
      VendName,
      PlmVendorId,
       b.Style, 
      e.Description [Status],
      f.FullName Requester,
        COUNT_BIG(*)AS countbig
FROM [dbo].[PrHdr] a
JOIN [dbo].[PrDtl] b ON a.prId=b.prId
JOIN dbo.PrSeries p ON a.PrSeriesId=p.PrSeriesId
JOIN [dbo].[PrStatus] e on a.PrStatusId = e.PrStatusId
JOIN [dbo].[User] f on p.UserCreate=f.UserName
GROUP BY a.PrSeriesId,
       DivisionId, 
       DateStart, 
       DateEnd, 
       Showroom, 
       VendorId, 
      VendName,
      PlmVendorId,
       b.Style, 
      e.Description,
      f.FullName

GO

/****** Object:  Index [IX_vPRs]    Script Date: 6/30/2014 10:45:49 PM ******/
CREATE UNIQUE CLUSTERED INDEX [IX_vPRs] ON [dbo].[vPRs]
(
    [PrSeriesId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

1 个答案:

答案 0 :(得分:0)

索引视图将数据实现为视图的一部分,这样在查询时它不必转到基础表来获取值。

看起来视图定义不正确或唯一索引denfintion不正确。