Solved

MSSQL Insert into multiple tables

Posted on 2010-08-16
3
618 Views
Last Modified: 2012-05-10
Hi

I want to insert into 2 tables multiple values.  e.g.

I have 2 tables.  

Table 1 = Emails_Sent
ES_ID, Cont_ID, DateTime

Table2 = Countrys_SentTo
CST_ID, ES_ID, Country_ID

A contact has multiple Countries.  When an e-mail is sent to them it logs in the Emails_Sent
and also logs the countries that contact is responsible for.  

If this was just one I'd have no problem using SCOPE_IDENTITY(), but I need to be able to do multiple e-mails sent at once

I have tried
Declare @ID int
Insert into  Emails_Sent (Cont_ID, DateTime) Select Cont_ID, getdate() From MyContTable
Set @ID = SCOPE_IDENTITY()
Insert into  Countrys_SentTo (ES_ID, Country_ID) Select @ID, County_F From MyCont_County_Table

The other issue I have is linking the 2 inserts into together e.g.MyContTable.Cont_ID = MyCont_County_Table.Cont_ID

I think I need to loop through the first, but don’t know how to do this.

Can any one help

Thanks

Brasso
0
Comment
Question by:brasso_42
3 Comments
 
LVL 22

Accepted Solution

by:
Om Prakash earned 500 total points
ID: 33444824
You can use cursor to do the same. Example:

Declare @ID int

DECLARE @Cont_ID INT





DECLARE db_cursor CURSOR FOR Select Cont_ID  From MyContTable

OPEN db_cursor   

FETCH NEXT FROM db_cursor INTO @Cont_ID

WHILE @@FETCH_STATUS = 0   

BEGIN   

	Insert into  Emails_Sent (Cont_ID, DateTime) values (@Cont_ID, getdate())

	Set @ID = SCOPE_IDENTITY()

	Insert into  Countrys_SentTo (ES_ID, Country_ID) Select @ID, County_F From MyCont_County_Table        



       FETCH NEXT FROM db_cursor INTO @Cont_ID

END   



CLOSE db_cursor   

DEALLOCATE db_cursor 

Open in new window

0
 
LVL 3

Expert Comment

by:man2002ua
ID: 33445319
The example above is simple and will work on any MSSQL, but as I can see you are using 2008, so you can use all power of MSSQL2008:


DECLARE @TmpTable table( InsID INTEGER);



Insert into  Emails_Sent (Cont_ID, DateTime) 

   OUTPUT INSERTED.<YOUR_IDENTITY_NAME_HERE>

   INTO @TmpTable

Select Cont_ID, getdate() From MyContTable;



Insert into  Countrys_SentTo (ES_ID, Country_ID) 

Select tmp.InsID, County_F From @TmpTable tmp, MyCont_County_Table;

Open in new window

0
 
LVL 1

Author Closing Comment

by:brasso_42
ID: 33445329
Awsome workd like a dream!

Many thanks

Brasso
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

920 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