Solved

MSSQL Insert into multiple tables

Posted on 2010-08-16
3
608 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 25
Server 2008 Cluster Fail-over Errors 5 43
replication - alerts? 4 19
Sql query 34 17
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

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

21 Experts available now in Live!

Get 1:1 Help Now