Solved

Import/update from one table to onother ?

Posted on 2012-03-22
5
273 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

840 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