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
 General SQL Server Forums
 New to SQL Server Programming
 Insert Into 2 tables using stored procedure

Author  Topic 

shammad
Starting Member

3 Posts

Posted - 2011-04-21 : 15:50:26
Hi,

I have two tables with 1 to many relationship, lets say
1- Device
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar] (50)

2- Specs
[ID] [int] IDENTITY(1,1) NOT NULL,
[DeviceID] [int] NOT NULL,
[DeviceType] [int] NOT NULL

I need to insert the data using stored procedure, but the issue is I need to get the current device.Id to insert it in spec.deviceID
where the parameters for this procedure is (name and deviceType)

please advise,
thanks,

midavis
Starting Member

23 Posts

Posted - 2011-04-21 : 16:36:19
INSERT INTO Device
( [Name] )
Values
( 'Device1' )

DECLARE @deviceId int
set @deviceId = SCOPE_IDENTITY()

INSERT INTO Specs
( [DeviceID] , [DeviceType] )
Values
( @deviceId , 'Type1' )
Go to Top of Page
   

- Advertisement -