mercredi 21 janvier 2015

SQL Insert Using Records From Another Insert

I have 3 tables, as follows:



tblAgentVisit (VisitID auto-increments)
VisitID (PK) | StatusID | A bunch of other columns
--------------------------------------------------
1 | 1 | etc.
2 | 1 | etc.

tblAgentVisitAgents
VisitID | AgtID | Prime
-----------------------
1 | 8507 | 1
2 | 56 | 1

tblAgentVisitLoad
AgtID
-----
2077
3068
432


Every quarter, we take our top 20% of agents, and load their IDs into tblAgentVisitLoad.


I need to create a new, unique visit for each agent in the table. I need to INSERT my StatusID and other columns to tblAgentVisit, grab the auto-incremented VisitID (using OUTPUT?), then INSERT VisitID, the AgtID, and Prime = 1 into tblAgentVisitAgents. Again, each visit has to be unique for each agent.


Ideally, this is how my tables would look when I'm done:



tblAgentVisit (VisitID auto-increments)
VisitID (PK) | StatusID | A bunch of other columns
--------------------------------------------------
1 | 1 | etc.
2 | 1 | etc.
3 | 1 | etc.
4 | 1 | etc.
5 | 1 | etc.

tblAgentVisitAgents
VisitID | AgtID | Prime
-----------------------
1 | 8507 | 1
2 | 56 | 1
3 | 2077 | 1
4 | 3068 | 1
5 | 432 | 1


Anyone have any suggestions for doing this within MS SQL Server 2005? I'm totally stumped.


Aucun commentaire:

Enregistrer un commentaire