递归调用写法.sql 1004 B

123456789101112131415161718
  1. /****** Script for SelectTopNRows command from SSMS ******/
  2. SELECT [ID]
  3. ,[Region_code]
  4. ,[Region_name]
  5. ,[Region_ParentID]
  6. ,[Region_level]
  7. ,[Region_order]
  8. ,[Region_name_en]
  9. ,[Region_shortname_en]
  10. FROM [DYBBERPDB].[dbo].[tb_ErpRegion]
  11. --递归调用写法
  12. WITH r AS(
  13. SELECT * FROM [tb_ErpRegion] WHERE id = 1
  14. union ALL
  15. SELECT [tb_ErpRegion].* FROM [tb_ErpRegion], r WHERE [tb_ErpRegion].[Region_ParentID] = r.id
  16. )
  17. SELECT * FROM r ORDER BY id;