默认情况下插入另一列时自动插入一列
Automatically insert into one column while inserting into another column by default
假设我在 table 中有两列。如果我试图插入一列,那么该值也应该自动出现在另一列中。
但由于性能原因我不想使用触发器。
我在添加新列时看到了一些地方,他在数据类型
之后使用了"expression"
请帮帮我。
它被称为计算列。
此示例将创建一个列(名为 InventoryValue),该列将包含基于其他两个列(QtyAvailable 和 单价).
发件人:https://msdn.microsoft.com/en-us/library/ms188300.aspx
CREATE TABLE dbo.Products
(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
, InventoryValue AS QtyAvailable * UnitPrice
);
注:
InventoryValue 的值将在 SELECT 时计算,而不是在 insert/update 期间计算。如果您希望它在插入时计算,那么您需要将其标记为 PERSISTED。如果您在计算中使用 getdate 函数,这一点尤其重要。
CREATE TABLE dbo.Products
(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
, InventoryValue AS QtyAvailable * UnitPrice PERSISTED
);
持久计算列可用于索引和外键。
CREATE TABLE dbo.tbl
(
ID int
, col2 AS ID )
假设我在 table 中有两列。如果我试图插入一列,那么该值也应该自动出现在另一列中。 但由于性能原因我不想使用触发器。
我在添加新列时看到了一些地方,他在数据类型
之后使用了"expression"请帮帮我。
它被称为计算列。
此示例将创建一个列(名为 InventoryValue),该列将包含基于其他两个列(QtyAvailable 和 单价).
发件人:https://msdn.microsoft.com/en-us/library/ms188300.aspx
CREATE TABLE dbo.Products
(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
, InventoryValue AS QtyAvailable * UnitPrice
);
注:
InventoryValue 的值将在 SELECT 时计算,而不是在 insert/update 期间计算。如果您希望它在插入时计算,那么您需要将其标记为 PERSISTED。如果您在计算中使用 getdate 函数,这一点尤其重要。
CREATE TABLE dbo.Products
(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
, InventoryValue AS QtyAvailable * UnitPrice PERSISTED
);
持久计算列可用于索引和外键。
CREATE TABLE dbo.tbl
(
ID int
, col2 AS ID )