我是一个语言思想不是很好的人,这里有个问题想问各位大侠,
我在一个界面 要同时插入两张表,其中第二张表tinpatient表中有一个字段,是第一张表tpatietn的主键
.我现在就是在代码上面插入两张表,写了个触发器,想要在tinpatient表插入tpatient表自增长的主键patiantid可是没有触发成功,没有将tpatient中的patientid插进tinpatientb表中
CREATE TRIGGER trg_Update_Inpatinet_patientId
on DB_IP2012.dbo.tinpatient
AFTER insert
AS
BEGIN
declare @IPSeqNoText char(2)
update DB_IP2012..tInpatient set PatientID=(select PatientID from tPatient
where @IPSeqNoText= (select IPSeqNoText from inserted))END
GO
我在一个界面 要同时插入两张表,其中第二张表tinpatient表中有一个字段,是第一张表tpatietn的主键
.我现在就是在代码上面插入两张表,写了个触发器,想要在tinpatient表插入tpatient表自增长的主键patiantid可是没有触发成功,没有将tpatient中的patientid插进tinpatientb表中
CREATE TRIGGER trg_Update_Inpatinet_patientId
on DB_IP2012.dbo.tinpatient
AFTER insert
AS
BEGIN
declare @IPSeqNoText char(2)
update DB_IP2012..tInpatient set PatientID=(select PatientID from tPatient
where @IPSeqNoText= (select IPSeqNoText from inserted))END
GO
go
if OBJECT_ID('test') is not null
drop table test
go
create table test(
id int identity(1,1) primary key,
value int
)
go
go
if OBJECT_ID('tbl') is not null
drop table tbl
go
create table tbl(
id int foreign key references test(id),
value int
)
go
--创建触发器实现上述功能:
if OBJECT_ID('tri_test') is not null
drop trigger tri_test
go
create trigger tri_test on test
for insert
as
insert tbl(id)
select id from inserted order by id asc
--如果需要插入不存在的就加上下面的条件:
--where not exists(select 1 from tbl where tbl.id=inserted.id)
--验证:
insert test(value)
select 3 union all
select 5
select * from tbl
/*
id value
---------------------
1 NULL
2 NULL
*/