473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Create trigger to know who made changes with date SQL Server Table

Hello guys,

I have one table Products and a second Products_audit with two columns User and Date. I need to have trigger which performs following actions:
When anybody will modify data in Products, I need trigger to insert the User's name and Date of inserting into the Products_audit in appropriate columns. I wrote following:

Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER Super1 ON  Products
  2.    AFTER UPDATE, INSERT
  3. AS 
  4. BEGIN
  5.     SET NOCOUNT ON;
  6.     INSERT INTO Products_audit (Date, [User])
  7.     SELECT suser_sname(),getdate() FROM Products
  8.  
  9. END 
When I try to insert data into Products I get error:

Server: Msg 515, Level 16, State 2, Procedure Super1, Line 6
Cannot insert the value NULL into column 'Discontinued', table 'Teachdb.dbo.Products_audit'; column does not allow nulls. INSERT fails.
The statement has been terminated.

I don't understand why it points out on 'Discontinued' column ? Note: before trigger was created I didn't face with that issue when I inserted data.
Mar 27 '13 #1
1 1376
Rabbit
12,516 Expert Mod 8TB
It's because if you don't define a value to insert into the field, by default it uses NULL. And your table definition doesn't allow NULLs. If you never got that message when you inserted stuff before, that means you defined a value to insert.
Mar 27 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kenneth Osenbroch | last post by:
Hi. I want to create a trigger that only allows delete from table A if corresponding record in table B does not exist. Any idea on how this can be done? Thanks, Kenneth.
1
by: Lisa Tang | last post by:
Hi, I have a table t1 with a long raw column, and I need to create a view v1 with a long raw column being a function ff1 of the long raw column in t1. And I need to update t1 with reverse...
1
by: Jack | last post by:
I created a trigger in the "source table" that will "feed" and second table. The trigger is as follows: CREATE TRIGGER ON dbo.FromUPS FOR INSERT AS Declare @Count int Select @Count =...
4
by: SAEED BASUDAN | last post by:
Hi i have 2 Table first one : Customer with 4 Fields : cst_no,cst_name,total_Debit,tot_credit second one : Transaction with 5 Fields : Trns_no,Trns_Date,cst_no,debit,credit MY QUESTION:...
2
by: rdraider | last post by:
I created and successfully tested a trigger on a test database. Now that I want to put this on a production system, the create trigger statement takes way too long to complete. I cancelled after...
1
by: vios | last post by:
Hi, I have created a trigger as below to update a table called vnd_trail to show what sort of operation(update/delete) happened to the name column. My problem is, I want to have another column to...
1
by: Reshmi Jacob | last post by:
Hello, Can any one help me in creating a trigger to update system date into a table while inserting a record into that table. I tried it like this, it is showing error !!! The following...
5
by: mse07 | last post by:
hi i need to create trigger when insert records in table for example when insert records in table then run exe program. or if any records insert in table i need alert that is record...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.