I have two data files that I need to merge into one table in SQL Server 2000. The layouts are below, * denotes key fields.
File 1 (Order):
State*
Type*
Cust #*
FileDate*
Customer Info
Account Type (B or D)*
Order (Y or N)
File 2 (Usage):
State*
Type*
Cust #*
FileDate*
Customer Info
Account Type (B or D)*
Final Table:
State*
Type*
Cust #*
FileDate*
Customer Info
TypeBOrder (Y or N)
TypeDOrder (Y or N)
TypeBUsage (Y or N)
TypeDUsage (Y or N)
The final table will exist perpetually (and hold a rolling 12 months worth of data). The 2 files will come in monthly. I plan to have seperate tables (identical layout to the files) to simply import the 2 data files each month, then run a stored proc to insert/update the main table with this new data, then delete the 2 tables.
Here's what I think I need to do:
[ul]
[li]Import Order table with Order data.[/li]
[li]Import Usage table with Usage data.[/li]
[li]Insert any key fields from the Order table that are not currently in the Main table.[/li]
[li]Update the TypeBOrder field on the main table where there is a record in the Order table with a matching key, Account Type = B and Order = Y[/li]
[li]Update the TypeDOrder field on the main table where there is a record in the Order table with a matching key, Account Type = D and Order = Y[/li]
[li]Update the TypeBUsage field on the main table where there is a record in the Usage table with a matching key and Account Type = B[/li]
[li]Update the TypeDUsage field on the main table where there is a record in the Usage table with a matching key and Account Type = D[/li]
[li]Delete Order table[/li]
[li]Delete Usage table[/li]
[li]Delete main table data > 12 months old[/li]
[/ul]
I can handle all the updating/deleting stuff easily enough. My problem comes in trying to insert all the Order items that are not currently in the table. I've tried Where Not Exists, Minus, etc. but can't seem to get it right. Am I just overlooking an easy solution to this?
I'm trying to avoid using a cursor and selecting the Order table, then checking the Main table to see if it's there...I didn't think that would be the efficient way to go about it.
Any ideas?
Thanks!!
File 1 (Order):
State*
Type*
Cust #*
FileDate*
Customer Info
Account Type (B or D)*
Order (Y or N)
File 2 (Usage):
State*
Type*
Cust #*
FileDate*
Customer Info
Account Type (B or D)*
Final Table:
State*
Type*
Cust #*
FileDate*
Customer Info
TypeBOrder (Y or N)
TypeDOrder (Y or N)
TypeBUsage (Y or N)
TypeDUsage (Y or N)
The final table will exist perpetually (and hold a rolling 12 months worth of data). The 2 files will come in monthly. I plan to have seperate tables (identical layout to the files) to simply import the 2 data files each month, then run a stored proc to insert/update the main table with this new data, then delete the 2 tables.
Here's what I think I need to do:
[ul]
[li]Import Order table with Order data.[/li]
[li]Import Usage table with Usage data.[/li]
[li]Insert any key fields from the Order table that are not currently in the Main table.[/li]
[li]Update the TypeBOrder field on the main table where there is a record in the Order table with a matching key, Account Type = B and Order = Y[/li]
[li]Update the TypeDOrder field on the main table where there is a record in the Order table with a matching key, Account Type = D and Order = Y[/li]
[li]Update the TypeBUsage field on the main table where there is a record in the Usage table with a matching key and Account Type = B[/li]
[li]Update the TypeDUsage field on the main table where there is a record in the Usage table with a matching key and Account Type = D[/li]
[li]Delete Order table[/li]
[li]Delete Usage table[/li]
[li]Delete main table data > 12 months old[/li]
[/ul]
I can handle all the updating/deleting stuff easily enough. My problem comes in trying to insert all the Order items that are not currently in the table. I've tried Where Not Exists, Minus, etc. but can't seem to get it right. Am I just overlooking an easy solution to this?
I'm trying to avoid using a cursor and selecting the Order table, then checking the Main table to see if it's there...I didn't think that would be the efficient way to go about it.
Any ideas?
Thanks!!