Yes... I think that this is what I'm looking to do.
On Fri, Dec 21, 2012 at 7:47 AM, David McCutcheon <[email protected]>wrote: > After you do the insert into T1 you can use the Scope_Identity() function > to get the value of the identity column for that last insert. > > so the next few lines would look something like this: > > Declare @T1Key as int = Scope_Identity() > > Insert Into T2(fk_t1_id, dept_name) > values(@T1Key, @dept_name) > > > You might not need to set to a variable first, but that's how I've always > used it. > > Hope this helps > > > On Thursday, December 20, 2012 5:34:17 AM UTC-5, X wrote: >> >> Hey guys, >> I'm working on a select/update Stored Procedure where I want to take a >> primary key from one table and insert it into another table as a foreign >> key in the insert statement. >> >> Table t1 ( >> t1_id int IDENTITY (1,1) NOT NULL, >> fname varchar(20) NULL, >> lname varchar(20) NULL, >> position varchar(20) NULL >> ) >> >> Table t2( >> t2_id int IDENTITY (1,1) NOT NULL, >> fk_t1_id int NULL, >> dept_name varchar (20) NULL >> >> >> SPROC >> >> CREATE PROCEDURE dbo.usp_insUpdt >> @fname varchar(20), >> @lname varchar(20), >> @position varchar(20), >> @dept_name varchar(20) >> >> AS >> >> BEGIN >> >> SET NOCOUNT ON >> >> IF EXISTS (SELECT @lname FROM t1 WHERE @lname = @lname) >> UPDATE t1 >> SET @fname = fname, @lname = lname, @position = position >> WHERE lname = @lname >> >> ELSE >> INSERT INTO t1(lname, fname,position) >> VALUES(@fname, @lname, @position) >> END >> --The pk from t1 above should be inserted into fk t2 >> >> >> Any suggestions? >> >> Thanks in advance >> >> >> -- > You received this message because you are subscribed to the Google > Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML > Web Services,.NET Remoting" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en > or visit the group website at http://megasolutions.net > -- You received this message because you are subscribed to the Google Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en or visit the group website at http://megasolutions.net
