?
Solved

Import/update from one table to onother ?

Posted on 2012-03-22
5
Medium Priority
?
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Suggested Courses

765 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