skip to Main Content

INSERT INTO TABLENAME (id, country, reference, status ) Values (21, ‘INDIA’, ‘0001’, ‘Pass’ )

here id and reference are unique.

how to insert 1000 records any loop concept in postgresql DB tool DBeaver, Please help ?

I am trying manually to insert the record individually which takes lot of time. So please help me in it ?

3

Answers


  1. Assuming reference is incremental as well, below will work. You can tune the loop

    DO
    $do$
    BEGIN 
       FOR i IN 1..1000 LOOP
          INSERT INTO TABLENAME (id, country, reference, status) 
          VALUES (i, 'INDIA',  LPAD(i::varchar, 4, '0'), 'Pass');
       END LOOP;
    END
    $do$;
    
    Login or Signup to reply.
  2. No need for a loop, generate_series() can do this for you:

    INSERT INTO TABLENAME (id, country, reference, status )
    SELECT i
         ,'INDIA'
         , to_char(i, '0000')
         , 'Pass'
    FROM generate_series(1,1000) g(i);
    
    Login or Signup to reply.
  3. Better not to go with the loop. An alternative way is to import as CSV via COPY command.

    This comes with its own pros & cons.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search