I have a tab delimited text file that I'll be using as a basis for periodic data import into my SQL 2005 database. One of the fields in the text file is ClientID.
What I need to do is loop through each row of the text file and determine whether the ClientID exists in our Clients table.
If it does exist, it should go ahead and run a couple of different Insert queries in order to add specified fields from the text file row as a new record in certain tables.
If it does NOT exist, I need it to write out the entire row from the text file to an ImportError table.
I have all the code written for opening the text file and querying from it using some nice OPENROWSET code and I also have the Update queries written for both conditions (when ClientID is valid and when it is not valid).
What I Need is:
Guidance on how to best handle the initial step - check to see if the ClientID is valid in the Clients table and then direct the stored proc to carry out the appropriate update queries
I was thinking of the following plan:
Setting up a temporary table with 2 columns (ClientID as INT, Status as BIT)
Looping through the text file to find all distinct ClientIDs (can be more than 1 row with same ClientID)
Write out the ClientID and if valid, set status to 1, if not valid, set status to 0
Then, once the temp table is populated, use that as a To Do list of sorts and loop through each record using some sort of For Each or cursor functionality in order to read the status bit value of each row and either do the appropriate inserts (if 1) or write to error table (if 0).
I look forward to your creative ideas and appreciate your assistance!