sql >> Database >  >> RDS >> Sqlserver

SQL Server recursieve query

Kijk naar het gebruik van een zogenaamde CTE (common table expression) (zie MSDN-document):

;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



  1. Hoe indexen voor een database of tabel in MySQL te zien?

  2. Database-e-mailmeldingen configureren in MS SQL Server

  3. ORDER BY ... USING-clausule in PostgreSQL

  4. Hoe te bestellen op maandnaam in MySQL