Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Rekursive SQL Server-Abfrage

Sehen Sie sich die Verwendung eines sogenannten CTE (allgemeiner Tabellenausdruck) an (siehe MSDN-Dokument):

;with cteAppointments as (
 select AppointmentID, PersonID, PrevAppointmentID
     from Appointments
     where PrevAppointmentID is null
 union all
 select a.AppointmentID, a.PersonID, a.PrevAppointmentID
     from Appointments a
         inner join cteAppointments c
             on a.PrevAppointmentID = c.AppointmentID
)
select AppointmentID, PrevAppointmentID
    from cteAppointments
    where PersonID = xxx