建立觸發器後,不能像表中插入資料,是什麼原
use [cai2016]
go/****** object: trigger [dbo].[trigger_yunum] script date: 2017/3/29 17:30:30 ******/
set ansi_nulls on
goset quoted_identifier on
goalter trigger [dbo].[trigger_yunum]
on [dbo].[newmoo_case_cgxd]
for insert
asbegin
declare @id bigint select @id = id from inserted
declare @purchase_id varchar select @purchase_id = purchase_id from inserted
declare @slength nvarchar select @slength = slength from inserted
declare @engname nvarchar select @engname = engname from inserted
declare @swidth nvarchar select @swidth = swidth from inserted
declare @kehu_id bigint select @kehu_id = kehu_id from inserted
declare @caseid bigint select @caseid =caseid from inserted
declare @productnum int select @productnum = productnum from inserted
declare @pay numeric select @pay = pay from inserted
declare @costprice numeric select @costprice = costprice from inserted
declare @pid_p bigint select @pid_p =pid_p from inserted
declare @cz int select @cz = cz from inserted
declare @productremarks nvarchar select @productremarks = productremarks from inserted
declare @weight numeric select @weight = weight from inserted
update a set a.overnum=(select a.id, a.overnum, a.oknum, a.yunum, b.caseid, b.productnum, b.ischeck, sum(b.productnum) from newmoo_case a, newmoo_case_cgxd b where b.caseid=a.id and b.ischeck=3)
enduse test
go-- 表1(id,productnum),建立幾個測試資料
if object_id('表1') is null
begin
create table 表1(id int,productnum numeric(10,2))
insert into 表1
select 1001,10.5 union all
select 1001,20.5 union all
select 1001,30.5 union all
select 1001,30.5 union all
select 1002,20.2 union all
select 1002,30.2
end-- select * from 表1
-- 表2(id,overnum,upd),建立幾個測試資料,彙總表
if object_id('表2') is null
begin
create table 表2(id int,overnum numeric(10,2),upd datetime )
insert into 表2
select 1001,0.0,getdate() union all
select 1002,0.0,getdate()
end-- select * from 表2
go-- 上面的是測試資料,下面開始建立 觸發器
alter trigger [dbo].[tr_表1] --第1次執行時,create,
on [dbo].[表1]
for insert
asbegin
declare @id int
select @id=id from inserted
update a set a.overnum=b.productnum,upd=getdate()
from 表2 a,(select id,productnum=sum(productnum) from 表1 where id=@id group by id) b
where a.id=b.id
endgo
-- 測試往「表1」插入資料,看錶2是否更新,檢視前後變化
select * from 表2
goinsert into 表1(id,productnum) values(1002,30.5)
select * from 表2
迴圈插入資料 建立觸發器 與效果檢驗
已有資料不夠檢驗,使用插入新的資料。use library 4 insert into reader rno,rname,r rage,reducation values r01 李東 男 22,研究生 然後建立觸發器。可以使用reader的刪除觸發器來進行borrow的刪除 if object i...
建立表序列的觸發器
建立表序列的觸發器 作用 直接往表中貼資料時讓序列的字段自動生成 檢視方法 一般為 1 觸發器使用者名稱.表名 trg,view就能夠看到相應的觸發器的 2 通過view對應的表看到最後的trigger段可以看到enable的trigger 實現 create or replace trigger ...
觸發器約束表中資料重複
表a中有兩個欄位code和channelid 想寫乙個觸發器,在新增或者修改的時候,不允許有code和chanelid同時重複的 比如 channelid 1 的記錄裡面不能有兩個code一樣的,但是channelid不同的話,code可以相等 幫寫個觸發器或者約束,謝謝了 if object id...