Solved

MSSQL Insert into multiple tables

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

724 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