Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MSSQL Insert into multiple tables

Posted on 2010-08-16
3
Medium Priority
?
655 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

598 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