SQL Server switch clustered table
<p>I wrote the next script in SQL Server 2012, but it fails on constraint.</p>

<p>I have a table with 20 000 000 rows.
I created the same table with same index with partition, but when I switched
the table, SQL Server failed</p>

<p>Here is my code:</p>

<pre><code>CREATE    DATABASE test
USE test

create  Partition Function  
[PF_Table_Log] ([DATETIME2](3)) As Range left   For VALUES
('2016-04-05 00:00:00.000','2016-04-06 00:00:00.000',
'2016-04-07 00:00:00.000','2016-04-08 00:00:00.000')



Create Partition Scheme PS_Table_Log_Datetime
As Partition [PF_Table_Log]
All To ([Primary]);


create  TABLE [Log](
[LogId] [BIGINT] IDENTITY(1,1) NOT NULL,
[ServiceInstanceId] [UNIQUEIDENTIFIER] NULL,
[ServiceId] [UNIQUEIDENTIFIER] NOT NULL,
[Component] [NVARCHAR](100) NULL,
[MachineName] [NVARCHAR](50) NULL,
[Datetime] [DATETIME2](3) NOT NULL,
[Severity] [INT] NOT NULL,
[LogText] [NVARCHAR](max) NULL,
[MessageId] [UNIQUEIDENTIFIER] NULL,
[MessageRole] [INT] NULL
) ON  [PRIMARY]

GO

CREATE CLUSTERED INDEX [PK_Log] ON [Log]
(

[LogId] ASC,
[datetime] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS=ON)               
GO


create  TABLE [Log_new1](
[LogId] [BIGINT] IDENTITY(1,1) NOT NULL,
[ServiceInstanceId] [UNIQUEIDENTIFIER] NULL,
[ServiceId] [UNIQUEIDENTIFIER] NOT NULL,
[Component] [NVARCHAR](100) NULL,
[MachineName] [NVARCHAR](50) NULL,
[Datetime] [DATETIME2](3) NOT NULL,
[Severity] [INT] NOT NULL,
[LogText] [NVARCHAR](max) NULL,
[MessageId] [UNIQUEIDENTIFIER] NULL,
[MessageRole] [INT] NULL
) ON  PS_Table_Log_Datetime (datetime)
GO


 CREATE CLUSTERED INDEX [PK_Log] ON [Log_new1]
 (
[LogId] ASC,[Datetime] asc
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, 
) ON PS_Table_Log_Datetime ([Datetime]) 




ALTER TABLE [Log] SWITCH TO [Log_new1] PARTITION 5
</code></pre>