Solved

Compare TXT File via SQL Script

Posted on 2014-01-21
9
312 Views
Last Modified: 2016-02-10
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.
0
Comment
Question by:slattdog
  • 5
  • 3
9 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 39797215
<wild guess>

1.  Import the .csv into a table, using either an SSIS package or a BULKINSERT statement.
2.  Write a MERGE statement merging the above 'source' with the CONTAINER 'target', spelling out which columns equate to a match (i.e. do nothing), which columns equate to a changed row (i.e. update), and which are new rows (i.e. insert)

Pragmatic Works' Task Factory suite of custom SSIS components has an Upsert task that allows you to perform 2. in SSIS.
0
 

Author Comment

by:slattdog
ID: 39797268
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.  :-)
0
 

Author Comment

by:slattdog
ID: 39797291
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?
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:slattdog
ID: 39798028
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39798087
Something like this perhaps
UPDATE t
SET YourField = ????
FROM YourTable t
           INNER JOIN BulkInsertTable b on t.ID = b.ID
0
 

Author Comment

by:slattdog
ID: 39798107
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.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 39798733
My preference is to use a JOIN, as in
UPDATE  c
SET     Customer = ''
FROM	test.dbo.Container c
		INNER JOIN test.dbo.Upload u ON c.ID = u.ID

Open in new window

But you can use a subquery if it is clearer to you.  But in any case it is single and not double quotes.

Another option is to use EXISTS as in:
UPDATE  c
SET     Customer = ''
FROM    test.dbo.Container c
WHERE   EXISTS ( SELECT 1
                 FROM   test.dbo.Upload u
                 WHERE  c.ID = u.ID );

Open in new window

0
 

Author Comment

by:slattdog
ID: 39800134
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).
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39801661
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

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question