Link to home
Start Free TrialLog in
Avatar of slattdog
slattdogFlag for United States of America

asked on

Compare TXT File via SQL Script

I have a .csv file containing a list of IDs.  The file gets updated daily.  I need a SQL script that will look for matching IDs within the database table named CONTAINER and then edit the LOCATION field of the matching record.
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slattdog

ASKER

I have only a very basic understanding of SQL.  I was really looking for help with the mechanics of it, not just a conceptual understanding.  :-)
I'd prefer to keep it in a script if possible.  Looking at the BULKINSERT statement it looks like that would work to get the data into the SQL DB.  What would the Query look like to compare that against a field in a table and then update a field in any records that match?
Okay.  I have the BULK INSERT working.  So, now I have a table with the IDs.  (This table consists of a single field, with each record being an ID.)  Now I need the SQL code to match these IDs against the IDs in another table (same database) and update a field in each record where there is a match.
Something like this perhaps
UPDATE t
SET YourField = ????
FROM YourTable t
           INNER JOIN BulkInsertTable b on t.ID = b.ID
Does this look right?

UPDATE test.dbo.Container
   SET Customer=""
   WHERE ID IN (SELECT ID FROM test.dbo.Upload);

Upload = table that I used for the Bulk Insert that contains all the IDs from the text file.
I want the Customer field to be blank if the ID field matches any ID from the Upload table.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to be clear... any of these methods this will cycle through each entry in test.dbo.Uploads for a matching entry in test.dbo.Container correct?  Non-matches would be ignored and duplicates would just get processed again (no harm done).
Why don't you see for yourself:

Method 1:
SELECT  *
FROM  test.dbo.Container
WHERE ID IN (SELECT ID FROM test.dbo.Upload);

Open in new window

Method 2:
SELECT c.*
FROM	test.dbo.Container c
		INNER JOIN test.dbo.Upload u ON c.ID = u.ID

Open in new window

Method 3:
SELECT c.*
FROM    test.dbo.Container c
WHERE   EXISTS ( SELECT 1
                 FROM   test.dbo.Upload u
                 WHERE  c.ID = u.ID );

Open in new window