ich will ja keine neue Datenmodellierung machen
Ich drücke mich wohl nicht richtig aus. Ich versuche denn noch einmal.
Zuerst
Die Tabelle product and order (Bestellung) sowie weitere sind schon lange vorhanden.
Ich möchte jetzt in einer Function eine neue Tabelle erzeugen und in einer Schleife für jedes gefunden Produkt eine neue Spalte hinzufügen. Der Name dieser Spalte soll die id des gefunden Produktes werden.
CREATE OR REPLACE FUNCTION neuronale_netze()
RETURNS integer AS
$BODY$
DECLARE
userProduct record;
prod_id_str TEXT;
count Integer;
BEGIN
CREATE Table neuronale_netze();
count := 1;
For userProduct IN (select prod_id from product where master_prod_id is not null and prod_id is not null and prba_id = 1 order by prod_id)
LOOP
IF COUNT < 1599 THEN -- Postgres not more than 1600 columns
prod_id_str := '' || userProduct.prod_id;
-- Alter Table neuronale_netze add column
EXECUTE ' ALTER TABLE ' || 'neuronale_netze' || ' ADD COLUMN ' || quote_ident(prod_id_str) || ' ' || 'INTEGER';
count := count +1;
END IF;
END LOOP;
-- Last add two needed columns
Alter Table neuronale_netze add column DHL INTEGER;
Alter Table neuronale_netze add column GLS INTEGER;
RETURN 0;
END; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Als Zweites ich in einer anderen Fuction diese Tabelle neuronale_netze füllen:
CREATE OR REPLACE FUNCTION neuronale_netze_order()
RETURNS integer AS
$BODY$
DECLARE
userOrder record;
prodArrayStr TEXT;
amountArrayStr Text;
count Integer;
productArray INTEGER[];
amountArray INTEGER[];
BEGIN
Drop Table neuronalenetze_order if exsits;
-- All order with available products
CREATE table neuronalenetze_order AS (SELECT _order.ohed_id, _item.prod_id, _item.amount
FROM order_head _order, order_item _item
WHERE _order.purchase_date >= to_date('2013-01-01 00:00:00', 'YYYY-MM-DD hh24:mi:ss') and (_order.clie_id = ANY (ARRAY[1, 2])) AND _order.ohed_id = _item.ohed_id AND (_order.osta_id = ANY (ARRAY[60])) AND (_item.prod_id IN ( SELECT product.prod_id
FROM product
WHERE product.master_prod_id IS NOT NULL and prba_id = 1 ))
group by _order.ohed_id, _item.prod_id, _item.amount order by _item.prod_id, _order.ohed_id);
count := 1;
For userOrder IN (select * from neuronalenetze_bestellung order by ohed_id)
LOOP
count := count +1;
amountArray := (select array(select amount from neuronalenetze_order where ohed_id = userOrder.ohed_id));
productArray := (select array(select prod_id from neuronalenetze_order where ohed_id = userOrder.ohed_id));
amountArrayStr := (select array_to_string(amountArray, ','));
prodArrayStr := (select array_to_string(productArray, ','));
-- INSERT INTO neuronale_netze (column1,column2,column3,...) VALUES (value1,value2,value3,...);
EXECUTE ' INSERT INTO neuronale_netze (' || prodArrayStr || ')' || 'VALUES(' || amountArrayStr || ')';
END LOOP; -- END LOOP;
RETURN count;
END; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Zum Schluss will ich diese gefüllte Tabelle zur Weitergabe an ein anderes Externes Programm als Datei (txt, csv oder) exportieren.
Da PG nur bis 1600 Spalten verarbeiten kann, ich aber mehr als 1600 Produkte habe, kam meine Frage nach einer Erweiterung der Anzahl der Spalten in einer Tabelle.
Und diese denn möglich oder nicht? Und wenn ja wie kann ich das machen?
Das das alles nicht Regelkonform und so weiter ist, ist mir bekannt!
Besser beschrieben?