如下,错误213 16 插入错误:列名或所提供值的数目与表定义不匹配?为什么?

判断 如果向表A添加了数据,只要添加了,就把部分字段中的值给别的表里,触发器怎么写?
CREATE TRIGGER InsertTax ON 数据总表
FOR INSERT
AS
BEGIN
DECLARE
@nsrsbh varchar(255),
@nsrmc varchar(255),
@ssqq varchar(255),
....
select @nsrsbh=纳税人识别号,@nsrmc=纳税人名称,@yh=开户银行,@yhzh=银行账号,@zgjgdm=主管税务机关代码,@zgjgmc=主管税务机关名称,@zsl=征收率,@jehj=金额合计 from Inserted
Insert into 单位表 select @nsrsbh,@nsrmc,@yh,@yhzh,@zgjgdm,@zgjgmc,@zsl,@jehj
END
帮忙看看错在哪?

DIEA
浏览 304回答 2
2回答

牧羊人nacy

Table Name: TB_1,TB_2 (两个table 结构完全一样)Create Table TB_1(ID Int, Name_1 varchar(20),Status varchar(20) ,Type Varchar(20))Create Table TB_2(ID Int, Name_1 varchar(20),Status varchar(20) ,Type Varchar(20))-- 为Tb_1创建Trigger, 当 TB_1 发生变化时,变化的信息(Insert ,Update,Delete)将保存于 TB_2 中,Update是两条记录,Update 之前和之后 都要保存盐类Create trigger Trig_t2On Tb_1For Insert,Update,DeleteAsdeclare @id int,@name_1 varchar(20),@status varchar(20)If not exists(Select 1 from deleted) ----InsertBeginselect @id=id, @name_1=name_1,@status=status from insertedinsert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'Insert')EndIf exists(select 1 from inserted) and exists(select 1 from deleted) ---UpdateBegin/* Update Before */select @id=id, @name_1=name_1,@status=status from deletedinsert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'UpdateBefor')/* Update End */select @id=id, @name_1=name_1,@status=status from insertedinsert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'UpdateEnd')EndIf not exists(select 1 from inserted) ---DeleteBeginselect @id=id, @name_1=name_1,@status=status from insertedinsert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'Delete')End----测试----insert into tb_1(id,name_1,status)values('1','1','1')select * from tb_1select * from tb_2id name_1 status type----------- -------------------- -------------------- --------------------------------------------------1 1 1 NULL(1 row(s) affected)id name_1 status type----------- -------------------- -------------------- --------------------------------------------------1 1 1 Insert-----------测试Update---------------update tb_1 set name_1='2'where id='1'select * from tb_1select * from tb_2id name_1 status type----------- -------------------- -------------------- --------------------------------------------------1 2 1 NULL(1 row(s) affected)id name_1 status type----------- -------------------- -------------------- --------------------------------------------------1 1 1 Insert1 1 1 UpdateBefor1 2 1 UpdateEnd

达令说

用插入触发器要添加记录的设表名为thistable,要用触发器修改的另一个表为TABLE_OTHER,为叙述简便,设置其结构相同,都有三个字段,字段名为field1,field2,field3,则通过下面的触发器,可以实现一个插入命令同时给两个表添加记录。create trigger ins_triggername on thistable FOR insertasbeginDECLARE @FIELD1 VARCHAR(10),@FIELD2 INT,@FIELD3 FLOATSELECT @FIELD1=FIELD1,@FIELD2=FIELD2,@FIELD3=FIELD3 FROM INSERTEDINSERT INTO TABLE_OTHER SELECT @FIELD1,@FIELD2,@FIELD3endgo
打开App,查看更多内容
随时随地看视频慕课网APP