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
 Transact-SQL (2000)
 Need example of Insert Into statement Joined tabl

Author  Topic 

keithc
Starting Member

35 Posts

Posted - 2005-11-11 : 14:11:17
Just need an example of insert into statement so that it inserts values into 2 seperate tables using join

Kristen
Test

22859 Posts

Posted - 2005-11-11 : 14:23:20
inserts values INTO 2 separate tables using join

or

insert data into ONE table using data FROM 2 separate tables using join

For the first one you need two separate INSERT statements (or a VIEW with an INSTEAD OF TRIGGER )

The second one I can tell you how to do ...

Kristen
Go to Top of Page

keithc
Starting Member

35 Posts

Posted - 2005-11-11 : 14:28:56
2 inserts is fine can you give me an example using Table1, Table2 with a join on customer_id to add values to the following?

Column values table1.colum1, table2.column1 etc
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-11-11 : 14:42:02
Dunno if this is what you are looking for?

BEGIN TRANSACTION
INSERT INTO MyTableA(ACol1, ACol2, ACol3, ACol4, ...)
SELECT BCol1, BCol2, CCol3, CCol4, ...
FROM MyTableB AS B
JOIN MyTableC AS C
WHERE C.ID = B.ID
IF @@ERROR <> 0 GOTO GotError

INSERT INTO MyTableD(DCol1, DCol2, DCol3, DCol4, ...)
SELECT BCol1, BCol2, CCol3, CCol4, ...
FROM MyTableB AS B
JOIN MyTableC AS C
WHERE C.ID = B.ID
IF @@ERROR <> 0 GOTO GotError

-- Success!
COMMIT
GOTO Done

GotError:
ROLLBACK

Done:

Kristen
Go to Top of Page
   

- Advertisement -