Seltsames Verhalten von SQL bei Begin + Schleife + Create or Replace @inmemorystorage

I have some strange behaviour of QueryTool Beta 20.1.435 when having a combined create or replace table + loop with a for r in a @InMemory created table

See and test the statement:

begin
  create or replace table t1@InMemoryStorage as select 1;
  begin
    for r in (select * from t1@InMemoryStorage)
    loop
      begin
        dbms_output.put_line('hello');
      end;
    end loop;
  end;
  drop table t1@InMemoryStorage;
end

to make this working, the t1 table needs to exists before executing the statement:

create or replace table t1@InMemoryStorage as select 1;
begin
  begin
    for r in (select * from t1@InMemoryStorage)
    loop
      begin
        dbms_output.put_line('do something');
      end;
    end loop;
  end;
  drop table t1@InMemoryStorage;
end

with this statement, no errors:

begin
    create or replace table t1@InMemoryStorage as select 1;
    drop table t1@InMemoryStorage;
end;

It seems that Query Tool tries to evaluate for r in (select * from t1@InMemoryStorage) before having been able to create or replace table t1@InMemoryStorage as select 1 or am I doing something wrong, like need to declare t1@InMemoryStorage ?)

Ich werde später eine Erklärung hinzufügen, aber die Grundlagen dieser Version von Invantive SQL sind, dass das Ausführen von DDL innerhalb eines PSQL-Blocks interessante Vorteile hat. Das ist einer davon.

Wir ziehen zu einer neuen Version mit einer stärkeren Fähigkeit um, solche Szenarien zu bewältigen, indem wir Teile der Evaluierung verzögern.

Erstellen Sie die Tabelle als Workaround in einer SQL- oder PSQL-Anweisung vorher und verwenden Sie einfach DML, um sie korrekt innerhalb des Blocks zu füllen.

All excuses are granted to Invantive , QueryTool does so much things that doing a small table creation before the loop statement is affordable.
It works now as expected.

To be noted I did a:

create table t1@inmemorystorage (field1 data-type, field2 data-type….)

Because doing a lazy

create or replace table t1@inmemorystorage as select 1

With having again a create or replace table t1@inmemorystorage as select [real stuff here]

in the block statement will solve the evaluation error on the loop statement but then data types errors are appearing (1onedata-type is numeric, but if my first real stuff row is datetime and query is not happy with that).

I have the same problem on a stored procedure, where I’m creating a table in memory storage and later try to loop over it. Could you please explain a little further how to work around this issue?

The workaround is to create the table@inmemorystorage before the stored procedure statement executes. Then you can re-create it in the stored procedure and loop on it.

The issue comes from the loop that tries to parse and recognize the table@inmemorystorage whereas it is not yet created when executing the stored procedure.

Like:

create or replace table shifts@inmemorystorage
( opening_date datetime
, closing_date datetime
, shift_id varchar2
);

-- PSQL procedure starts here
declare
-- my declarations
begin 
  create or replace table shifts@inmemorystorage 
  as 
  select opening_date
  ,      closing_date
  ,      shift_id 
  from   mytable@mycontainer
  ;
--here you can also loop on your shifts@inmemorystorage;
end;

Very good, this works for me! Thank you for the elaboration.

Wie versprochen folgt hier etwas mehr Hintergrund. Es hat länger gedauert, hierher zurückzukommen, da Kapazitäten benötigt wurden, um eine außer Kontrolle geratene Situation mit einer der verbundenen Plattformen zu bewältigen.

Die Invantive-SQL-Anweisungen (einschließlich der PSQL-Blöcke, die als SQL-Anweisung durch Umschließung mit begin und end gestartet werden) gehen davon aus, dass alle Datenbankobjekte vor der Ausführung vorhanden sind und ihre Definition während der Ausführung nicht ändern.

Dies ist im Wesentlichen die Grenze zwischen DML (Data Manipulation Language) und DDL (Data Definition Language).

Während der Kompilierung von DML-Anweisungen (einschließlich PSQL) prüft die Invantive-SQL-Engine jedoch nicht immer, dass die Datenbankobjekte und der Code diese Anforderungen erfüllen. Oft können Code-Teile während der Kompilierung und Ausführung Datenbankobjekte erstellen oder ersetzen, ohne dass dies Auswirkungen hat, was der Natur des Kompilierungsprozesses von Invantive SQL geschuldet ist.

Obwohl dies Vorteile bietet, wird dieses aktuelle Verhalten als Fehler und nicht als Feature angesehen.

Die nächste Version der SQL-Engine wird die Kompilierungsphase in mehr Unterphasen aufteilen. Bestehender Code, der Datenbankobjekte wie Tabellen dynamisch erstellt, kann fehlschlagen oder weiterhin funktionieren.

Wir werden versuchen, für die nächste Version die Vorteile beizubehalten, aber die unscharfe Natur des Änderns von Datenbankobjekten während der Ausführung von DML zu entfernen.

Um wartbaren Code zu schreiben, wird empfohlen, die Datenbankobjekte in separaten SQL-Anweisungsbatches mit PSQL, SQL-DDL-Anweisungen oder dynamischem SQL zu erstellen. Das ursprüngliche DML bleibt als der zuletzt ausgeführte SQL-Batch.

Ein SQL-Batch ist eine Reihe von SQL-Anweisungen, die in einem Durchgang ausgeführt werden (siehe Invantive UniversalSQL - Invantive). Falls es Fragen gibt, behandeln Sie diese bitte in einem separaten Thema und beziehen Sie sich auf dieses Thema.