表Call有字段1 type 字段(等于1时 分析an字段,写入an_type字段;2时 分析dn字段,写入dn_type字段;3时 分析ing字段,写入ing_type字段)2 an ,dn,ing 字段中存储号码 ,但是要根据不同号码写入不同的 an_type,dn_type,ing_type(就是在插入这个表时填写这些字段),一个有值时其他两个肯定没有值就是说an,dn,ing这几个字段只有一个字段有值,也就是这个有值的字段在insert时 在进行分析,写入他们的an_type,dn_type,ing_type这个触发器的框架怎么写?
--大概是这样吧create table call
(
[type] int,
[type_value] nvarchar(10),
[an_type] nvarchar(10),
[dn_type] nvarchar(10),
[ing_type] nvarchar(10)
)create trigger ins_call on dbo.call
instead of insert
as
declare @type int
declare @type_value nvarchar(10)
select @type=[type],@type_value=[type_value]
from insertedif @type=1
insert into call([type],[an_type]) values(1,@type_value)
if @type=2
insert into call([type],[dn_type]) values(2,@type_value)
if @type=3
insert into call([type],[ing_type]) values(3,@type_value)
goinsert call([type],[type_value])
values(2,'BBB')go
select * from call
(
[type] int,
[an] nvarchar(10),
[bn] nvarchar(10),
[ing] nvarchar(10),
[an_type] nvarchar(10),
[dn_type] nvarchar(10),
[ing_type] nvarchar(10)
)
(
[type] int,
[an] nvarchar(10),
[dn] nvarchar(10),
[ing] nvarchar(10),
[an_type] nvarchar(10),
[dn_type] nvarchar(10),
[ing_type] nvarchar(10)
)
[an] nvarchar(10),
[dn] nvarchar(10),
[ing] nvarchar(10),
[an_type] nvarchar(10),
[dn_type] nvarchar(10),
[ing_type] nvarchar(10)
如果写instead of insert,就是插入
如果写for insert,就是更新
instead of insert
for insert我的目的就是在插入这条数据得时候分析这个字段后,写入另外一个字段,插入时顺便完成
=--------------------------------
能说明白吗?
(等于1时 an字段会有数据,分析这个字段的数值,用触发器写入an_type字段;
2时 dn字段会有数据,分析这个字段的数值,用触发器写入dn_type字段;
3时 ing字段会有数据,分析这个字段的数值,用触发器写入ing_type字段)当type=1时 只有an字段有值,其他的dn,ing字段没有数值这样说可以理解吗
create table call
(
[type] int,
[an] nvarchar(10),
[dn] nvarchar(10),
[ing] nvarchar(10),
[an_type] nvarchar(10),
[dn_type] nvarchar(10),
[ing_type] nvarchar(10)
)
------------------------
跟疯子的疑问是一样的,最起码应该有个主键吧???
create trigger wsp on call for insert
as
declare @type int
select @type=type from inserted
if(@type=1)
update call set an_type=b.an from call a,inserted b where a.主键=b.主键
else if(@type=2)
update call set dn_type=b.dn from call a,inserted b where a.主键=b.主键
else if(@type=3)
update call set ing_type=b.ing from call a,inserted b where a.主键=b.主键
go
------既然有主键。那就直接把上面的“主键”替换为你的id就行了
create trigger wsp on call for insert
as
declare @type int
select @type=type from inserted
if(@type=1)
update call set an_type=b.an from call a,inserted b where a.id=b.id
else if(@type=2)
update call set dn_type=b.dn from call a,inserted b where a.id=b.id
else if(@type=3)
update call set ing_type=b.ing from call a,inserted b where a.id=b.id
go