Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

help with nightly update query

Posted on 2013-01-29
5
Medium Priority
?
174 Views
Last Modified: 2013-02-04
Hi experts, I need some help with Nightly updates that I need to do. I need to save some data from queries to files and upload one time and later on daily I should be uploading the new data that is added. The difference of data from Day 1(results from query on Day1) and Day 2(results from query on Day 2).
Please suggest me ways to do it.
thanks
0
Comment
Question by:sqlcurious
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:deviprasadg
ID: 38833929
Possible ways :

Solution1:
- Create a table in the database with columns that are fetched by the query, also add the date of execution as a column
- Every day execute the query to populate the table created above (with the date of execution column also populated)
- Also write a query that fetches the difference of data from Day1 and Day2, using the EXCEPT operator.
Sample query:
SELECT COL1,COL2.. FROM CompareTable where ExecuteDate = cast(getdate() as date)
except
SELECT COL1,COL2.. FROM CompareTable where ExecuteDate = cast(getdate() -1 as date)

Open in new window


Also be sure to populate executedate with the date(not datetime)
use cast(getdate() as date) while inserting data into this table also.

Solution2:
- BCP Out the query data into files every day.(also you can copy paste into excel and save as CSV)
- Create a two tables in the database with columns that are fetched by the query
   one table can be used for day1 data and other for day2 data.
- BCP in the files that has to be compared into a tables created above.
- Write a query that fetches the difference of data from Day1 table and Day2 table , using the EXCEPT operator.
0
 
LVL 6

Expert Comment

by:liija
ID: 38834311
If you need some more control, easier error handling, logging etc. you might use SQL Integration Services package to do the job.
0
 

Author Comment

by:sqlcurious
ID: 38841334
For some reason the below logic is not working:
SELECT COL1,COL2.. FROM CompareTable where ExecuteDate = cast(getdate() as date)
except
SELECT COL1,COL2.. FROM CompareTable where ExecuteDate = cast(getdate() -1 as date)

I have 7000 records that are different from day1 and day2 but its giving me way way too many, please help
0
 
LVL 10

Accepted Solution

by:
deviprasadg earned 2000 total points
ID: 38842420
SELECT COL1,COL2.. FROM CompareTable where ExecuteDate = cast(getdate() as date)
except
SELECT COL1,COL2.. FROM CompareTable where ExecuteDate = cast(getdate() -1 as date)

This query returns any distinct values from the query to the left of the EXCEPT operand that are not also found on the right query.

As you said "7000 records that are different from day1 and day2", you could have come up with this number based on some specific columns that you compared among two result sets.
So use those columns only in the select lists above.

Refer: http://msdn.microsoft.com/en-us/library/ms188055(v=sql.100).aspx

Alternatively you can use Joins or Exists Clause based on the same columns.
0
 

Author Closing Comment

by:sqlcurious
ID: 38852774
thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 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