简体   繁体   English

使用Firedac在Delphi中使用PlSql

[英]PlSql in Delphi with Firedac

I'm trying to run a plsql with firedac , but I'm not getting it. 我正在尝试使用firedac运行plsql,但我不明白。 I've tried with FDScript1 and FDQuery1 . 我已经尝试使用FDScript1和FDQuery1。

Is a parameter not found error . 是找不到参数错误。 Does anyone know to run this plsql ? 有谁知道运行此plsql吗?

the error is 错误是

FDQuery1: Parameter 'TALHAO_ID' not found. FDQuery1:找不到参数“ TALHAO_ID”。

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. 其中之一是必须使用ExecSQL进行更新/插入PostgreSQL数据库,而不是使用Openec。 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. 根据此文档, http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html将值分配给plpgsql变量与pascal / Delphi相似,使用的是分配符号“:=” not“ =: ”,如查询中一样。 Secondly, when executing a query, either via plpgsql or sql language the assignment sign is "=" not ":=" or "=:". 其次,在执行查询时,通过plpgsql或sql语言,分配符号为“ =”而不是“:=”或“ =:”。 Correct your mistakes in SQL ant the function will succeed. 更正SQL ant中的错误,该函数将成功。 Your return message is form postgreSQL backend. 您的返回消息是表单postgreSQL后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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