We help IT Professionals succeed at work.
Get Started

how to insert new record in my table if not exists sql server 2008

569 Views
Last Modified: 2012-05-12
Hi,
I want to insert a new record into my table if does not exist based on the TimeStamp field.  I have 2 tables, one is called InternalTable which has the following field:
TimeStamp                                   Status      TicketID
2011-07-27 13:44:09.000              Closed        124
2011-07-27 14:22:37.000              Closed        254
2012-01-01 15:11:57.000                Open        257


My 2nd table is called ExternalTable which has all the data that I need.  
TimeStamp                                   Status      TicketID
2011-07-27 13:44:09.000              Closed        124
2011-07-27 14:22:37.000              Closed        254
2012-01-01 15:11:57.000                Open        257
2012-01-10 15:11:57.000                Open        221
2012-01-11 15:11:57.000                Open        557

So I want only the new data to be inserted into the InternalTable from the ExternalTable.  If there is no new data is there (ExternalTable) then no record should be inserted. I think the key field here is the TimeStamp field because I get a new data every hour and I want to update the with the new data only based on the timestamp field.

Here is my current query (which does not do what I need)
Insert into dbo.InternalTable(TimeStamp, Status, TicketID)
Select * from dbo.ExternalTable
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE