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.
| Author |
Topic |
|
fgeorge
Starting Member
6 Posts |
Posted - 2004-09-11 : 06:10:52
|
| have a bunch of rows (the result of a query) which i want to insert into a table called TableA..i want to loop through all of them and only insert those not found in TableB..I think i have the inserting part but my problem is the looping bit..thanks..i dont want to do this with a cursor..--------------------------------------if not exists (select uniquekey from TableA whereuniquekey = @keyfrom_my_view) BEGIN INSERT INTO TableA select * from my_view where keyfrom_my_view = @keyfrom_my_view END--------------------------The "not exists" part is important to avoid inserting duplicates and saves me having to do error handling..thanks again. |
|
|
hgorijal
Constraint Violating Yak Guru
277 Posts |
Posted - 2004-09-11 : 06:23:30
|
| I don't see table B in your query.. but this is what I'm guessing it is in the IF statement.does this help.... (no loops)INSERT INTO TableA select * from my_view where not exists (select 'x' from TABLEB where TABLEB.uniquekey = keyfrom_my_view)Hemanth GorijalaBI Architect / DBA |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-09-11 : 07:00:44
|
| -- #a could be a derived table in the insertselect * into #a from tablea where 1 = 0insert #a select myqueryinsert tableaselect #a.*from #aleft join tablea aon #a.col1 = a.col1 -- put however many cols you need to distinguish the rows here - careful about nullsand #a.col2 = a.col2where a.col1 is null==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|