Solved

TRIGGER - Insert current date on new row

Posted on 2008-06-18
4
1,033 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

707 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