Page 1 of 1

How to use UniQuery SqlInsert

Posted: Thu 23 Feb 2012 02:24
by vinge
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!

Posted: Mon 27 Feb 2012 08:43
by AlexP
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;