Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to insert into 2 tables sql server 2005?

Author  Topic 

collie
Constraint Violating Yak Guru

400 Posts

Posted - 2010-05-06 : 04:37:37
Hi

I need help please.

I have a sp with begin tran and commit tran that needs to insert records into 2 tables.
The first table is NoticeInformation and the second insert is into TasksActionData. The primary key information_id needs to be inserted into TasksActionData column information_id.

sample data inserted into NoticeInformation
information_id information_details
1 gli
2 cat
3 data

TasksActionData
ActionDataId information_id
2 1
2 2
4 3

sample sp:
begin try
BEGIN tran
--Get The id of the GLI Status from Carton Status
DECLARE @GLI AS int
SET @GLI =
(SELECT cartonstatus_id
FROM CartonStatus
WHERE (cartonstatus_name = 'GLI'))
--Create a temp table to hold Temp cartons ids
CREATE table #tempPLs(id int identity, packlistid nvarchar(20))
--Fill the temptable with the pl ids that their status must be changed
insert into #tempPLs
--Find Packlists that received GLI in the last 24 hours ie one day
SELECT PackList_id
FROM PackList
where packlist.packlist_statusDate< DateADD(day,-1,Getdate())


---INSERT INTO NOTICES AND TASK ACTION
DECLARE @notice_id INT

if not exists (select notice_Message from notice where notice_Message='PL remains in GLI in the last 24hrs')
begin
INSERT INTO [nicita].[dbo].[Notice]
([notice_Message])
VALUES
('PL remains in GLI in the last 24hrs')
set @notice_id=@@identity
insert into noticegroup (notice_id,groups_id)
values (@notice_id,2)--finance group
end
else --notice exists already so only get notice id
begin
set @notice_id=(select top 1 notice_id from notice where notice_Message='PL remains in GLI in the last 24hrs')
end
INSERT INTO [NoticeInformation]
([information_details]
,[information_date]
,[information_done]
,[notice_id])
select 'GLI PL: ' + packlistid
,getdate()
,0
,@notice_id
from #tempPLs



INSERT INTO [TasksActionData]
(information_id
,[Data]--PL
,[ActionId])
SELECT @@identity,PACKLISTID,4 --none
from #tempPLs


COMMIT TRAN -- Transaction Success!



CREATE TABLE [dbo].[NoticeInformation](
[information_id] [bigint] IDENTITY(1,1) NOT NULL,
[information_details] [varchar](max) NULL,
[information_date] [datetime] NULL,
[information_done] [bit] NULL ,
[notice_id] [int] NOT NULL

The 2nd table is TasksActionData
CREATE TABLE [dbo].[TasksActionData](
[ActionDataId] [int] IDENTITY(1,1) NOT NULL,
[information_id] [bigint] NOT NULL,
[Data] [nvarchar](max) NULL,
[ActionId] [int] NULL


Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-06 : 13:23:35
use OUTPUT clause and gets the generated ids onto a temporary table. then use this for populating the second table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -