0

When i use this code it inserts row

create trigger [dbo].[InsertInvPayment] on dbo.LG_001_01_PAYTRANS
after update
as
begin
  SET NOCOUNT ON;
   declare @InvLogicalRef int;
   declare @InvNumber varchar(50);
   select @InvLogicalRef = inserted.FICHEREF from inserted
   select @InvNumber = dbo.LG_001_01_INVOICE.DOCODE from dbo.LG_001_01_INVOICE where      dbo.LG_001_01_INVOICE.LOGICALREF = @InvLogicalRef
   insert into dbo.CRMINVPAYMENT(INVNUMBER) values('Hello')
  end

if i change it like this

create trigger [dbo].[InsertInvPayment] on dbo.LG_001_01_PAYTRANS
after update
as
begin
  SET NOCOUNT ON;
   declare @InvLogicalRef int;
   declare @InvNumber varchar(50);
   select @InvLogicalRef = inserted.FICHEREF from inserted
   select @InvNumber = dbo.LG_001_01_INVOICE.DOCODE from dbo.LG_001_01_INVOICE where      dbo.LG_001_01_INVOICE.LOGICALREF = @InvLogicalRef
   insert into dbo.CRMINVPAYMENT(INVNUMBER) values(@InvNumber)
  end

it doesn't work. i couldn't find a mistake in the second

2
  • can you print values of @InvNumber? Commented Oct 2, 2013 at 9:04
  • 1
    "It doesn't work" isn't very descriptive. Error message? Unexpected results? Also, it's broken anyway. inserted may contain 0, 1 or many rows. Commented Oct 2, 2013 at 9:04

2 Answers 2

1

Seems like You are missing the source for select InvLogicalRef. It should be

select @InvLogicalRef = inserted.FICHEREF from inserted

Also with this trigger you are assuming that only one record will update at once. This will fail for bulk updates.

Sign up to request clarification or add additional context in comments.

Comments

0

The line where you assign a value to @InvLogicalRef is incomplete. As that variable doesn't contain a value, @InvNumber can't be assigned its value and you are therefore trying to insert a NULL string into CRMINVPAYMENT.

Make sure you select the value of @InvLogicalRef from the inserted table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.