-- 为 watch_id 为 NULL 的设备分配 900-1000 之间的唯一 ID -- 注意:此脚本假设 900-1000 范围内没有被现有设备占用 -- 仅更新最多 101 条记录,以防超出范围 WITH target_devices AS ( SELECT id, ROW_NUMBER() OVER (ORDER BY bind_time ASC) as rn FROM public.ak_devices WHERE watch_id IS NULL LIMIT 101 -- 900到1000共101个数字 ) UPDATE public.ak_devices SET watch_id = 899 + target_devices.rn FROM target_devices WHERE public.ak_devices.id = target_devices.id;