Solved

TRIGGER - Insert current date on new row

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Latency in .net app using DB in .net 21 63
SQL Help - SELECT Statement 6 52
constraint check 2 40
Connection to multiple databases 13 18
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

776 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