Solved

Import/update from one table to onother ?

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

690 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