?
Solved

MSSQL Insert into multiple tables

Posted on 2010-08-16
3
Medium Priority
?
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
Om Prakash earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…
Suggested Courses

770 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