简体   繁体   English

如何确定视图的列是派生还是常量?

[英]How to determine if a column of a view is derived or constant?

Let's say I have the following table: 假设我有下表:

create table t_Item (
    ItemID int not null identity(1,1) constraint PK_Item primary key,
    Description varchar(256) not null,
    Price decimal(10,2) not null
)

and the following view: 以及以下观点:

create view Item as
  select ItemID
        ,Description
        ,Price
        ,1.09 Tax
        ,Price * 1.09 TaxedPrice
    from t_Item

TaxedPrice is a derived column, and Tax is a constant column. TaxedPrice是派生列, Tax是常量列。

Therefore, I can't insert or update any of them. 因此,我无法插入或更新它们中的任何一个。 The first following query would pass, whereas the other ones would fail with an error. 第一个跟随查询将通过,而其他查询将失败并出现错误。

insert into Item (Description, Price) values ('Test item', 14.00)

insert into Item (Description, Price, TaxedPrice) values ('Test item', 14.00, 15.26)

insert into Item (Description, Price, Tax) values ('Test item', 14.00, 1.09)

And here is the returned error message: 这是返回的错误消息:

Update or insert of view or function 'Item' failed because it contains a derived or constant field. 更新或插入视图或功能“项目”失败,因为它包含派生或常量字段。

Is there a way, maybe with the system views, to list the view columns which must not be updated? 有没有办法,可能有系统视图,列出不能更新的视图列?

Looks like there is no system view saves information you are looking for. 看起来没有系统视图保存您正在寻找的信息。 You can find out derived column or constant column by parsing the view definition or by exception handling...not good, but didn't find other ways... 您可以通过解析视图定义或通过异常处理找出派生列或常量列...不好,但没有找到其他方法...

  1. Any columns come from the following aggregation functions, considered as derived column. 任何列都来自以下聚合函数,被视为派生列。 AVG, COUNT,SUM,MIN,MAX,GROUPING,STDEV,STDEVP,VAR,VARP AVG,COUNT,SUM,MIN,MAX,GROUPING,STDEV,STDEVP,VAR,VARP
  2. If the view definition contains following syntax, all columns considered as derived column. 如果视图定义包含以下语法,则所有列都被视为派生列。 UNION, UNION ALL, CROSSJOIN, EXCEPT, INTERSECT UNION,UNION ALL,CROSSJOIN,EXCEPT,INTERSECT
  3. The columns affected by GROUP BY, HAVING, DISTINCT, are also considered as derived column. 受GROUP BY,HAVING,DISTINCT影响的列也被视为派生列。

I don't think this cover all scenarios, but a start point to write the parser. 我不认为这涵盖所有场景,而是编写解析器的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM