Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Loop through records

Author  Topic 

TheBigOnion
Starting Member

2 Posts

Posted - 2008-05-08 : 10:51:38
Hello,
i have a table that has some informatio regarding an office building in it.
It also has the building managers information in it. I want to create two
new tables. One with just the building info, and one with just the managers
info. Now, I have created the two new tables. In the building table, I have
one column that has the ManagerID. So I have written a query that imports
all the building info into the new table. Here is my question. How can I
insert (one at a time I guess) the Managers into their new table while
updating the new ManagerID into the buildings table? If i was doing this
through something like C# I would just insert one row at a time, get the new
ID, the update the second table. But I do now know how to build that type of
look in SQL. Can someone help me?

Thanks,
Michael

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-08 : 10:55:11
Post table structures, sample data and expected output.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

TheBigOnion
Starting Member

2 Posts

Posted - 2008-05-08 : 11:07:15
Old Table:

Buildings
RecordID BuildingName BuildingDescription ManagerName
-------- ------------ ------------------- -----------
1 Orian This is the Orian Montgomery
2 Stellar This is Stellar Johnson



New Tables:

Buildings
RecordID BuildingName BuildingDescription ManagerID
-------- ------------ ------------------- -----------
1 Orian This is the Orian 101
2 Stellar This is Stellar 102



Managers
ManagerID ManagerName
--------- -----------
101 Montgomery
102 Johnson


So, I need to have a way to read through the old table, insert the mangers name into the 'Managers' table, take that new ID that is created and insert it into the new 'Buildings' table with realated Building ID.
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2008-05-08 : 11:25:14
This should do...

CREATE TABLE #Managers (ManagerID int identity(1,1), ManagerName varchar(40))
CREATE TABLE #BuildingsOld (RecordID int,BuildingName varchar(100),BuildingDescription varchar(100),ManagerName varchar(40))
CREATE TABLE #BuildingsNew (RecordID int,BuildingName varchar(100),BuildingDescription varchar(100),ManagerID int)

INSERT INTO #BuildingsOld(RecordID,BuildingName,BuildingDescription,ManagerName)
SELECT 1,'Orian','This is the Orian','Montgomery' UNION ALL
SELECT 2,'Stellar','This is Stellar','Johnson'

INSERT INTO #Managers (ManagerName)
SELECT DISTINCT ManagerName FROM #BuildingsOld

INSERT INTO #BuildingsNew (RecordID,BuildingName,BuildingDescription,ManagerID)
SELECT A.RecordID,A.BuildingName,A.BuildingDescription,B.ManagerID
FROM #BuildingsOld A INNER JOIN #Managers B ON A.ManagerName = B.ManagerName

SELECT * FROM #BuildingsNew
SELECT * FROM #Managers

DROP TABLE #Managers
DROP TABLE #BuildingsOld
DROP TABLE #BuildingsNew


Thanks
Karunakaran
Go to Top of Page
   

- Advertisement -