我有两个相同的表,除了一个有一个标识列而另一个没有。而是第二个表使用第一个表中的标识列的值。当一条记录插入第一个表时,我以为我会插入第二个表作为触发器。我似乎无法正确使用语法。
从标识列@EDVisitId返回Null。
ALTER TRIGGER [dbo].[trgInserterrEDVisitOriginal] ON [dbo].[errEDVisit]
AFTER INSERT
AS
--Name: Bob Avallone
--Date: 6-15-2017
--
-- The purpose of this trigger is to insert a record into errEDVisitOriginal
-- whenever a new errEDVisit is inserted.
--XXXXXXXXXX
declare @EDVisitId int
declare @SubmissionControlID INT
Select @EDVisitId = EDVisitID from inserted
SELECT @SubmissionControlID = SubmissionControlID from Inserted
Begin
Insert Into errEDVisitOriginal (EDVisitId, SubmissionControlID)
VALUES (@EDVisitId, @SubmissionControlID )
End
答案 0 :(得分:1)
感谢所有建议。我放弃了触发器的想法。相反,我只是将第一个表中的新记录插入第二个表中。见下文。
Insert errEDVisitOriginal(EdVisitId, SubmissionControlID)
Select EdVisitId, SubmissionControlID
from errEDVisit where SubmissionControlID = @SubmissionControlID
答案 1 :(得分:0)
你只需要scope_identity()
选择@EDVisitId = scope_identity()
答案 2 :(得分:0)
您可以使用SCOPE_IDENTITY从第一个表中获取最后插入的id,将其放入变量,然后插入第二个表。
https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql
答案 3 :(得分:0)
Though the decision to duplicate information intentionally is dubious, you are vastly over-thinking the code. Just:
loop()
{
if (i2cmessage == 1) // act on the semaphore
{
cli(); // optional but maybe smart to turn off interrupts while big message traffic going through...
i2cmessage = 0; // reset until next interrupt
while (Wire.available())
{
messageFromBigArduino = Wire.read();
// do something with bytes read
}
Serial.println(messageFromBigArduino);
sei(); // restore interrupts if turned off earlier
}
}