简体   繁体   中英

PlSql in Delphi with Firedac

I'm trying to run a plsql with firedac , but I'm not getting it. I've tried with FDScript1 and FDQuery1 .

Is a parameter not found error . Does anyone know to run this plsql ?

the error is

FDQuery1: Parameter 'TALHAO_ID' not found.

unit Unit11;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, Data.DB,
  FireDAC.Comp.Client,FireDAC.Phys.PG, FireDAC.VCLUI.Wait, FireDAC.Stan.Param,
  FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Vcl.StdCtrls, Vcl.Grids,
  Vcl.DBGrids, FireDAC.Comp.DataSet, FireDAC.Comp.UI, Datasnap.Provider,
  Datasnap.DBClient, JvExStdCtrls, JvCombobox, JvDBCombobox, JvExControls,
  JvDBLookup, FireDAC.Comp.ScriptCommands, FireDAC.Comp.Script;

type
  TForm11 = class(TForm)
    Connection: TFDConnection;
    FDWait: TFDGUIxWaitCursor;
    FDQuery1: TFDQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Button1: TButton;
    FDPhysPgDriverLink1: TFDPhysPgDriverLink;
    FDManager1: TFDManager;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    JvDBComboBox1: TJvDBComboBox;
    ComboCultura: TJvDBLookupCombo;
    FDScript1: TFDScript;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form11: TForm11;

implementation

{$R *.dfm}

procedure TForm11.Button2Click(Sender: TObject);
begin

 FDQuery1.Close;
 FDQuery1.SQL.text:='DO LANGUAGE plpgsql $$ '+
    'BEGIN                    '+
    '   update  talha_safra set  TALHAO_ID=:TALHAO_ID, SAFRA_ID=:SAFRA_ID, SIT_CADASTRAL=:SIT_CADASTRAL, REPLICACAO_EFETUADA=:REPLICACAO_EFETUADA where talhao_id=:talhao_id and safra_id=:safra_id ' +
    '        IF found THEN  '+
    '              RETURN;      '+
    '        END IF;            '+
    '            '+
    '            '+
    ' INSERT INTO TALHAO_SAFRA (TALHAO_ID, SAFRA_ID, SIT_CADASTRAL, REPLICACAO_EFETUADA)  VALUES  (:TALHAO_ID,:SAFRA_ID,:SIT_CADASTRAL,:REPLICACAO_EFETUADA) '+
    '        RETURN; '+
    '        EXCEPTION WHEN unique_violation THEN '+
    '        END; '+
    '               '+
    'END;             '+
    '$$;';
     FDQuery1.Params.ParamByName('TALHAO_ID').AsInteger:=12;
     FDQuery1.Params.ParamByName('SAFRA_ID').AsInteger:=1  ;
     FDQuery1.Params.ParamByName('SIT_CADASTRAL').AsString:='Ativo';
     FDQuery1.Params.ParamByName('REPLICACAO_EFETUADA').AsString:='NÃO';
     FDQuery1.Open;


end;

procedure TForm11.FormCreate(Sender: TObject);
begin
Connection.Connected:=true;
end;

end.  

You have several issues. One of them is that instead of Open you have to use ExecSQL for an Update/Insert into a postgreSQL Database. The second issue is that your query is wrong. According to this document http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html assigning values to a plpgsql variable is done similar to pascal/Delphi using the assignment sign ":=" not "=:" as in your query. Secondly, when executing a query, either via plpgsql or sql language the assignment sign is "=" not ":=" or "=:". Correct your mistakes in SQL ant the function will succeed. Your return message is form postgreSQL backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM