Solved

help with nightly update query

Posted on 2013-01-29
5
165 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 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query to return total 6 23
SSRS 2013 - Overlapping reports 2 32
MS SQL Update query with connected table data 3 41
(sql serv16)ssis 2016 question/check 1 74
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 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