[英]Create indeterminant CHECK CONSTRAINT that works in SQL Server Partitioned View
A SQL partitioned view reads tables like any other view. SQL 分区视图像读取任何其他视图一样读取表。 However, what makes them special is that they allow me to write to the underlying tables through when a portioning key column is indicated by using a
CHECK CONSTRAINT
on the underlying tables.但是,它们的特别之处在于,当通过在基础表上使用
CHECK CONSTRAINT
指示分区键列时,它们允许我写入基础表。
For example, here a CHECK CONSTRAINT
validates the [Year] column value is always 2022
.例如,此处的
CHECK CONSTRAINT
验证 [Year] 列值始终为2022
。 The value of 2022
in the CHECK
is a hard coded (deterministic) value. CHECK
中的值2022
是硬编码(确定性)值。 During a SELECT
operation, the view ignores the CHECK
but during an INSERT
operation, the CHECK
instructs the view to which table it should insert the record(s).在
SELECT
操作期间,视图忽略CHECK
,但在INSERT
操作期间, CHECK
指示视图应将记录插入到哪个表。
CREATE TABLE Part2022
(
Id UNIQUEIDENTIFIER UNIQUE CLUSTERED
, Year INT CONSTRAINT req2022 CHECK (Year = 2022)
PRIMARY KEY NONCLUSTERED (Id, Year)
);
Using this approach, a partitioning view would look like this:使用这种方法,分区视图将如下所示:
CREATE VIEW Parts AS
SELECT * FROM Part2022
UNION ALL SELECT * FROM Part2021
UNION ALL SELECT * FROM Part2020
This works just fine.这很好用。
I understand the issue, but I wonder if some clever data engineer out there has figured a workaround that would enable a different approach - something similar to this:我理解这个问题,但我想知道是否有一些聪明的数据工程师想出了一个解决方法来启用不同的方法——类似于此:
CREATE TABLE PartCurrent
(
Id UNIQUEIDENTIFIER UNIQUE CLUSTERED
, Year INT CONSTRAINT reqCurrent CHECK (Year = YEAR(GETUTCDATE()))
PRIMARY KEY NONCLUSTERED (Id, Year)
);
Of course, this does not work.当然,这是行不通的。 Though the
CHECK CONSTRAINT
can be applied to the table without issue, including this table in a partitioned view now results in:虽然
CHECK CONSTRAINT
可以毫无问题地应用于表,但在分区视图中包含此表现在会导致:
Msg 4436, Level 16, State 12, Line 53 view is not updatable because a partitioning column was not found.
消息 4436,级别 16,State 12,第 53 行视图不可更新,因为未找到分区列。
Q: Is there a way to use an indeterminate CHECK CONSTRAINT
with a partitioned view?问:有没有办法对分区视图使用不确定的
CHECK CONSTRAINT
?
PS: I know what partitioned tables are and when to use them in a model. In this case, though, they do not meet the requirements. PS:在model中我知道什么是分区表,什么时候使用分区表。但是,在这种情况下,它们不符合要求。 As a result, I am asking about partitioned views.
结果,我问的是分区视图。 Thx.
谢谢。
Answer: Though a check constraint condition can be dynamic in a table, if that table is in a partition view, checks on partition key columns must be hardcoded.回答:虽然检查约束条件在表中可以是动态的,但如果该表在分区视图中,则必须对分区键列的检查进行硬编码。 This is by design.
这是设计使然。
That is to say, you must do something like this:也就是说,您必须执行以下操作:
CREATE TABLE Part2022
(
Id UNIQUEIDENTIFIER UNIQUE CLUSTERED
, Year INT CONSTRAINT req2022 CHECK (Year = 2022)
PRIMARY KEY NONCLUSTERED (Id, Year)
);
You cannot do something like this:你不能这样做:
CREATE TABLE Part2022
(
Id UNIQUEIDENTIFIER UNIQUE CLUSTERED
, Year INT CONSTRAINT req2022 CHECK (Year = YEAR(GETUTCDATE()))
PRIMARY KEY NONCLUSTERED (Id, Year)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.