Can I use variable to store sql data - II

Поиск
Список
Период
Сортировка
Искать

Can I use variable to store sql data - II

От:
NosyMan <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.

Re: Can I use variable to store sql data - II

От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
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/
FAQ