Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Jul 27, 1999

Comparing Text File with Existing Table

Question:
How can I go about reading in a (comma-delimited) text file and comparing it to an existing table? If there is a match on the key field, the row should be updated. If there is no match on the key field, then the row should be added.

I am using SQL 7.0. The front end is a Web browser, either Microsoft Internet Explorer or Netscape Navigator.

Answer:
You are fortunate to be undertaking this in SQL 7.0, because you get a very capable tool to manage the conversion, namely Data Transformation Services (DTS).

DTS is like BCP on steroids, not from a speed perspective, but from a usability perspective. In DTS, you define a job that consists of steps. Each step has a successor based on the success or failure of its predecessor. These jobs are saved and can be kicked off as scheduled tasks or run whenever you want.

Inside DTS, I would do the following:

  1. Create a "staging table" that would be all SQLCHARs.
  2. Name the column names as mirrors of the target table.
  3. Use DTS to import the text file into the staging table.
  4. Either convert on-the-fly or use CONVERT to massage the SQLCHAR fields so you can INSERT from the staging table into the Target table.
Either do this yourself in small steps by making these SQL tasks or take advantage of a DTS feature called Data Driven Query (DDQ).

Once you have the cleaned-up data in a proper table, use DDQ to search a lookup table and check whether the value of the column in the current row exists in the lookup table. If the row exists in the lookup table, update the row; if the current row does not exist in the lookup table, insert it. This is specified in the VBScript page of the DDQ.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date