sql存储问题

SELECT

pk_customer,

name,

code,

shortname,

pk_custclass,

pk_customer_main,

def3,

def4,

def6 

FROM

bd_customer 

WHERE

dr = '0' 这是语句

其中pk_customer 是主键 def3是有空也有值的,有值的是一定在pk_customer中有的,

我想要写个存储过程,只要def3不是空的时候传递def3的值进去,查询 select pk_customer,def3  from bd_customer where pk_customer = (传递过来的def3值) 这是第一次查询,循环到查询出的def3是空的时候返回 pk_customer 值,这个要怎么写循环

tangshi998 发布于 2022-1-13 12:50
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
1
Z4u3z1Lv6专家互助
发布于2022-1-13 14:00(编辑于 2022-1-14 09:02)

啥数据库?

with area(id,"name",f_id,leve) as (

  select  1,'中国',0,1 union all

  select  2,'湖北',1,2 union all

  select  3,'武汉',2,3 union all

  select  4,'云贵',1,2 union all

  select  5,'云南',4,3 union all

  select  6,'贵阳',4,3 union all

  select  7,'云南子区',5,4 union all

  select  8,'贵阳子区',6,4 union all

  select  9,'蔡甸',2,3

)

--使用cte递归求出每个节点的路径

,t(id,f_id,"name","level",fullpath) as (

  select a.id,a.f_id,a."name",a.leve,'$'+cast(a.id as varchar(max))+'$'

  from area a

  where a.leve=1

  union all

  select b.id,b.f_id,b."name",b.leve,t.fullpath+'->$'+cast(b.id as varchar(max))+'$'

  from area b

  inner join t on t.id=b.f_id

)

SELECT * FROM T WHERE fullpath LIKE '%$4$%'

image.png

  • 1关注人数
  • 335浏览人数
  • 最后回答于:2022-1-14 09:02
    请选择关闭问题的原因
    确定 取消
    返回顶部