pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryan White <bryan@arcamax.com>
To: Walt Bigelow <walt@stimpy.com>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] copy one to many?
Date: Thu, 27 Aug 1998 15:56:22 -0400
Message-ID: <001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com> (raw)

>This is a follow up to my last question on how to use INSERT INTO to copy
>rows.  I have one more hurdle... how can I copy one or more source rows to
>many dest rows, but with different target library numbers?
>
>I have this:
>INSERT INTO tblspotinfo
> (librarynumber,
> spotnumber,
> audiotypeid
...
> FROM
> tblspotinfo
> WHERE
> librarynumber = '9988';
>
>Is there a way to say, get all records with the library number = '9988'
>and copy them to a list of NEW library numbers?
>
>So source would be librarynumber 9988,
>and dest would be 4457, 4458, 4459, 4460 instead of 6666.
>
>Is SQL capabile of this or do I need a function on the server side to do
>this easily?


I think you can use a sequence to do this.  Look at the man page for
create_sequence.  I think next_seq is the built in function to retrieve the
next id.  You could call that as a column in the select portion of your
statement: ie:
CREATE SEQUENCE myseq start 4457;
INSERT into tblspotinfo (...) SELECT next_seq('myseq'), ... FROM ... WHERE
...;
DROP SEQUENCE myseq;

Of course you would only do the DROP if you were not going to use the
sequence again.






view thread (4+ messages)  latest in thread

Message-ID: <001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com>
Permalink:  ../001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com/
Also on:    postgresql.org/message-id/001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: bryan@arcamax.com, walt@stimpy.com
  Subject: Re: [SQL] copy one to many?
  In-Reply-To: <001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com>
  References: <001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox