I am trying to import data from a CSV file to a SQL server. The data imports fine, except when I run the import again, the data is recopied in to the table instead of the data in the table getting updated. The data in the CSV file will be updated every day and it may or may not have new or updated data in it. I cant figure out how to make it so the data on the server is the updated and most recent version of the CSV file.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
SQL Server 2000 problem
Home » Forums » Admin IT Lounge » Application servers – Exchange, IIS, Sharepoint » SQL Server 2000 problem
- This topic has 27 replies, 4 voices, and was last updated 19 years, 9 months ago.
Viewing 1 reply threadAuthorReplies-
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 2:18 pm #917771I take it you’re using a DTS package in conjunction with a SQL Job to perform the transfer. (If you’re not, you might consider doing it this way…)
The default action for this import is to append to the destination table. You’ll need to add a task to the DTS package to delete the destination data before the import.To delete the data from the SQL table before importing the CSV file, simply add an “Execute SQL Task” task to the DTS designer. Enter the desired SQL statement (i.e. “DELETE FROM “). Make sure you have the appropriate SQL Connection selected, then click OK.
To make sure this statement completes successfully BEFORE the import begins, select the newly created SQL task icon, then CTRL+Select the first step of the CSV import process (most likely a Text File Source icon). Right-click the CSV icon and select Workflow | On Success.
For more information see the following SQL-Books-Online topics:
-Execute SQL Task
-Creating DTS Package Workflow and TasksHope this helps
-
WSBigZ
AskWoody LoungerJanuary 3, 2005 at 2:23 pm #917775Mark,
I am trying to do it with the DTS. The data in the CSV file with have the Last Name, First Name, Title, Phone Number, Extension, and Address. Each have their own column. The last name and first name need to be used as the primary key. If the name changes, then it will be a new row. However if the title, phone number, extension, or address change, I need to replace the old data with the updated data. I have been trying to do it by inserting the CSV file into a temp table and the searching the data in the temp table to find the new data, but I am not having any luck. Any help is appreciated.
Thanks,
Nick -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 2:33 pm #917781So it sounds like your CSV file does not contain the complete table each time – just changes and additions, right?
Your approach sounds fine – import the CSV data to a temp table then process. You’ll probably want to enlist the help of a Stored Procedure for the processing. I would normally avoid using a Cursor in T-SQL when possible, but it seems that this task would be appropriate for a cursor. Here’s the pseudo-code:
Declare a cursor with all fields
Open the cursor
Move to the first record
While there are still records in the cursor
Search for an existing record with the first/last names
If it exists, update it with the new data
Otherwise insert a new record
Move to the next record
End While
Deallocate the cursorYou’ll want to double-check the exact process in BooksOnline (see “DECLARE CURSOR”), but that should get the job done for you.
Post back if you need any further assistance.
-
WSBigZ
AskWoody Lounger -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 2:56 pm #917788I wouldn’t say that cursors aren’t the “easiest” but they’re certainly not the best in terms of performance. Running a cursor through a large table (100,000+ records) in a web application would cause some less-than-desired performance. However, using cursors for an import process (where extra seconds or even minutes won’t make a difference) is perfectly appropriate.
Now that you mention it, you could also use some non-cursor code for inserting like this:
INSERT
SELECT * FROM
RIGHT JOIN ON .FirstName = .FirstName AND .LastName = .LastName
WHERE .FirstName IS NULL AND .LastName IS NULLHowever, you still need a way to iterate all (existing) records to perform an update. At the moment, a cursor is the best method that comes to mind.
As an after-thought, you could also use a Trigger on the Temp table. Although, I try to avoid triggers since the logic is more difficult to track and maintain than stored procedures. (I’m a developer – not a DBA)
-
WSBigZ
AskWoody LoungerJanuary 3, 2005 at 3:07 pm #917792If I would use a cursor, can you give me an example of the code used to declare the cursor? I see the pseudo-code listed above, but I am not certain how to declare the cursor. Would it be something like this:
DECLARE temp_cursor CURSOR
FOR SELECT * FROM temp
OPEN temp_cursor
FETCH NEXT FROM temp_cursorIf this is correct, I am not sure where to go from here. Thanks again
-
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 3:26 pm #917807I use cursors so infrequently that I have to look at SQL BooksOnline for help each time! From the syntax you included, I can see that you found either the BOL help topic or some other resource that discusses this process. The only change you’ll need to make is adding a variable for each field – like this:
DECLARE @FirstName varchar(50)
DECLARE @LastName varchar(50)
DECLARE @Title varchar(5)
–etc…DECLARE temp_cursor CURSOR
FOR SELECT * FROM tempOPEN temp_cursor
FETCH NEXT FROM temp_cursor
INTO @FirstName, @LastName, @Title –, etc…WHILE @@FETCH_STATUS = 0
BEGIN
–Declare variable for record count
DECLARE @Count int
–Get count of whether record exists in permanent table or not
SELECT @Count = COUNT(*) FROM WHERE FirstName = @FirstName AND LastName = @LastName
–If count > 0, record already exists
IF @Count > 0 –Record already exists, perform update
UPDATE
SET—…..
WHERE LastName = @LastName AND FirstName = @FirstName
ELSE –Record does not exist, perform insert
INSERT INTO
(FirstName, LastName, Title, etc)
VALUES (@FirstName, @LastName, @Title, etc)–Get next record
FETCH NEXT FROM temp_cursor
INTO @FirstName, @LastName, @Title –, etc…END
CLOSE temp_cursor
DEALLOCATE temp_cursorThat’s basically it… You’ll need to complete the various statments and add the appropriate variables for the field names.
Post back if that doesn’t do the trick.
-
WSBigZ
AskWoody Lounger -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 3:47 pm #917824I would create a stored procedure, then execute the stored procedure from the DTS package – but that’s simply a suggestion. I find it easier to maintain the logic if it’s easy to find and reuse. If the code is entered directly into a SQL Task in the DTS package, you can’t reuse it for anything. But if it’s in a Stored Procedure it can be used by multiple DTS packages, etc.
-
WSBigZ
AskWoody Lounger -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 4:20 pm #917836That sounds like it will work. The only thing I would do differently is purge your temp table at the beginning rather than the end of the process. This will allow you to keep the temp records after the import in case you need to reference them for any reason.
Also, you may consider adding a date/time stamp field to the import table, if one is not already there. This will allow you to determine when the record was updated.
It looks like you’re on the right track!
-
WScgehlhausen
AskWoody LoungerJanuary 24, 2005 at 2:24 pm #924336My name’s Chris, I am the engineer that is actually installing this SQL program – dubbs11 was doing the preliminary groundwork since I’m short on time.
Everything seems to be working except for the most simple (I think) of issues, but I can’t find an answer. HOW DO I REFERENCE ONE DATABASE FROM THE OTHER?
We have the DTS working properly – dropping and re-importing the CSV file each time. It’s coming into database TEMP, table cardemp. The database that it’s going into is WINPAK2, table cardholder. Here’s the code for cursoring and sorting:
DECLARE @FirstName varchar(30)
DECLARE @LastName varchar(30)
DECLARE @Note1 varchar(64) –Title
DECLARE @Note2 varchar(64) –JobCode
DECLARE @Note3 varchar(64) –WorkArea
DECLARE @Note4 varchar(64) –Department
DECLARE @Note5 varchar(64) –HireDate
DECLARE @Note6 varchar(64) –EmployeeIDDECLARE Temp_Cursor cursor
FOR SELECT * FROM [temp].[cardemp]OPEN Temp_Cursor
FETCH NEXT FROM Temp_Cursor
INTO @FirstName, @LastName, @Note1, @Note2, @Note3, @Note4, @Note5, @Note6WHILE @@FETCH_STATUS = 0
BEGINDECLARE @Count int
SELECT @Count = Count(*) FROM [WINPAK2].[CardHolder] –FIRST ERROR HAPPENS HERE
WHERE FirstName = @FirstName AND Note5 = @Note5 AND Note6 = @Note6IF @Count > 0
UPDATE [WINPAK2].[CardHolder]
SET LastName = @LastName, Note1 = @Note1, Note2 = @Note2, Note3 = @Note3, Note4 = @Note4
WHERE FirstName = @FirstName AND Note5 = @Note5 AND Note6 = @Note6
ELSE
INSERT INTO [WINPAK2].[CardHolder] (FirstName, LastName, Note1, Note2, Note3, Note4, Note5, Note6)
Values (@FirstName, @LastName, @Note1, @Note2, @Note3, @Note4, @Note5, @Note6)FETCH NEXT FROM Temp_Cursor
INTO @FirstName, @LastName, @Note1, @Note2, @Note3, @Note4, @Note5, @Note6
ENDCLOSE Temp_Cursor
DEALLOCATE Temp_CursorRunning this code from the DTS gives errors for each reference of [WINPAK2].[CardHolder], but running it from a Stored Procedure in WINPAK2 gives an error for referencing [temp].[cardemp]. I can’t seem to find anywhere what the correct syntax is for referencing other database tables! Please Help.
-
WSMarkJ
AskWoody LoungerJanuary 24, 2005 at 2:51 pm #924343You’re close…
To reference another database on the same sql server instance, use 3-part naming: [database].[owner].[ table ]
(i.e. [WINPAK2].[dbo].[cardtemp]) — Please ignore the spaces surrounding “table”, the Lounge has a table tag that will cause this text to not display…
To reference a database on another sql server instance, use 4-part naming: [sqlserver].[database].[owner].[ table ]
(i.e. [SQLENT1].[Pubs].[dbo].[Authors])As a shortcut you can leave out the owner and use only dots – i.e. [WINPAK2]..[cardtemp]). This will use the default owner (i think).
I’ve done a quick search in SQL Books Online and didn’t not find this in writing, but I’m sure it’s in there…
-
WScgehlhausen
AskWoody LoungerJanuary 26, 2005 at 1:21 pm #924878Mark,
That was the trick. Sorry I didn’t reply sooner, but I actually found an example that showed me the same answer, then I was offline testing and installing it. It works really sweet – one line at a time, updating or adding each record individually. They’re exporting the entire database each night across the network to a CSV file, then my DTS is running every morning at 1am to update the other database. There’s about 4,500 employees right now, so there’s a big time savings in typing!
-
WSBigZ
AskWoody LoungerAugust 29, 2005 at 3:21 pm #969449Before the import was ran one night, a relationship got changed in the compare portion of the local package. The csv file was imported as normal but the FirstName was imported as the LastName and the LastName was imported as the FirstName. I am trying to write some code to search through the database and find the records where the LastName = FirstName, First Name = LastName, Emp# = Emp#, and the date is null, and delete this record. Here is the code I have so far:
DECLARE @Emp# varchar(64)
DECLARE @Date varchar(8)
DECLARE @LastName varchar(30)
DECLARE @FirstName varchar(30)DECLARE Temp_Cursor cursor
FOR SELECT * FROM [Test].[dbo].[TestTable]OPEN Temp_Cursor
FETCH NEXT FROM Temp_Cursor
INTO @Date, @LastName, @FirstName, @Emp#WHILE @@FETCH_STATUS = 0
BEGINDECLARE @Count int
SELECT @Count = Count(*) FROM [Test].[dbo].[TestTable]
WHERE Emp# = @Emp# And FirstName = @LastName AND LastName = @FirstName And Date = nullIF @Count > 0
DELETE [Test].[dbo].[TestTable]
WHERE Emp# = @Emp# And FirstName = @LastName AND LastName = @FirstName And Date = null
ELSEFETCH NEXT FROM Temp_Cursor
INTO @Date, @LastName, @FirstName, @Emp#
ENDCLOSE Temp_Cursor
DEALLOCATE Temp_Cursor
GOThe file runs without any errors, but does not delete the duplicate entry I have in my table. Any help is appreciated.
Thanks,
Nick -
WSrory
AskWoody Lounger -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 4:20 pm #917837That sounds like it will work. The only thing I would do differently is purge your temp table at the beginning rather than the end of the process. This will allow you to keep the temp records after the import in case you need to reference them for any reason.
Also, you may consider adding a date/time stamp field to the import table, if one is not already there. This will allow you to determine when the record was updated.
It looks like you’re on the right track!
-
WSBigZ
AskWoody Lounger -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 3:47 pm #917825I would create a stored procedure, then execute the stored procedure from the DTS package – but that’s simply a suggestion. I find it easier to maintain the logic if it’s easy to find and reuse. If the code is entered directly into a SQL Task in the DTS package, you can’t reuse it for anything. But if it’s in a Stored Procedure it can be used by multiple DTS packages, etc.
-
WSBigZ
AskWoody Lounger -
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 3:26 pm #917808I use cursors so infrequently that I have to look at SQL BooksOnline for help each time! From the syntax you included, I can see that you found either the BOL help topic or some other resource that discusses this process. The only change you’ll need to make is adding a variable for each field – like this:
DECLARE @FirstName varchar(50)
DECLARE @LastName varchar(50)
DECLARE @Title varchar(5)
–etc…DECLARE temp_cursor CURSOR
FOR SELECT * FROM tempOPEN temp_cursor
FETCH NEXT FROM temp_cursor
INTO @FirstName, @LastName, @Title –, etc…WHILE @@FETCH_STATUS = 0
BEGIN
–Declare variable for record count
DECLARE @Count int
–Get count of whether record exists in permanent table or not
SELECT @Count = COUNT(*) FROM WHERE FirstName = @FirstName AND LastName = @LastName
–If count > 0, record already exists
IF @Count > 0 –Record already exists, perform update
UPDATE
SET—…..
WHERE LastName = @LastName AND FirstName = @FirstName
ELSE –Record does not exist, perform insert
INSERT INTO
(FirstName, LastName, Title, etc)
VALUES (@FirstName, @LastName, @Title, etc)–Get next record
FETCH NEXT FROM temp_cursor
INTO @FirstName, @LastName, @Title –, etc…END
CLOSE temp_cursor
DEALLOCATE temp_cursorThat’s basically it… You’ll need to complete the various statments and add the appropriate variables for the field names.
Post back if that doesn’t do the trick.
-
WSBigZ
AskWoody LoungerJanuary 3, 2005 at 3:07 pm #917793If I would use a cursor, can you give me an example of the code used to declare the cursor? I see the pseudo-code listed above, but I am not certain how to declare the cursor. Would it be something like this:
DECLARE temp_cursor CURSOR
FOR SELECT * FROM temp
OPEN temp_cursor
FETCH NEXT FROM temp_cursorIf this is correct, I am not sure where to go from here. Thanks again
-
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 2:56 pm #917789I wouldn’t say that cursors aren’t the “easiest” but they’re certainly not the best in terms of performance. Running a cursor through a large table (100,000+ records) in a web application would cause some less-than-desired performance. However, using cursors for an import process (where extra seconds or even minutes won’t make a difference) is perfectly appropriate.
Now that you mention it, you could also use some non-cursor code for inserting like this:
INSERT
SELECT * FROM
RIGHT JOIN ON .FirstName = .FirstName AND .LastName = .LastName
WHERE .FirstName IS NULL AND .LastName IS NULLHowever, you still need a way to iterate all (existing) records to perform an update. At the moment, a cursor is the best method that comes to mind.
As an after-thought, you could also use a Trigger on the Temp table. Although, I try to avoid triggers since the logic is more difficult to track and maintain than stored procedures. (I’m a developer – not a DBA)
-
-
WSBigZ
AskWoody Lounger
-
-
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 2:33 pm #917782So it sounds like your CSV file does not contain the complete table each time – just changes and additions, right?
Your approach sounds fine – import the CSV data to a temp table then process. You’ll probably want to enlist the help of a Stored Procedure for the processing. I would normally avoid using a Cursor in T-SQL when possible, but it seems that this task would be appropriate for a cursor. Here’s the pseudo-code:
Declare a cursor with all fields
Open the cursor
Move to the first record
While there are still records in the cursor
Search for an existing record with the first/last names
If it exists, update it with the new data
Otherwise insert a new record
Move to the next record
End While
Deallocate the cursorYou’ll want to double-check the exact process in BooksOnline (see “DECLARE CURSOR”), but that should get the job done for you.
Post back if you need any further assistance.
-
-
WSBigZ
AskWoody LoungerJanuary 3, 2005 at 2:23 pm #917776Mark,
I am trying to do it with the DTS. The data in the CSV file with have the Last Name, First Name, Title, Phone Number, Extension, and Address. Each have their own column. The last name and first name need to be used as the primary key. If the name changes, then it will be a new row. However if the title, phone number, extension, or address change, I need to replace the old data with the updated data. I have been trying to do it by inserting the CSV file into a temp table and the searching the data in the temp table to find the new data, but I am not having any luck. Any help is appreciated.
Thanks,
Nick
-
-
WSMarkJ
AskWoody LoungerJanuary 3, 2005 at 2:18 pm #917772I take it you’re using a DTS package in conjunction with a SQL Job to perform the transfer. (If you’re not, you might consider doing it this way…)
The default action for this import is to append to the destination table. You’ll need to add a task to the DTS package to delete the destination data before the import.To delete the data from the SQL table before importing the CSV file, simply add an “Execute SQL Task” task to the DTS designer. Enter the desired SQL statement (i.e. “DELETE FROM “). Make sure you have the appropriate SQL Connection selected, then click OK.
To make sure this statement completes successfully BEFORE the import begins, select the newly created SQL task icon, then CTRL+Select the first step of the CSV import process (most likely a Text File Source icon). Right-click the CSV icon and select Workflow | On Success.
For more information see the following SQL-Books-Online topics:
-Execute SQL Task
-Creating DTS Package Workflow and TasksHope this helps
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
June 2025 Office non-Security Updates
by
PKCano
19 minutes ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
19 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
15 minutes ago -
How start headers and page numbers on page 3?
by
Davidhs
10 hours, 39 minutes ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
5 hours, 14 minutes ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
19 hours, 20 minutes ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
19 hours, 22 minutes ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
9 hours, 3 minutes ago -
Firefox 139
by
Charlie
1 hour, 38 minutes ago -
Who knows what?
by
Will Fastie
14 hours, 27 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
20 hours, 5 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
14 hours, 29 minutes ago -
Misbehaving devices
by
Susan Bradley
22 hours, 13 minutes ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
2 days, 2 hours ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 hour, 23 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
2 days ago -
Discover the Best AI Tools for Everything
by
Alex5723
23 hours, 48 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
1 day, 14 hours ago -
Rufus is available from the MSFT Store
by
PL1
1 day, 22 hours ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
3 days, 1 hour ago -
KB5061768 update for Intel vPro processor
by
drmark
1 day, 1 hour ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
1 day ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
2 days, 21 hours ago -
Office gets current release
by
Susan Bradley
3 days ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
4 days, 14 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
3 days, 22 hours ago -
Stop the OneDrive defaults
by
CWBillow
4 days, 15 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
5 days ago -
X Suspends Encrypted DMs
by
Alex5723
5 days, 3 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
5 days, 3 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.