How to use UniQuery SqlInsert

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
vinge
Posts: 1
Joined: Thu 23 Feb 2012 01:14

How to use UniQuery SqlInsert

Post by vinge » Thu 23 Feb 2012 02:24

Hello, I try to use UniQuery SqlInsert, but somthing I do not understand:

Code: Select all

procedure TForm3.FormCreate(Sender: TObject);
begin
  uniqry1.SQL.Add('sp_get');     //sp_get: CREATE PROCEDURE sp_get AS
                                   // BEGIN select * from table_test END
  uniqry1.SQLInsert.Add('sp_insert(:name,:age)') ;     //sp_insert: CREATE PROCEDURE sp_Insert 
                                   //@Name nvarchar(50),@age int AS
                                 // BEGIN insert into table_test VALUES(@Name,@age) END
  uniqry1.Open;
end;

procedure TForm3.btnInsertClick(Sender: TObject);
begin
  uniqry1.Insert;
end;

procedure TForm3.btnSaveClick(Sender: TObject);
begin
  uniqry1.ParamByName('name').Value := 'jack';   //there are wrong:"parameter 'name' not found"   ?
  uniqry1.Post;
end;
What is wrong here?
thank you!

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Mon 27 Feb 2012 08:43

Hello,

It is possible to set parameters explicitly only in TQniQuery.SQL. The parameters in the SQLInsert, SQLUpdate, SQLDelete, and SQLRefresh properties are formed automatically (if the correspondent values are not set in these properties), when calling the Insert, Update, Delete, and Refresh methods respectively.
In your case, you should use the FieldByName property instead of the ParamByName (the procedure returning the data must have fields matching with the parameter names in the insert procedure)

Code: Select all

procedure TForm3.FormCreate(Sender: TObject); 
begin 
  uniqry1.SQL.Add('sp_get');     //sp_get: CREATE PROCEDURE sp_get AS 
                                   // BEGIN select Name, age from table_test END 
  uniqry1.SQLInsert.Add('sp_insert(:name,:age)') ;     //sp_insert: CREATE PROCEDURE sp_Insert 
                                   //@Name nvarchar(50),@age int AS 
                                 // BEGIN insert into table_test VALUES(@Name,@age) END 
  uniqry1.Open; 
end; 

procedure TForm3.btnInsertClick(Sender: TObject); 
begin 
  uniqry1.Insert; 
end; 

procedure TForm3.btnSaveClick(Sender: TObject); 
begin 
  uniqry1.FieldByName('name').Value := 'jack';   
  uniqry1.Post; 
end;

Post Reply