How to solve IntelliSense error in auto generated temporal table - sql-server

I'm trying to use temporal tables in my SQL server db project in visual studio.
A sample table
CREATE TABLE [dbo].[Sale Types]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (MAX) NOT NULL,
SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime),
CONSTRAINT [PK_Sale Types] PRIMARY KEY CLUSTERED ([Id] ASC)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.[Sale Types History]));
My problem comes from errors being generated in these auto generated tables (dbo.[Sale Types History])
Error SQL46010: Incorrect syntax near Types

Related

How do I create system versioned tables using SSDT in Visual Studio 2019?

I'm trying to create Table with system versioning using Database Project.
Following schema gives error:
SQL70633: System-versioned temporal table must have history table name explicitly provided.
CREATE TABLE [dbo].[Products]
(
[Id] INT NOT NULL PRIMARY KEY,
[Name] NVARCHAR(255) NOT NULL,
[ModifiedBy] NVARCHAR(127) NULL
)
WITH (SYSTEM_VERSIONING = ON)
GO
With explicit name:
SQL71501: Table: [dbo].[Products] has an unresolved reference to Table [history].[ProductsHistory].
SQL46010: Incorrect syntax near ].
CREATE TABLE [dbo].[Products]
(
[Id] INT NOT NULL PRIMARY KEY,
[Name] NVARCHAR(255) NOT NULL,
[ModifiedBy] NVARCHAR(127) NULL
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [history].ProductsHistory))
GO
I've tried both, latest version of Visual Studio 2019 (16.7.5) and latest preview (16.8.0 Preview 3.2).
The syntax in both cases is invalid. Executing the first query in SSMS returns:
Cannot set SYSTEM_VERSIONING to ON when SYSTEM_TIME period is not defined.
The command needs a PERIOD FOR SYSTEM_TIME clause specifying the columns used to specify the validity period of a record.
The documentation examples show how to create a temporal table with a default, automatically named history table :
CREATE TABLE [dbo].[Products]
(
[Id] INT NOT NULL PRIMARY KEY,
[Name] NVARCHAR(255) NOT NULL,
[ModifiedBy] NVARCHAR(127) NULL,
SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
)
WITH (SYSTEM_VERSIONING = ON)
In this case, the SysStartTime and SysEndTime are used to specify the validity period of a record.
Similar syntax is needed to create a temporal table with a user-specified table name
create TABLE [dbo].[Products]
(
[Id] INT NOT NULL PRIMARY KEY,
[Name] NVARCHAR(255) NOT NULL,
[ModifiedBy] NVARCHAR(127) NULL,
SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductHistory))
It's possible to create the history table on a different schema, eg history, as long as that schema exists, BUT it's probably not a good idea unless this solves some specific problem. The current and history table represent the same entity, depend on each other and have specific security restrictions so storing them in separate schemas can make life harder.
To create the table in a separate schema, first create the schema :
CREATE SCHEMA history
Then use the schema in the table definition:
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = history.ProductHistory))

How to prevent SQL71609 with SSDT on a new temporal table (system-versioned)

I try to use SQL Server Temporal Tables within Visual Studio 2017 and SQL Server Data Tools (SSDT).
But I get immediately following error:
SQL71609: System-versioned current and history tables do not have
matching schemas. Mismatched column: '[dbo].[MyTable].[ValidFrom]'
I don't see any mistake. Do I miss something?
I created a small repository on GIT HUB for reproduction
The current table is defined as:
CREATE TABLE [dbo].[MyTable]
(
[TenantId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF_MyTable_TenantId] DEFAULT
CAST(SESSION_CONTEXT(N'TenantId') AS UNIQUEIDENTIFIER),
[Rn] BIGINT IDENTITY(1,1) NOT NULL,
[Id] UNIQUEIDENTIFIER NOT NULL,
[PropA] INT NOT NULL,
[PropB] NVARCHAR(100) NOT NULL,
[ValidFrom] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL CONSTRAINT [DF_ValidFrom] DEFAULT CONVERT(DATETIME2, '0001-01-01'),
[ValidTo] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL CONSTRAINT [DF_ValidTo] DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
PERIOD FOR SYSTEM_TIME ([ValidFrom], [ValidTo]),
CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED ([Id]),
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory]))
GO
CREATE UNIQUE CLUSTERED INDEX [CIX_MyTable] ON [dbo].[MyTable]([Rn])
GO
And the history table :
CREATE TABLE [dbo].[MyTableHistory]
(
[TenantId] UNIQUEIDENTIFIER NOT NULL,
[Rn] BIGINT IDENTITY(1,1) NOT NULL,
[Id] UNIQUEIDENTIFIER NOT NULL,
[PropA] INT NOT NULL,
[PropB] NVARCHAR(100) NOT NULL,
[ValidFrom] DATETIME2,
[ValidTo] DATETIME2,
);
GO
CREATE CLUSTERED COLUMNSTORE INDEX [COLIX_MyTableHistory]
ON [dbo].[MyTableHistory];
GO
CREATE NONCLUSTERED INDEX [IX_ImpactHistory_ValidFrom_ValidTo_Id]
ON [dbo].[MyTableHistory] ([ValidFrom], [ValidTo], [Id]);
GO
Not really sure why you are getting this particular error message.
I've tested your code on db fiddle and got different errors.
BTW, please note that you don't have to write the history table yourself - if you only set it's name using the SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory]) and not create it, SQL Server will generate it automatically for you - as can be seen in this fiddle.
For the first attempt I've got this error:
Msg 13518 Level 16 State 1 Line 20
Setting SYSTEM_VERSIONING to ON failed because history table 'fiddle_e3d361da65804a39b041c8149132b443.dbo.MyTableHistory' has IDENTITY column specification. Consider dropping all IDENTITY column specifications and trying again.
So I've removed the identity from [Rn] column in the history table and tried again.
Then I've got this error:
Msg 13530 Level 16 State 1 Line 20
Setting SYSTEM_VERSIONING to ON failed because system column 'ValidFrom' in history table 'fiddle_d6660ab11cdc448dba35790867169a14.dbo.MyTableHistory' corresponds to a period column in table 'fiddle_d6660ab11cdc448dba35790867169a14.dbo.MyTable' and cannot be nullable.
So I've changed both the ValidFrom and ValidTo columns to NOT NULL and finally got it working.
The working version is copied to here:
CREATE TABLE [dbo].[MyTableHistory]
(
[TenantId] UNIQUEIDENTIFIER NOT NULL,
[Rn] BIGINT NOT NULL,
[Id] UNIQUEIDENTIFIER NOT NULL,
[PropA] INT NOT NULL,
[PropB] NVARCHAR(100) NOT NULL,
[ValidFrom] DATETIME2 NOT NULL,
[ValidTo] DATETIME2 NOT NULL,
);
CREATE CLUSTERED COLUMNSTORE INDEX [COLIX_MyTableHistory]
ON [dbo].[MyTableHistory];
CREATE NONCLUSTERED INDEX [IX_ImpactHistory_ValidFrom_ValidTo_Id]
ON [dbo].[MyTableHistory] ([ValidFrom], [ValidTo], [Id]);
CREATE TABLE [dbo].[MyTable]
(
[TenantId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF_MyTable_TenantId] DEFAULT CAST(SESSION_CONTEXT(N'TenantId') AS UNIQUEIDENTIFIER),
[Rn] BIGINT IDENTITY(1,1) NOT NULL,
[Id] UNIQUEIDENTIFIER NOT NULL,
[PropA] INT NOT NULL,
[PropB] NVARCHAR(100) NOT NULL,
[ValidFrom] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL CONSTRAINT [DF_ValidFrom] DEFAULT CONVERT(DATETIME2, '0001-01-01'),
[ValidTo] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL CONSTRAINT [DF_ValidTo] DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
PERIOD FOR SYSTEM_TIME ([ValidFrom], [ValidTo]),
CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED ([Id]),
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory]))
CREATE UNIQUE CLUSTERED INDEX [CIX_MyTable] ON [dbo].[MyTable]([Rn])

Do temporal tables in SQL Server 2017 support foreign keys?

If I try to add a foreign key to a temporal table in Visual Studio, it can't resolve the reference to the primary table for the key. This happens whether that table is also a temporal table or is not a temporal table.
The docs say you cannot have foreign keys in the history table for a temporal table... but they don't say you can't have them in the temporal table itself.
Is it possible ?
CREATE TABLE [Doc].[Document]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[SysStart] DATETIME2 (7) GENERATED ALWAYS AS ROW START NOT NULL DEFAULT CAST('1900-1-1 00:00:00.0000000' AS datetime2),
[SysEnd] DATETIME2 (7) GENERATED ALWAYS AS ROW END NOT NULL DEFAULT CAST('9999-12-31 12:59:59.9999999' AS datetime2),
[Name] NVARCHAR(250) NOT NULL,
[TypeId] INT NOT NULL,
[InActive] BIT NOT NULL DEFAULT 0,
[UpsertedBy] NVARCHAR(100) NOT NULL,
[DateAndTime] DATE NOT NULL DEFAULT getdate(),
CONSTRAINT [FK_Document_ToTable]
FOREIGN KEY ([TypeId]) **REFERENCES [Type]([Id])**,
PERIOD FOR SYSTEM_TIME ([SysStart], [SysEnd])
)
WITH (SYSTEM_VERSIONING = ON(HISTORY_TABLE=[Doc].[Document_HISTORY], DATA_CONSISTENCY_CHECK=ON))
Yes it's perfectly possible.
The below works fine
CREATE TABLE Foo(FooId INT PRIMARY KEY);
CREATE TABLE Bar
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
FooId INT REFERENCES Foo, /*Works fine*/
[SysStart] DATETIME2 (7) GENERATED ALWAYS AS ROW START NOT NULL DEFAULT CAST('1900-1-1 00:00:00.0000000' AS datetime2),
[SysEnd] DATETIME2 (7) GENERATED ALWAYS AS ROW END NOT NULL DEFAULT CAST('9999-12-31 12:59:59.9999999' AS datetime2),
PERIOD FOR SYSTEM_TIME ([SysStart], [SysEnd])
)
WITH (SYSTEM_VERSIONING = ON)

transact sql temporal table and user-defined data type

Sorry for my english.
I have the following definitions in a visual studio 2017 dacpac project (target platform = SQL SERVER 2016).
CREATE SCHEMA [Demo]
CREATE TYPE Demo.Code FROM NVARCHAR(16) NOT NULL
CREATE TABLE [Demo].[Gear] (
[Id] int identity(1,1) primary key,
[Name] nvarchar(25) not null,
[Code] [Demo].[Code],
[rowguid] uniqueidentifier constraint [DF_Gear_rowguid] default (newid()) rowguidcol not null,
[ValidStart] DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
[ValidEnd] DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME([ValidStart], [ValidEnd])
)
WITH ( SYSTEM_VERSIONING = ON(HISTORY_TABLE = [Histo].[Gear]) )
GO
After build, I have this error :
SQL71609: System-versioned current and history tables do not have
matching schemas. Mismatched column: '[Demo].[Gear].[Code]'.
Without user defined data type, it's okay. Is someone knowing that's the problem ?

why can not build sql server project on VS with this object

If execute against database it works! but trying build on Vs sql server project it doesn't.
CREATE TABLE [dbo].[Recebimento_Arquivo] (
[RAr_Codigo] INT IDENTITY (1, 1) NOT NULL,
[Rar_ID_FS] UNIQUEIDENTIFIER DEFAULT (newid()) ROWGUIDCOL NOT NULL,
[Rec_Codigo] BIGINT NULL,
[FAr_Codigo] INT NOT NULL,
[RAr_Arquivo] VARBINARY (MAX) FILESTREAM NULL,
[RAr_Dat_Cadastro] DATETIME CONSTRAINT [dnfRecebimentoArquivo_RArDatCadastro] DEFAULT (getdate()) NULL,
CONSTRAINT [PK_Recebimento_Arquivo] PRIMARY KEY CLUSTERED ([RAr_Codigo] ASC) ON [FG_ARQUIVOS_DADO],
CONSTRAINT [FK_Recebimento_Arquivo_FAr_Codigo] FOREIGN KEY ([FAr_Codigo]) REFERENCES [dbo].[Formato_Arquivo] ([FAr_Codigo]),
UNIQUE NONCLUSTERED ([Rar_ID_FS] ASC) ON [FG_ARQUIVOS_DADO]
) FILESTREAM_ON [FG_ARQUIVOS_FS_01];
It results
Severity Code Description Project File Line Suppression State
Error SQL71566: Filegroup: [FG_ARQUIVOS_FS_01] cannot not be set on both the Table: [dbo].[Recebimento_Arquivo] and the clustered Primary Key: [dbo].[PK_Recebimento_Arquivo].
At first sight, looks to be a bug in VS SQL Server projects.
As other comments say, you cannot speecify different filegroups for clustered key and for table. But that's not what you do. The final filegroup specification is for the filestream data that you store in the varbinary(max) column. Based on the syntax description in Books Online, this should be valid - and the fact that this runs when you execute it on SQL Server directly confirms that.
Ans is in your error itself,
Severity Code Description Project File Line Suppression State
Error SQL71566: Filegroup: [FG_ARQUIVOS_FS_01] cannot not be set on both the Table: [dbo].[Recebimento_Arquivo] and the clustered Primary Key: [dbo].[PK_Recebimento_Arquivo].
you need to have different FileGroup and Primary Key Identifier.

Resources