Wie kann ich explizit einfügen von Nullen in eine parametized Abfrage?

Ich bin mit Delphi 7 und Firebird 1.5.

Ich habe eine Abfrage, die ich zur Laufzeit erstellen, wo einige der Werte kann null sein. Ich kann nicht herausfinden, wie man Firebird zu akzeptieren explizite null-Werte für Werte, die ich brauche zu verlassen, als null. In dieser Phase Baue ich den SQL so, dass ich nicht die Parameter enthalten, die null sind, aber das ist mühsam und fehleranfällig.

var
  Qry: TSQLQuery;
begin
  SetConnection(Query); // sets the TSQLConnection property to a live database connection
  Query.SQL.Text := 'INSERT INTO SomeTable (ThisColumn) VALUES (:ThisValue)';
  Query.ParamByName('ThisValue').IsNull := true; // read only, true by default
  Query.ParamByName('ThisValue').Clear; // does not fix the problem
  Query.ParamByName('ThisValue').IsNull = true; // still true
  Query.ParamByName('ThisValue').Bound := true; // does not fix the problem
  Query.ExecSQL;

Derzeit eine EDatabaseError "Kein Wert für parameter 'ThisValue'"' ausgelöst wird, in DB.pas also ich vermute, das ist von design als vielmehr einer firebird-problem.

Kann ich legen Sie die Parameter auf NULL? Wenn ja, wie?

(edit: sorry, für die nicht explizit versucht .Klar vor. Ich ließ es sich für die Erwähnung IsNull. Haben Hinzugefügt, Erklärung und mehr code)

Sorry, noch eine Sache: es gibt keine "not NULL" - constraint auf der Tabelle. Ich glaube nicht, dass es immer so weit, aber dachte, ich sollte sagen.

Komplette console-app, die zeigt das problem an meinem Ende:

program InsertNull;

{$APPTYPE CONSOLE}

uses
  DB,
  SQLExpr,
  Variants,
  SysUtils;

var
  SQLConnection1: TSQLConnection;
  Query: TSQLQuery;
begin
  SQLConnection1 := TSQLConnection.Create(nil);

  with SQLConnection1 do
  begin
    Name := 'SQLConnection1';
    DriverName := 'Interbase';
    GetDriverFunc := 'getSQLDriverINTERBASE';
    LibraryName := 'dbexpint.dll';
    LoginPrompt := False;
    Params.clear;
    Params.Add('Database=D:\Database\ZMDDEV12\clinplus');
    Params.Add('RoleName=RoleName');

    //REDACTED Params.Add('User_Name=');
    //REDACTED Params.Add('Password=');

    Params.Add('ServerCharSet=');
    Params.Add('SQLDialect=1');
    Params.Add('BlobSize=-1');
    Params.Add('CommitRetain=False');
    Params.Add('WaitOnLocks=True');
    Params.Add('ErrorResourceFile=');
    Params.Add('LocaleCode=0000');
    Params.Add('Interbase TransIsolation=ReadCommited');
    Params.Add('Trim Char=False');
    VendorLib := 'gds32.dll';
    Connected := True;
  end;
  SQLConnection1.Connected;
  Query := TSQLQuery.Create(nil);
  Query.SQLConnection := SQLConnection1;
  Query.Sql.Text := 'INSERT INTO crs_edocument (EDOC_ID, LINKAGE_TYPE) VALUES (999327, :ThisValue)';
  //Query.ParamByName('ThisValue').IsNull := true; // read only, true by default
//  Query.ParamByName('ThisValue').Value := NULL;
  Query.ParamByName('ThisValue').clear; // does not fix the problem
  Query.ParamByName('ThisValue').Bound := True; // does not fix the problem
//  Query.ParamByName('ThisValue').IsNull; // still true
  Query.ExecSQL;
end.

InformationsquelleAutor der Frage | 2011-06-15

Schreibe einen Kommentar