将相同的SQL查询应用于所有合作伙伴 [Odoo]

huangapple go评论74阅读模式
英文:

Apply the same SQL query to all partners [Odoo]

问题

我想对每个合作伙伴都添加一个税收,如果可能的话,只需一个查询。我有4700个合作伙伴,所以逐个查询是不可行的。我正在寻找一个像循环函数一样执行此操作的查询。

这是我已经有的查询。在这种情况下,仅适用于ID为9915的合作伙伴(并且可以正常工作)。

INSERT INTO 
    res_partner_perception 
      (id, perception_id, create_date, percent,  excluded, partner_id) 
VALUES 
    ((SELECT COALESCE(MAX(id), 0) FROM res_partner_perception) + 1,114,
      now(),3,0,9915);

但我想将此查询应用于数据库中存在的所有partner_ids。我该如何做到这一点?

英文:

I want to add a tax to every single partner with a single query if it's possible. I have 4700 partners, so it's non-viable to do it query by query. I'm looking for a query that do this like if it's a loop function.

This is the Query that I already have. In this case only apply for partner with ID 9915 (And works OK).

INSERT INTO 
    res_partner_perception 
      (id, perception_id, create_date, percent,  excluded, partner_id) 
VALUES 
    ((SELECT COALESCE(MAX(id), 0) FROM res_partner_perception) + 1,114,
      now(),3,0,9915);

But i want to apply this query to ALL partner_ids that exists in the database. How can i do this?

答案1

得分: 0

INSERT INTO res_partner_perception
(id, perception_id, create_date, percent, excluded, partner_id)
SELECT
(SELECT COALESCE(MAX(id), 0) FROM res_partner_perception) + row_number() over(),
114,
now(),
3,
0,
p.id
FROM all_partner_ids p;

英文:

Assuming that there is a table all_partner_ids in your database you could do a query like this:

INSERT INTO res_partner_perception 
          (id, perception_id, create_date, percent,  excluded, partner_id) 
    SELECT
        (SELECT COALESCE(MAX(id), 0) FROM res_partner_perception) + row_number() over(),
    		114,
    		now(),
    		3,
    		0,
    		p.id
    FROM all_partner_ids p;

huangapple
  • 本文由 发表于 2023年3月7日 06:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定