Solved

Import/update from one table to onother ?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now