在每个新插入上运行过程或函数

时间:2018-04-20 00:07:31

标签: sql-server tsql

有没有办法可以在CREATE TABLE table ( ... )中强制程序或函数在每个插页上运行?

-- For exmaple:
CREATE TABLE table (
  ID INT IDENTITY (1,1) PRIMARY KEY,
  Pass varchar(200) -- Can I do Proc(Pass) to return the output instead?
)

2 个答案:

答案 0 :(得分:1)

您可以在每个新插入上运行触发器并执行您想要的操作。您可以访问包含插入值的触发器中名为inserted的虚拟表

Create trigger name
on tbl
after insert 
as
begin

 ---select * from inserted
--your proc here
end

答案 1 :(得分:0)

You might also take a look at computed columns. They are virtual columns in a table computed by an expression defined when the table is created. E.g.:

CREATE TABLE products
             (id INT,
              price MONEY,
              tax AS price * .15);

-- the column tax is computed as 15% of the price whenever a price is changed (inserts or updates). Though having limitations they come in handy in simple cases.