We help IT Professionals succeed at work.

Using cursor to insert rows into multiple tables

huerita37
huerita37 asked
on
Medium Priority
1,282 Views
Last Modified: 2012-08-13
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

Comment
Watch Question

Commented:
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

Author

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.