Solved

TRIGGER - Insert current date on new row

Posted on 2008-06-18
4
1,011 Views
Last Modified: 2009-05-18
I would like to write a trigger to insert the current date in my ProjectRequestDate column when a new row is added. I have tried to set this as a DEFAULT when I created the table but it does not work unless the query is re-run after the db committ. I have no experience with triggers but I feel this is the sure way to go. Please could someone help me write this? I am using a Visual Basic form and a SQL Server 2005.
0
Comment
Question by:teustace
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21811146
can you please clarify the table structure, ie the primary key field?
0
 
LVL 22

Expert Comment

by:dportas
ID: 21811164
I think a trigger is overkill for this. Use a DEFAULT. Maybe you could post some example code to show how the default isn't working for you.

In a trigger you could do something like this:

UPDATE YourTbl
SET ProjectRequestDate = CURRENT_TIMESTAMP
FROM YourTbl
JOIN Inserted
ON YourTbl.KeyCol = Inserted.KeyCol;
0
 

Author Comment

by:teustace
ID: 21811447
I agree that the TRIGGER may be a bit too powerful but I don't know what else to do...

In my table I have defined  two default values on two columns. One is GetDate() the other is an integer (6). I am developing a visual basic form based on this table. When I enter a new row, be it from the form or direct into the db table, the default values will not populate the table unless I re-run the ! SQL. Do I need to create a trigger and if so what is the code? The columns are currently set to NOT NULL. When I try entering the data directly into my table (rather than from the form) it gives a red dot to tell me that "the row was successfully committed to the db however, a problem occurred when attempting to retrieve the data back". So it tells me to re-run the query again. THEN it populates with the specifued default values.
0
 

Accepted Solution

by:
teustace earned 0 total points
ID: 21812150
Hi Deportas
Just trying out your trigger. I want to make sure it only updates the Current Timestamp if it is NULL, jsut incase it over writes any stored values. Can you help me out with the code?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now