Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    4

    Unanswered: looping insert query

    hai all...need you're help...^^

    how to do looping for INSERT query in postgresql 8.4...?? let's say I want to do INSERT to table for 100 times...

    before I used query below in SQLserver 2000 and it can works...but in postgresql 8.4 it can't...>.<

    ================================================== ==
    declare
    @count as integer

    set @count = 1
    while @count <= 100
    begin
    insert into <tablename> values('...','...','...')
    set @count = @count + 1
    end
    ================================================== ==

    OS I used = Windows XP

    thx before...^^

  2. #2
    Join Date
    Aug 2009
    Location
    Olympia, WA
    Posts
    337
    Well that's nice tsql you have there. You won't be able to run it on Postgres though. You can create procedures in a variety of languages on Postgres including c, java, perl, python, ruby, php and r. But the most common is probably pl/pgsql.
    PostgreSQL: Documentation: Manuals: PostgreSQL 8.4: PL/pgSQL - SQL Procedural Language

    But generally on Postgres, you wouldn't use a procedure for this in the first place.

    Code:
    INSERT INTO foo (myId, myDate, myStuff)
    SELECT i, '2009-01-01'::date + i, 'sample data'
    FROM generate_series(1,100) i;

  3. #3
    Join Date
    Oct 2009
    Posts
    4
    Quote Originally Posted by artacus72
    INSERT INTO foo (myId, myDate, myStuff)
    SELECT i, '2009-01-01'::date + i, 'sample data'
    FROM generate_series(1,100) i;
    waw...it works...tq very much artacus...^^

  4. #4
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    yes... generate_series is a very handy function.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •