DTS Package Using MAX Function on Existing Table

Posted on 2006-04-27
Medium Priority
Last Modified: 2012-05-05
I'm trying to update an existing table from our National Data Center to a local machine.  I only want to pull in the records that have a greater transaction number(Primary Key) than the records I've already pulled in.  I don't seem to be able to use the MAX function to get the largest transaction number our of my local table while I'm building the DTS Package.  Does anyone no the SQL needed to do this or is there another solution.  In other words I only want to download the new transactions because there are several thousand records in the source that I'm pulling from.

Question by:jchance
LVL 30

Accepted Solution

nmcdermaid earned 2000 total points
ID: 16558029
Assuming that the transaction number is always increasing, the trick is to find the biggest tran number in the destination database, then use a sql query to only extract records with tran numbers after that.

this is how you do it:

1. Create a global variable (right click/package properties then go to the global variables tab)
2. Create an execute SQL task which has this sql:

SELECT MAX(TransactionNumber) As LastNumber FROM <YourTable>

It needs to run that against your destination database.

3. On that same task press the parameters button. On the output parameters tab, select output parameter type = row value, and assign your global variable that you just created in the parameter mappings

So now test just that part - run the sql task by right click/execute step. Then check your global variable value in the right click/package properties/global variables tab. It should have picked up the last transaction number in your destination database.

Now we need to extract data that has a transaction number after that.

Simlpy create a data pump between your source and destination and for the source sql type:

SELECT <field list> FROM <yourtable> WHERE transactionnumber > ?

hit the parameters button and assign your global variable.

Now test the whole thing. That should give you what you want.

Expert Comment

ID: 16558059
Hi jchance,

You don't need to use the MAX function in order to do that. You just need to pass a value of last (maximal) transaction number you stored to local machine to your DTS package and then make one select like this:

select * from Transaction
where TransactionID > LastMaxTransactionID

where TransactionID is the name of your primary key column and LastMaxTransactionID is the name of parameter passed to DTS.
Here is described how you can pass the parameter to DTS: http://support.microsoft.com/default.aspx?scid=kb;en-us;242391&sd=tech

I hope this is what you need.


Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

839 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