Can I use variable to store sql data - II

Started by NosyManalmost 21 years ago2 messagespatches
Jump to latest
#1NosyMan
nosyman@gmail.com

Hi,

The scenario:
1. SELECT id_product FROM products WHERE product_code='PRD-030';

Now I want to insert id_product into another table :

2. INSERT INTO product_sales( id_product, sale_date)
INSERT INTO product_sales( id_product, sale_date)
INSERT INTO product_sales( id_product, sale_date)

As you can see the 'id_product' is not known. I know one solution:
INSERT INTO product_sales( (SELECT id_product FROM products WHERE
product_code='PRD-030'), sale_date)
... but I don't like it very mutch.

Can I set id_product from statement 1 to a variable and use that variable into
statements 2.

Thanks.

#2Peter Eisentraut
peter_e@gmx.net
In reply to: NosyMan (#1)
Re: Can I use variable to store sql data - II

NosyMan wrote:

Can I set id_product from statement 1 to a variable and use that
variable into statements 2.

You can't do that in plain SQL, but perhaps you want to look into
PL/pgSQL.

Please don't post to pgsql-patches unless you have a patch.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/