2

I am trying to do an insert but it is giving an error stating that credits cannot be null but I am not providing null. Attached Screenshot

Code: Insert into [dbo].[Course] values (12,'Java',1,1,1);

Error: Msg 515, Level 16, State 2, Procedure trgAfterInsert, Line 31 Cannot insert the value NULL into column 'Credits', table 'DemoCollege.dbo.Course'; column does not allow nulls. INSERT fails. The statement has been terminated.

Trigger:

Create trigger trgAfterInsert2 ON [dbo].[Course]
After Insert
AS 
Declare @cid int;
Declare @cname nvarchar(50);



Select @cid = i.CourseID from inserted i;
Select @cname = i.Title from inserted i;


Insert into dbo.CourseTemp values (@cid, @cname);

Print 'After insert trigger fired';
GO
4
  • You have a failing trigger on the table Commented Mar 4, 2016 at 17:20
  • Always provide column list in insert statement. Commented Mar 4, 2016 at 17:57
  • And please note that your trigger does not support multiple row inserts. Commented Mar 4, 2016 at 18:12
  • There's no way that trigger generates the error you're getting. Please post the actual one. Commented Mar 4, 2016 at 19:38

2 Answers 2

2

There's an insert trigger on the table & that's where the error is being generated from. There must be some logic in that is converting the provided credit value to a null value somehow.

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

Comments

1

Just provide the columns where you want to insert the values.

 Insert into [dbo].[Course](ColName1, ColName2, ColName3, ColName4, etc..) values (12,'Java',1,1,1);

Most probably you're missing a col in the middle

1 Comment

Good advice to provide column list, but it's not the problem here.

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.