?
Solved

Create Insert Trigger

Posted on 2011-09-30
5
Medium Priority
?
229 Views
Last Modified: 2012-05-12
I have the following table but need to create a timestamp trigger on the table.

CREATE TABLE Completion
(
      Recid int NOT NULL IDENTITY (1, 1) PRIMARY KEY,
      Application                  Varchar(60) NULL,
      Environment                  Varchar(4) NULL,
      Comment                        Varchar(max) NULL,
      CreationDate                    datetime NOT NULL
)


Can somebody help me with this?
0
Comment
Question by:Favorable
5 Comments
 
LVL 5

Assisted Solution

by:MrNetic
MrNetic earned 400 total points
ID: 36892648
0
 
LVL 21

Assisted Solution

by:JestersGrind
JestersGrind earned 400 total points
ID: 36892699
A timestamp is a special SQL Server data type that adds a sequential binary number to the column every time the records changes.  It's unique across the database.  For that you simply add a column of that data type.  No trigger required.  

I'm going to assume that you are referring to adding a date time trigger to your table, maybe populating the CreationDate column?  For that a trigger isn't necessary.  Just add a default of GetDate() to the CreationDate column.

If I'm completely off, please let me know and I'll suggest something else.

Greg

0
 
LVL 3

Accepted Solution

by:
pg_vinod earned 800 total points
ID: 36892759
Are you looking for timestamp to update "CreationDate" column?
If it on insert then you can create default Constraint as well on column level of the table.
CREATE TABLE Completion
(
      Recid int NOT NULL IDENTITY (1, 1) PRIMARY KEY,
      Application                  Varchar(60) NULL,
      Environment                  Varchar(4) NULL,
      Comment                        Varchar(max) NULL,
      CreationDate                    datetime default current_timestamp NOT NULL
)


0
 
LVL 8

Assisted Solution

by:Leo Torres
Leo Torres earned 400 total points
ID: 36893123
CREATE TABLE Completion
(
      Recid int NOT NULL IDENTITY (1, 1) PRIMARY KEY,
      Application                  Varchar(60) NULL,
      Environment                  Varchar(4) NULL,
      Comment                        Varchar(max) NULL,
      CreationDate                    datetime default current_timestamp NOT NULL
)

or
in SSMS right click on table--> click design--> click on column--_ at the bottom pannel find defualt and enter getdate() there
this will work for an existing table
0
 

Author Closing Comment

by:Favorable
ID: 36913797
My very thank you goes to everyone.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question