Using cursor to insert rows into multiple tables

This is an addon to an earlier question.  

Earlier question:  I need to take some rows from the Personnel table that meets certain criteria and then insert those rows into the Personnel table but with a different "AgencyID", Create_ID and Create_Date.  Then I need the ID of that newly created row.

That I understand and now I need a little more done with that cursor.  Everyone in the Personnel table also has an entry in the UserAccounts table. Once I get the ID from the Personnel table I then need to copy the data for those particular people and insert them into the UserAccounts table.  Below is my full curosr. Is this the correct way to do this?
DECLARE @AgencyID INT
DECLARE @LastName VARCHAR(50)
DECLARE @FirstName VARCHAR(50)
DECLARE @Hired VARCHAR(50)
DECLARE @JobTitle VARCHAR(50)
DECLARE @SSNumber VARCHAR(50)
DECLARE @newpers INT
DECLARE @nUserID INT
 
SET @nUserID = 122
SET @AgencyID = 17
 
DECLARE c1 CURSOR FOR
 
--define cursor
SELECT p.LastName, p.FirstName, p.Hired, p.JobTitle, p.SSNumber
FROM Personnel p INNER JOIN UnitAllocation ON PersonnelID = p.ID WHERE (ReportingUnit = '7528-A')
 
OPEN c1
 
FETCH NEXT FROM c1
INTO @LastName, @FirstName,,@Hired, @JobTitle, @SSNumber
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
        INSERT INTO [Personnel] (
         AgencyID, LastName, FirstName, Hired, JobTitle, SSNumber, create_date, create_id, update_date, update_id)  
        ) VALUES (
         @AgencyID, @LastName, @FirstName,,@Hired, @JobTitle, @SSNumber, getDate(), @nUserID, getDate(), @nUserID
        )
                        
set @newpers = SELECT @@Identity
 
INSERT INTO [UserAccounts] (
		AgencyID, PersonnelId, UserName, Password, AccessLevel, EffectiveDate, 
		create_date, create_id, update_date, update_id)
SELECT @AgencyID, @newpers,  Left(p.FirstName, 1) + p.LastName + '@'+ sAgencyLoginTemplate, @Password, @AccessLeve, @EffectiveDate, getDate(), @nUserID, getDate(), @nUserID
FROM UserAccounts INNER JOIN Personnel ON ON uacct.PersonnelId = p.ID 
INNER JOIN UnitAllocation ua ON ua.PersonnelID = p.ID 
WHERE (ReportingUnit = '7528-A')
 
FETCH NEXT FROM c1
INTO @AgencyID, @LastName, @FirstName,,@Hired, @JobTitle, @SSNumber
 
END
CLOSE c1
DEALLOCATE c1

Open in new window

huerita37Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
tigin44Commented:
this will work
DECLARE @AgencyID INT
DECLARE @LastName VARCHAR(50)
DECLARE @FirstName VARCHAR(50)
DECLARE @Hired VARCHAR(50)
DECLARE @JobTitle VARCHAR(50)
DECLARE @SSNumber VARCHAR(50)
DECLARE @newpers INT
DECLARE @nUserID INT
 
SET @nUserID = 122
SET @AgencyID = 17
 
DECLARE c1 CURSOR FOR
 
--define cursor
SELECT p.LastName, p.FirstName, p.Hired, p.JobTitle, p.SSNumber
FROM Personnel p INNER JOIN UnitAllocation ON PersonnelID = p.ID WHERE (ReportingUnit = '7528-A')
 
OPEN c1
 
FETCH NEXT FROM c1
INTO @LastName, @FirstName,,@Hired, @JobTitle, @SSNumber
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
        INSERT INTO [Personnel] (
         AgencyID, LastName, FirstName, Hired, JobTitle, SSNumber, create_date, create_id, update_date, update_id)  
        ) VALUES (
         @AgencyID, @LastName, @FirstName,,@Hired, @JobTitle, @SSNumber, getDate(), @nUserID, getDate(), @nUserID
        )
                        
SELECT @newpers =  @@Identity
 
INSERT INTO [UserAccounts] (
                AgencyID, PersonnelId, UserName, Password, AccessLevel, EffectiveDate, 
                create_date, create_id, update_date, update_id)
SELECT @AgencyID, @newpers,  Left(p.FirstName, 1) + p.LastName + '@'+ sAgencyLoginTemplate, @Password, @AccessLeve, @EffectiveDate, getDate(), @nUserID, getDate(), @nUserID
FROM UserAccounts INNER JOIN Personnel ON ON uacct.PersonnelId = p.ID 
INNER JOIN UnitAllocation ua ON ua.PersonnelID = p.ID 
WHERE (ReportingUnit = '7528-A')
 
FETCH NEXT FROM c1
INTO @AgencyID, @LastName, @FirstName,,@Hired, @JobTitle, @SSNumber
 
END
CLOSE c1
DEALLOCATE c1

Open in new window

0
 
huerita37Author Commented:
I looked but I didn't notice any changes.  Did you make any?
0
 
huerita37Author Commented:
Do I need to DECLARE all of the variables for the UserAccount table?  For example,  @Password, @AccessLeve, @EffectiveDate?
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.