Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Data insert

Author  Topic 

KabirPatel
Yak Posting Veteran

54 Posts

Posted - 2007-03-09 : 07:03:35

Hi,

I have a table (TableA) as follows:

ColumnA ColumnB
----------------------
Value01 R01
Value01 R02
Value02 R04


I have another table as follows (TableB):

ColumnA ColumnB
----------------------
Value02 R01
Value01 R03
Value01 R01

I want to insert into TableA all the values from TableB that do not currently exist in TableA. I want to do this without using a cursor.

I dont want to do this with an "IF EXISTS" either as this is slow.

I tried something like the following:
INSERT INTO [TableA]([ColumnA], [ColumnB])
SELECT [ColumnA], [ColumnB]
FROM [TableB] LEFT JOIN [TableA] ON
[TableB].[ColumnA] = [TableA].[ColumnA]
AND [TableB].[ColumnB] = [TableA].[ColumnB]
WHERE [TableA].[ColumnB] IS NULL

but is doesnt seem to work.

Any ideas?

Thanks in advance.
Kabir

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-09 : 07:06:17
insert tablea (columna, columnb)
select tb.columna, tb.columnb from tableb as tb
left join tablea as ta on ta.columna = tb.columna and ta.columnb and tb.columnb
where ta.columna is null


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-09 : 07:07:13
You have to prefix your column names with table names, just as the error message tells you.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -