Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:15:03+00:00 2026-06-09T01:15:03+00:00

Example: Table=[category1,category2,category3] for varTable in Table: cr.execute(‘update varTable SET id=%s,WHERE id=%s) …….. …. How

  • 0

Example:

Table=["category1","category2","category3"]

for varTable in Table:
    cr.execute('update varTable SET id=%s,WHERE id=%s)
     ........
       ....

How to do this loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:15:06+00:00Added an answer on June 9, 2026 at 1:15 am

    Use dynamic SQL for that. The default is to use plpgsql with EXECUTE.
    Create a function or use a DO statement for ad-hoc execution.

    Dynamic SQL

    CREATE OR REPLACE FUNCTION f_up(_new_id int, _old_id int)
      RETURNS void AS
    $BODY$
    DECLARE
        _tbl text[] := '{category1,category2,category3}';
        t    text;
    
    BEGIN
    
    FOREACH t IN ARRAY _tbl
    LOOP
        EXECUTE '
        UPDATE ' || t || '
        SET    id = $1
        WHERE  id = $2'
        USING  _new_id, _old_id;
    END LOOP;
    
    END;
    $BODY$ LANGUAGE plpgsql;
    

    Call:

    SELECT f_up(23, 45);
    

    There are lots of similar answers on SO. Search for dynamic-sql, plpgsql and EXECUTE for more examples and explanation.

    Plain SQL

    If plpgsql is still black magic to you, you can solve this simple case quite effectively with a data-modifying CTE. Requires PostgreSQL 9.1 – for data-modifying CTE.

    WITH vals AS (
        SELECT 23 AS new_id, 45 AS old_id -- provide values once
        )
        , a AS (
        UPDATE category1
        SET    id = v.new_id
        FROM   vals v
        WHERE  id = v.old_id
        )
        , b AS (
        UPDATE category2
        SET    id = v.new_id
        FROM   vals v
        WHERE  id = v.old_id
        )
    UPDATE category3
    SET    id = v.new_id
    FROM   vals v
    WHERE  id = v.old_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the example table: Column | 1st record | 2nd record | 3rd
Take for example this table (let's call it BIN_TABLE ): +------+------+ | A |
I have two columns in my products table, category1, and category2. In my categories
Example: Table: box boxID color 01 red 02 blue 03 green Table: boxHas boxID
Example table content 'main' | id | total | | 1 | 10 |
Ok, I have an example table with the following information and query. First up
I have two tables, for example: Table A Table B ======= ======= Name |
How can I concatenate two char columns within a perform screen? example: table sample
I have three records in a table example: *Usernames* *email* *city* Nick nick@example.com London
Example: an Employee table with an optional DateOfBirth field can be normalized into two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.