Re: [SQL] copy one to many?

From: "Bryan White" <bryan(at)arcamax(dot)com>
To: "Walt Bigelow" <walt(at)stimpy(dot)com>, <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] copy one to many?
Date: 1998-08-27 19:56:22
Message-ID: 001601bdd1f4$c3f9df00$a3f0f6ce@bryan.arcamax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>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.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Chris Johnson 1998-08-27 22:35:55 Absolute value on int2 or int4 field
Previous Message Walt Bigelow 1998-08-27 19:38:44 copy one to many?