I have a stored procedure with several insert / select statements. Let's say I use the first insert to populate the Manager table. Upon insertion, the Manager ID is added (with the addition automatically), but is not specified in the insert statement. Then I want to use the ManagerId from this table to insert a row into another table, where ManagerId is the foreign key. Sample code as follows.
USE [TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sptInsertNewManager]
@FName varchar(50),
@LName varchar(50),
@EMail varchar(100),
@UserRoleID int,
@LANUserID varchar(25),
@GroupID int
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [Manager]
([FName],
[LName],
[Email],
[UserRoleID],
[LANUserID],
[ActiveFlag],
[GroupID]
)
VALUES
(@FName
,@LName
,@EMail
,@UserRoleID
,@LANUserID
,1
,@GroupID);
COMMIT
SELECT ManagerId FROM [Manager] AS newManager WHERE LANUserID = @LANUserID;
INSERT INTO [dbo].[aspnet_Users] (
[UserId],
[UserName],
[LoweredUserName],
[ManagerId]
)
VALUES (
NEWID(),
@LANUserID,
LOWER(@LANUserID),
newManager)
END
This obviously does not work. It was my attempt to solve this. I am new to SQL, so any help with this problem would be greatly appreciated.
Chris source
share