Solved

Import/update from one table to onother ?

Posted on 2012-03-22
5
269 Views
Last Modified: 2012-04-15
Hi!

Have used BULK INSERT, and inserted about 85000 records
into table1_tmp

But need to insert/update from table1_tmp to a existing table ->table2

if record exist in table2, it must update table2
if record not exist it must insert the new record to table2

Whaqt is the fastest way to do this, using stored procedure ?

Please give me example of this
0
Comment
Question by:team2005
  • 4
5 Comments
 
LVL 10

Expert Comment

by:Umar Topia
ID: 37751955
May be you can try using Trigger on table1_tmp, which will do the addition/modification on table2
0
 
LVL 2

Author Comment

by:team2005
ID: 37751967
Hi!

Never used trigger before.

Can you please give me example of this ?
0
 
LVL 2

Author Comment

by:team2005
ID: 37751988
Hi!

Found this code:

--Update Existing
UPDATE dbo.KJE_KJEDEREG
SET Lopenummer = dbo.KJE_KJEDEREG_TMP.Lopenummer,
   Column = dbo.KJE_KJEDEREG_TMP.Column
FROM dbo.KJE_KJEDEREG_TMP
INNER JOIN dbo.KJE_KJEDEREG
   ON dbo.KJE_KJEDEREG_TMP.Lopenummer = dbo.KJE_KJEDEREG.Lopenummer

--Insert New
INSERT INTO dbo.KJE_KJEDEREG (Lopenummer, Column)
SELECT dbo.KJE_KJEDEREG_TMP.Lopenummer, dbo.KJE_KJEDEREG_TMP.Column
FROM dbo.KJE_KJEDEREG_TMP
LEFT OUTER JOIN dbo.KJE_KJEDEREG
   ON dbo.KJE_KJEDEREG_TMP.Lopenummer = dbo.KJE_KJEDEREG.Lopenummer
WHERE dbo.KJE_KJEDEREG.Lopenummer IS NULL

Open in new window


Tryed this code, but gives me this error message:

2:39:59  [UPDATE - 0 row(s), 0.000 secs]  1) [Error Code: 156, SQL State: S1000]  Incorrect syntax near the keyword 'Column'. 2) [Error Code: 156, SQL State: S1000]  Incorrect syntax near the keyword 'Column'. 3) [Error Code: 156, SQL State: S1000]  Incorrect syntax near the keyword 'Column'.

?????
0
 
LVL 2

Accepted Solution

by:
team2005 earned 0 total points
ID: 37831302
Hi!

Have solved this issue:

Example of working code:

ALTER PROCEDURE "dbo"."KJE_Belligenhet_importorter"
@PathFileName varchar(100),
@FileType int
AS


DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
  SET @SQL = "BULK INSERT dbo.KJE_BELLIGENHET_TMP FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = '"",""') "
 END
ELSE
 BEGIN
  SET @SQL = "BULK INSERT dbo.KJE_BELLIGENHET_TMP FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = ',') "
 END


EXEC (@SQL)


UPDATE dbo.KJE_BELLIGENHET_TMP 
SET NavnKort =SUBSTRING(NavnKort,2,LEN(NavnKort)-2),
    Navn =SUBSTRING(Navn,2,LEN(Navn)-2)

	

UPDATE dbo.KJE_BELLIGENHET
SET dbo.KJE_BELLIGENHET.Beliggenhetsnummer = dbo.KJE_BELLIGENHET_TMP.Beliggenhetsnummer,
dbo.KJE_BELLIGENHET.Navnkort = dbo.KJE_BELLIGENHET_TMP.Navnkort,
dbo.KJE_BELLIGENHET.Navn = dbo.KJE_BELLIGENHET_TMP.Navn


FROM dbo.KJE_BELLIGENHET_TMP
INNER JOIN dbo.KJE_BELLIGENHET
   ON dbo.KJE_BELLIGENHET_TMP.Beliggenhetsnummer = dbo.KJE_BELLIGENHET.Beliggenhetsnummer


INSERT INTO dbo.KJE_BELLIGENHET (Beliggenhetsnummer,Navnkort, 
Navn)

SELECT dbo.KJE_BELLIGENHET_TMP.Beliggenhetsnummer,dbo.KJE_BELLIGENHET_TMP.Navnkort, 
dbo.KJE_BELLIGENHET_TMP.Navn

FROM dbo.KJE_BELLIGENHET_TMP
LEFT OUTER JOIN dbo.KJE_BELLIGENHET
   ON dbo.KJE_BELLIGENHET_TMP.Beliggenhetsnummer = dbo.KJE_BELLIGENHET.Beliggenhetsnummer
WHERE dbo.KJE_BELLIGENHET.Beliggenhetsnummer IS NULL



TRUNCATE TABLE dbo.KJE_BELLIGENHET_TMP

Open in new window

0
 
LVL 2

Author Closing Comment

by:team2005
ID: 37848060
Have faound solution on this isseu, myself..
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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, show how to shrink a transaction log file down to a reasonable size.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now