I' using the latest version of SQL Server 2016 Developer edition and SSMS (13.0.15600.2).
There is a possible bug in importing Data tier Application (bacpac
) that contain any temporal tables and any object (function. view) that references them with 'WITH SCHEMABINDING'.
It is perfectly possible to export such database to backpac but when importing it back - there is an error:
Error SQL72014: .Net SqlClient Data Provider: Msg 3729, Level 16, State 4, Line 1 Cannot ALTER 'Table2Version' because it is being referenced by object 'BindTest'. Error SQL72045: Script execution error. The executed script: ALTER TABLE [dbo].[Table2Version] SET (SYSTEM_VERSIONING = OFF);
The smallest DB creation script that will reproduce this problem is:
USE [master]
GO
CREATE DATABASE [temptest]
GO
ALTER DATABASE [temptest] SET COMPATIBILITY_LEVEL = 130
GO
USE [temptest]
GO
CREATE SCHEMA [History]
GO
CREATE TABLE [History].[Table2Version](
[Id] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[SysStartTime] [datetime2](0) NOT NULL,
[SysEndTime] [datetime2](0) NOT NULL
) ON [PRIMARY]
GO
CREATE CLUSTERED INDEX [ix_Table2Version] ON [History].[Table2Version]
([SysEndTime] ASC, [SysStartTime] ASC )
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table2Version](
[Id] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[SysStartTime] [datetime2](0) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
[SysEndTime] [datetime2](0) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
CONSTRAINT [PK__Table2Version] PRIMARY KEY NONCLUSTERED ( [Id] ASC ),
PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
) ON [PRIMARY]
WITH ( SYSTEM_VERSIONING = ON (HISTORY_TABLE = [History].[Table2Version] , DATA_CONSISTENCY_CHECK = ON ))
GO
CREATE FUNCTION [dbo].[BindTest]()
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT Id AS id
from dbo.Table2Version ;
GO
Is it really a bug or I'm doing something wrong?
This seems to be a bug: https://social.msdn.microsoft.com/Forums/en-US/b6564949-2919-4af6-9560-832169750c1f/possible-bug-with-bacpac-import-and-temporal-tables?forum=ssdt
Posted it on Microsoft Connect: https://connect.microsoft.com/SQLServer/Feedback/Details/3073649