无法插入分区视图

时间:2014-04-02 10:45:27

标签: sql view partitioning

我搜索了网络和许多示例,但无法确定我收到此错误的原因:

  

UNION ALL view' devCascade_Gold1_hr.dbo.CustomAudit_View'不是   可更新,因为找不到分区列。

我正在创建一些表格,其中包含一个脚本,该脚本旨在获取大量数据并将其推送到分区视图中以更好地处理它。出于某种原因,我无法在视图中插入数据并获得上述错误。

我已经检查了一切。主键 - 检查,无标识列 - 检查。我插入表格,我正在测试克制,但不是视图。

以下是它的全部构建方式:

    SET NOCOUNT ON
DECLARE @DataTabLabel as nvarchar(100), @sql as nvarchar(4000), @sqlView as nvarchar(max)
DECLARE @monthcount as tinyint, @filter as nvarchar(200), @droptext as nvarchar(80)
DECLARE @counter as int =1

DECLARE cur cursor for
select DISTINCT DataTabLabel from IntranetStdDataRequestCache
open cur
fetch next from cur into @DataTabLabel

IF OBJECT_ID ('dbo.CustomAudit_View', 'V') IS NOT NULL
  DROP VIEW dbo. CustomAudit_View

SET @sqlView = 'CREATE VIEW CustomAudit_View AS
                SELECT * FROM CustomAudit'+CAST(@counter as nvarchar(3))
while @@FETCH_STATUS = 0
BEGIN

    SET @filter = 'datatablabel = ''' + @DataTabLabel + ''''

    SET @droptext = 'DROP TABLE dbo.CustomAudit' + CAST(@counter as nvarchar(3))
    IF OBJECT_ID ('dbo.CustomAudit'+CAST(@counter as nvarchar(3)), 'U') IS NOT NULL
        EXEC sp_executesql @droptext

    SET @sql = 'CREATE Table CustomAudit'+CAST(@counter as nvarchar(3)) +' (
          layoutid bigint ,
          displayemployeeid nvarchar (50) COLLATE database_default ,
          forename nvarchar (50) COLLATE database_default ,
          surname nvarchar (50) COLLATE database_default ,
          datatablabel nvarchar (50) COLLATE database_default 
          CONSTRAINT CK_CustomAudit'+CAST(@counter as nvarchar(3))+' CHECK ('+@filter+'),
          requesteddatetime datetime,
          username nvarchar (50) COLLATE database_default ,
          updatelevel nvarchar (20) COLLATE database_default ,
          updatetype nvarchar (20) COLLATE database_default ,
          field nvarchar (50) COLLATE database_default ,
          CurrentValue nvarchar (400) COLLATE database_default ,
          Value nvarchar (400) COLLATE database_default ,
          authorised nvarchar (20) COLLATE database_default ,
          authoriserdatetime datetime ,
          authemployeedesc nvarchar (100),
          recruitment bit
        CONSTRAINT PK_CustomAudit'+CAST(@counter as nvarchar(3))+' PRIMARY KEY CLUSTERED (displayemployeeid,datatablabel,requesteddatetime)
    )

    CREATE INDEX CustomAudit'+CAST(@counter as nvarchar(3))+'_AG ON CustomAudit'+CAST(@counter as nvarchar(3))+' (displayemployeeid, datatablabel) INCLUDE (field,requesteddatetime,username)
    CREATE INDEX CustomAudit'+CAST(@counter as nvarchar(3))+'_empid ON CustomAudit'+CAST(@counter as nvarchar(3))+' (displayemployeeid)
    CREATE INDEX CustomAudit'+CAST(@counter as nvarchar(3))+'_reqdate ON CustomAudit'+CAST(@counter as nvarchar(3))+' (requesteddatetime)'
    EXEC sp_executesql @sql
    PRINT 'dbo.CustomAudit'+CAST(@counter as nvarchar(3))+' Created'
    PRINT 'Filter: ' + @filter

    IF @counter <> 1
    BEGIN
        SET @sqlView = @sqlView + ' UNION ALL 
               SELECT * FROM ' + 'CustomAudit'+CAST(@counter as nvarchar(3))
    END
    SET @counter = @counter + 1

fetch next from cur into @DataTabLabel
END

IF OBJECT_ID ('dbo.CustomAudit_View', 'V') IS NOT NULL
  DROP VIEW dbo. CustomAudit_View


EXEC sp_executesql @sqlView
PRINT 'CustomAudit_View Created'

close cur
Deallocate cur
SET NOCOUNT OFF

表生成正常 - 他们的个人检查工作完美。 该视图还可以生成并且可以毫无问题地进行查询 - 即使执行计划显示它在尝试获取数据时仅使用正确的约束表。 在这里提供答案的人赢得了我的全部心灵。

我甚至尝试删除日期字段并再次生成它,只是因为我不断看到提到的时间戳问题:

INSERT INTO CustomAudit_View
(layoutid , displayemployeeid, forename , surname , datatablabel,  username,
      updatelevel , updatetype, field , CurrentValue , Value, authorised ,  authemployeedesc,  recruitment)
      VALUES
      (83921,   33134,  'Postponement',' One'   ,'AUTO ENROLMENT',  'v',    'Direct','update'   ,'IsMemberOfQualifyingPensionScheme',
        0,  1   ,'N/A'  ,   'N/A',  0) 

但是没有 - 仍然拒绝插入视图。 我的表60是唯一一个将Auto Enrolement设置为约束的表,所以现在我输了:

  

CustomAudit60 datatablabel([datatablabel] =&#39; AUTO ENROLLMENT&#39;)

1 个答案:

答案 0 :(得分:0)

我的灵魂也是我这一天。 你想知道解决方案吗?结果我的动态代码为多于一个表设置相同的检查约束:

DECLARE cur cursor for
select DISTINCT DataTabLabel from IntranetStdDataRequestCache a
UNION SELECT DISTINCT TabLabel FROM intranetpagesx
open cur

现在有效!!!

非常感谢您为本网站提供全面的可能性列表: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=54747