We help IT Professionals succeed at work.

T-SQL: How to extract records into a new table

201 Views
Last Modified: 2018-03-21
I need query an exceptions table with 14 million records and extract only records from the previous 1 year. There is a datetime field which contains the creation date for that row.

How do I write the query that inserts only records that are newer than 1 years old?

Thanks
Comment
Watch Question

Vitor MontalvãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
"I need query an exceptions table with 14 million records and extract only records from the previous 1 year"
(...)
"How do I write the query that inserts only records that are newer than 1 years old?"

After all what do you need? To retrieve or to insert records?
SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
curiouswebsterSoftware Engineer

Author

Commented:
thanks
Vitor MontalvãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
newbieweb, you shouldn't accept my comment as solution. I just asked and didn't provide any solution.
curiouswebsterSoftware Engineer

Author

Commented:
I asked it be re-opened. Also, I do have a conflict

I created the exact clone of the Log table but used a unique names for the table and constrain.


INSERT INTO Logging.dbo.LogRecentRecords
SELECT * FROM Logging.dbo.Log L
WHERE L.Timestamp > DATEDIFF(year, -1, GETDATE())


Msg 8101, Level 16, State 1, Line 2
An explicit value for the identity column in table 'Logging.dbo.LogRecentRecords' can only be specified when a column list is used and IDENTITY_INSERT is ON.

So, it's having trouble inserting the primary key. It would be better to preserve the exact LogID in the event I ever need to correlate those records.

The LogRecentRecords clones table will never need to be used for any purpose other than me executing queries against the data, so if it has IDENTITY_INSERT set to OFF, that's fine.
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
curiouswebsterSoftware Engineer

Author

Commented:
thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
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
Empower Your Career
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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.