• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1264
  • Last Modified:

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

0
huerita37
Asked:
huerita37
  • 2
1 Solution
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now