Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQl: Update Table from a text File

Here's what I have to do :

I have a text file which has 3 columns: PID, X, Y.

Now I have two tables in my database:

  • Table 1 contains 4 columns: UID, PID, X, Y
  • Table 2 contains multiple columns, required ones being UID, X, Y

I need to update Table 2 with corresponding X and Y values.

I think we can use BULK INSERT for updating table 1, then some WHILE loop or something.

But I can't figure out exact thing.

like image 632
Spandan Avatar asked Jun 07 '13 08:06

Spandan


People also ask

How do I UPDATE a text field in SQL?

Use UPDATETEXT to change only a part of a text, ntext, or image column in place. Use WRITETEXT to update and replace a whole text, ntext, or image field. This feature will be removed in a future version of Microsoft SQL Server.

How do you UPDATE existing data in SQL?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement. UPDATE table_name SET column1 = value1, column2 = value2,...

How do you UPDATE a table with a query?

Open the database that contains the records you want to update. On the Create tab, in the Queries group, click Query Design. Click the Tables tab. Select the table or tables that contain the records that you want to update, click Add, and then click Close.


2 Answers

CREATE PROCEDURE [dbo].[BulkInsert]            
(            
@PID int  ,          
@x int,          
@y int,          

)            
AS            
BEGIN            
SET NOCOUNT ON;            

declare @query varchar(max)            


CREATE TABLE #TEMP            
(            
[PID] [int] NOT NULL ,          
[x] int NOT NULL,          
[y] int NOT NULL,             

)            


SET @query = 'BULK INSERT #TEMP FROM ''' + PathOfYourTextFile + ''' WITH ( FIELDTERMINATOR = '','',ROWTERMINATOR = ''\n'')'            
--print @query            
--return            
execute(@query)            


BEGIN TRAN;            

MERGE TableName AS Target            
USING (SELECT * FROM #TEMP) AS Source            
ON (Target.YourTableId = Source.YourTextFileFieldId)
-- In the above line we are checking if the particular row exists in the table(Table1)  then update the Table1 if not then insert the new row in Table-1.           

WHEN MATCHED THEN            
UPDATE SET             
Target.PID= Source.PID, Target.x= Source.x, Target.y= Source.y           
WHEN NOT MATCHED BY TARGET THEN            

-- Insert statement  

You can use this above approach to solve your problem. Hope this helps. :)

like image 132
garvit gupta Avatar answered Sep 21 '22 19:09

garvit gupta


How are you going to run it ? From a stored procedure ?

To save some performance, I would have done BULK INSERT to temp table, then insert from temp table to Table 1 & 2.

It should look like this

INSERT INTO Table1 ( PID, X, Y)
SELECT  PID, X, Y
FROM    #tempTable

Some will tell that temp table are not good, but it really depend - if you file is big, reading it from disk will take time and you don't want to do it twice.

like image 35
Mzf Avatar answered Sep 21 '22 19:09

Mzf