Solved

TRIGGER - Insert current date on new row

Posted on 2008-06-18
4
1,032 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…
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…

737 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