how to get last auto-incremented by mssql?
Posted: Wed 18 May 2011 07:59
pls get me a demo.
Discussion forums for open issues and questions concerning database tools, data access components and developer tools from Devart
https://forums.devart.com/
Code: Select all
procedure TMainForm.BitBtnWorkClick(Sender: TObject);
begin
UniQuery.SpecificOptions.Values['QueryIdentify'] := 'True';
UniQuery.SQL.Text := 'SELECT * FROM DEPT';
// table dept has three fields:
// DEPTNO INT IDENTITY(1,1) NOT NULL, DNAME VARCHAR(14) NULL, LOC VARCHAR(13) NULL
UniQuery.Open;
UniQuery.Append;
UniQuery.FieldByName('DNAME').AsString := 'test';
UniQuery.FieldByName('LOC').AsString := 'test';
UniQuery.Post; // here the value of the IDENTIFY column DEPTNO will be set automatically
end;Code: Select all
procedure TMainForm.BitBtnWorkClick(Sender: TObject);
begin
UniQuery.SQL.Text := 'SELECT * FROM DEPT';
UniQuery.SQLInsert.Clear;
UniQuery.SQLInsert.Add('INSERT INTO DEPT(DNAME, LOC) VALUES(:DNAME, :LOC)');
UniQuery.SQLInsert.Add('SET :DEPTNO = SCOPE_IDENTITY()');
UniQuery.Open;
UniQuery.Append;
UniQuery.FieldByName('DNAME').AsString := 'test';
UniQuery.FieldByName('LOC').AsString := 'test';
UniQuery.Post;
end;
procedure TMainForm.UniQueryBeforeUpdateExecute(Sender: TDataSet;
StatementTypes: TStatementTypes; Params: TDAParams);
begin
if stInsert in StatementTypes then
Params.ParamByName('DEPTNO').ParamType := ptInputOutput;
end;