Hallo,
ich habe folgendes Problem.
Ich bin totaler Anfänger was stored procedures angeht, jedoch relativ geübt, was Delphi und VBA angeht, sprechend ist mein Code ent-standen…
Wie es scheint, wird die gesamte Funktionsbezeichnung von Postgres selber erstellt. Ich habe jedoch noch nicht verstanden, inwieweit die EIngangs oder Ergebnisargumente noch in den Deklarationen stehen müssen. Unklar ist mir auch noch inwieweit If - then Strukturen oder ähnliche Programm Strukturen problemlos neben SQL Abfragen stehen können.
Könnte mir jemand helfen, den Syntax in folgender verschachtelter Case-Abfrage so zu korrigieren, dass es dem Datenbank PL-PG- SQL konform ist ? Irgendwie kriege ich immer eine Fehlermeldung. Zwar gibt es realtiv viele Info’s zu stored procedures PL-PG-SQL im Internet, aber weniger zu PL-Pg-SQL als zu den anderen, so dass ich mir nicht sicher bin, ob da nicht auch was durcheinander geraten ist:
[size=100][font=Calibri]CREATE FUNCTION is_valid(pconreac, pconpro: text) RETURNS boolean AS [/font][/size]
[font=Lucida Console]declare @Return boolean[/font]
[size=100][font=Calibri]BEGIN[/font][/size]
[size=100][font=Calibri]Select pcon.pcontype as typepro[/font][/size]
[size=100][font=Calibri]from pcon[/font][/size]
[size=100][font=Calibri]where (pcon.symbol = pconpro);[/font][/size]
[size=100][font=Calibri]case[/font][/size]
[size=100][font=Calibri] when (pcon.typepro = 'SecondaryMaster')[/font][/size]
[size=100][font=Calibri] then [/font][/size]
[size=100][font=Calibri] begin[/font][/size]
[size=100][font=Calibri] Select pcon.pcontype as typereac[/font][/size]
[size=100][font=Calibri] from pcon[/font][/size]
[size=100][font=Calibri] where (pcon.symbol = pconreac);[/font][/size]
[size=100][font=Calibri] case[/font][/size]
[size=100][font=Calibri] when (pcon.typereac = 'PrimaryMaster')[/font][/size]
[size=100][font=Calibri] then [/font][/size][font=Lucida Console]@return = [/font][size=100][font=Calibri]true;[/font][/size]
[size=100][font=Calibri] else[/font][/size]
[font=Calibri][size=100] [/size][/font][font=Lucida Console]@return = [/font][size=100][font=Calibri]false;[/font][/size]
[size=100][font=Calibri] end case;[/font][/size]
[size=100][font=Calibri] when (pcon.typepro = 'Product')[/font][/size]
[size=100][font=Calibri] then [/font][/size]
[size=100][font=Calibri] begin[/font][/size]
[size=100][font=Calibri] Select pcon.pcontype as typereac[/font][/size]
[size=100][font=Calibri] from pcon[/font][/size]
[size=100][font=Calibri] where (pcon.symbol = pconreac);[/font][/size]
[size=100][font=Calibri] case[/font][/size]
[size=100][font=Calibri] when (pcon.typereac = 'PrimaryMaster')[/font][/size]
[size=100][font=Calibri] then [/font][/size][font=Lucida Console]@return = [/font][size=100][font=Calibri]true;[/font][/size]
[size=100][font=Calibri] when (pcon.typereac = 'SecondaryMaster')[/font][/size]
[size=100][font=Calibri] then [/font][/size][font=Lucida Console]@return = [/font][size=100][font=Calibri]true;[/font][/size]
[size=100][font=Calibri] else[/font][/size]
[font=Calibri][size=100] [/size][/font][font=Lucida Console]@return = [/font][size=100][font=Calibri]false;[/font][/size]
[size=100][font=Calibri] end case;[/font][/size]
[size=100][font=Calibri]when (pcon.typepro = 'MineralsSolids')[/font][/size]
[size=100][font=Calibri] then [/font][/size]
[size=100][font=Calibri] begin[/font][/size]
[size=100][font=Calibri] Select pcon.pcontype as typereac[/font][/size]
[size=100][font=Calibri] from pcon[/font][/size]
[size=100][font=Calibri] where (pcon.symbol = pconreac);[/font][/size]
[size=100][font=Calibri] case[/font][/size]
[size=100][font=Calibri] when (pcon.typereac = 'PrimaryMaster')[/font][/size]
[size=100][font=Calibri] then [/font][/size][font=Lucida Console]@return = [/font][size=100][font=Calibri]true;[/font][/size]
[size=100][font=Calibri] when (pcon.typereac = 'SecondaryMaster')[/font][/size]
[size=100][font=Calibri] then [/font][/size][font=Lucida Console]@return = [/font][size=100][font=Calibri]true;[/font][/size]
[size=100][font=Calibri] else[/font][/size]
[font=Calibri][size=100] [/size][/font][font=Lucida Console]@return = [/font][size=100][font=Calibri]false;[/font][/size]
[size=100][font=Calibri] end case;[/font][/size]
[size=100][font=Calibri] else[/font][/size]
[font=Lucida Console]@return = [/font][size=100][font=Calibri]false; [/font][/size]
[size=100][font=Calibri][/font][/size]
[size=100][font=Calibri]case end;[/font][/size]
[size=100][font=Calibri][size=100][font=Calibri]$$ LANGUAGE SQL;[/font][/size][/font][/size]
[size=100][font=Calibri]
[/font][/size]
In dem Code geht es darum, dass in einer (hier nicht näher erwähnten Tabelle reactions) Einträge gemacht werden, die aus einer anderen Tabelle pcon entnommen werden und nur bestimmte charakteristiken besitzen dürfen. Wenn z.B. das Reactionendsprodukt pconpro den Status ‘Secondary-Master’ -species hat, dann werden als Reaktionsausgangsprodukte nur Einträge mit dem Status ‘PrimaryMaster’ zugelassen. Entsprechende Einschränkungen gelten auch für andere Spezies. Die Procedure soll dann in einem Trigger zu der Tabelle Reactions aufgerufen werden, wobei die erwähnten Variablen pconPro dann dort New.PconProduct und pconReac dem Eintrag New.PconReactant entsprechen sollen.
Ich kenne die Postgres-SQL Dokumentation und habe 2 bis 3 Tutorials mir angeschaut, aber die betrafen entweder ganz andere Themen, waren zu einfach oder nicht ausführlich genug.
Kann jemand neben Tipps zur Lösung dieser Aufgabe ein gutes Gratis-Tutorial empfehlen oder, falls nicht vermeidbar, auch ein wirklich gutes (und günstiges) Buch, das von ganz einfachen Anfängen zu komplexen Beispielen führt ?
Vielen Dank für jede Hilfe.
ThommyW