CREATE FUNCTION [dbo].[f_GetParentCode](@id int)
  RETURNS @re TABLE(id int,pid int,level int)
  AS
  begin
  declare @level int
  set @level = 1
  declare @pid int
  select @pid = ParentCode from T_GWZJ_CityMapping where HCityCode = @id
  insert @re
  select HCityCode,ParentCode,@level from T_GWZJ_CityMapping where HCityCode = @id
  set @level = @level + 1
  insert @re
  select HCityCode,ParentCode,@level from T_GWZJ_CityMapping where HCityCode = @pid
  while @@rowcount > 0  
  begin
  set @level = @level + 1
  select @pid = ParentCode from T_GWZJ_CityMapping where HCityCode = @pid
  insert @re
  select HCityCode,ParentCode,@level from T_GWZJ_CityMapping where HCityCode = @pid
  end
  return
  end

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

GO

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄