Problema no insert

Delphi

26/07/2013

Pessoal, estou com o seguinte problema, possuo um cadastro de locação de produtos ortopédicos, porem agora quando vou gravar um novo registro ao invés do gerador me gerar um novo numero para o novo cadastro quando o insert termina ele praticamente me retorna o código do ultimo registro no cadastro assim não efetuando o insert da nova locação alguém já viu isso ?

Detalhe, eu já cadastrei vários registros agora que começou acontecer.
Andre Ucella

Andre Ucella

Curtidas 0

Respostas

Wilton Júnior

Wilton Júnior

26/07/2013

Ola boa tarde isso é computacao coisa inesplicaveis acontece
Esse insert que vc tah fazendo como é qual componente vc tah usando ou tah utilizando comando sql?
posta o comando de inserir para auto numerar para para entender melhor o que ta acontecendo para lhe ajudar.
GOSTEI 0
Andre Ucella

Andre Ucella

26/07/2013

Tenhos as seguintes tabelas, Generator e Trigger;

CREATE TABLE LOCACAO (
PK COD_LOC INTEGER NOT NULL,
DATA_LOCACAO DATE,
VALOR_TOTAL NUMERIC(18,2),
FK COD_CLIENTE INTEGER,
STATUS CHAR(1),
DATA_ENTREGA DATE,
DATA_PREVISAO_ENTREGA DATE,
SEM_PREVISAO_ENTREGA CHAR(1),
DIARIA_MENSAL CHAR(1),
VALOR_MENSAL NUMERIC(18,2),
VENCIMENTO_MENSAL DATE
);

CREATE GENERATOR LOCACAO;

CREATE TRIGGER LOCACAO FOR LOCACAO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.cod_loc IS NULL) THEN
NEW.cod_loc = GEN_ID(locacao,1);
END

CREATE TABLE LOCACAO_ITENS (
PK CHAVE INTEGER NOT NULL,
FK COD_LOC INTEGER NOT NULL,
FK COD_PROD INTEGER NOT NULL,
DIAS INTEGER,
VALOR NUMERIC(18,2),
DATA_LOCACAO DATE,
DATA_ENTREGA DATE,
VALOR_TOTAL NUMERIC(18,2),
ENTREGUE CHAR(1),
VALOR_MENSAL DECIMAL(18,2),
STATUS_PRODUTO CHAR(1)
);

CREATE GENERATOR LOCACAO_ITENS;

CREATE TRIGGER LOCACAO_ITENS FOR LOCACAO_ITENS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.chave IS NULL) THEN
NEW.chave = GEN_ID(locacao,1);
END

Abaixo o codigo Insert que estou usando;


procedure Tfrmlocacao.SpeedButton4Click(Sender: TObject);
begin
if edtcheck.Text = 'INC' then
begin
DM.Qlocacao.close;
DM.Qlocacao.sql.clear;
DM.Qlocacao.sql.add('insert into locacao (DATA_LOCACAO ,VALOR_TOTAL, COD_CLIENTE, STATUS, DATA_ENTREGA, DATA_PREVISAO_ENTREGA, SEM_PREVISAO_ENTREGA, DIARIA_MENSAL, VALOR_MENSAL, VENCIMENTO_MENSAL)');
DM.Qlocacao.sql.add('values (:parDATA_LOCACAO ,:parVALOR_TOTAL, :parCOD_CLIENTE, :parSTATUS, :parDATA_ENTREGA, :parDATA_PREVISAO_ENTREGA, :parSEM_PREVISAO_ENTREGA, :parDIARIA_MENSAL, :parVALOR_MENSAL, :parVENCIMENTO_MENSAL)');
DM.Qlocacao.ParamByName('parDATA_LOCACAO').AsDate := StrToDate(Datelocacao.Text);
DM.Qlocacao.ParamByName('parVALOR_TOTAL').AsFloat := 0.00;
DM.Qlocacao.ParamByName('parCOD_CLIENTE').AsInteger := StrToInt(cod_cli.Text);
DM.Qlocacao.ParamByName('parSTATUS').AsString := 'A';

if edtprevisao.Text = ' / / ' then
begin
DM.Qlocacao.ParamByName('parDATA_ENTREGA').AsDate := Null;
end;

if edtprevisao.Text = ' / / ' then
begin
DM.Qlocacao.ParamByName('parDATA_PREVISAO_ENTREGA').AsDate := Null;
end;

if edtprevisao.Text >= Datelocacao.Text then
begin
DM.Qlocacao.ParamByName('parDATA_PREVISAO_ENTREGA').AsDate := StrToDate(edtprevisao.Text);
end;

if Chprevisao.Checked then
DM.Qlocacao.ParamByName('parSEM_PREVISAO_ENTREGA').AsString := 'S'
else
DM.Qlocacao.Parambyname('parSEM_PREVISAO_ENTREGA').AsString := 'N';

if RadioButton1.Checked = True then
begin
DM.Qlocacao.ParamByName('parDIARIA_MENSAL').AsString := 'D'
end;

if RadioButton2.Checked = True then
begin
DM.Qlocacao.ParamByName('parDIARIA_MENSAL').AsString := 'M';
end;

if edtvlmensal.Text = '' then
DM.Qlocacao.ParamByName('parVALOR_MENSAL').Value := StrToCurr('0,00')
else
DM.Qlocacao.ParamByName('parVALOR_MENSAL').Value := StrToCurr(edtvlmensal.Text);

if edtprevisao.Text = ' / / ' then
begin
DM.Qlocacao.ParamByName('parVENCIMENTO_MENSAL').AsDate := Null;
end;

if edtprevisao.Text >= Datelocacao.Text then
begin
DM.Qlocacao.ParamByName('parVENCIMENTO_MENSAL').AsDate := StrToDate(edtprevisao.Text);
end;

DM.Qlocacao.ExecSQL;
DM.IBTransaction.CommitRetaining;

DM.Qlocacao.Close;
DM.Qlocacao.SQL.Clear;
DM.Qlocacao.SQL.Add('select * from locacao');
DM.Qlocacao.Open;

DM.Qlocacao.Last;

cod_locacao.Text := DM.Qlocacao.fieldbyname('cod_loc').AsString;

Panel1.Enabled := False;
GroupBox1.Enabled := True;
edtcodpro.SetFocus;

BitBtn1.Enabled := true;
SpeedButton6.Enabled := true;
SpeedButton7.Enabled := true;
SpeedButton5.Enabled := false;
SpeedButton4.Enabled := false;


end;

if edtcheck.Text = 'EDIT' then
begin


DM.Qlocacao.close;
DM.Qlocacao.sql.clear;
DM.Qlocacao.sql.add('update locacao set DATA_LOCACAO =:parDATA_LOCACAO ,VALOR_TOTAL =:parVALOR_TOTAL, COD_CLIENTE =:parCOD_CLIENTE, STATUS =:parSTATUS, DATA_ENTREGA =:parDATA_ENTREGA, DATA_PREVISAO_ENTREGA =:parDATA_PREVISAO_ENTREGA, ' +
'SEM_PREVISAO_ENTREGA =:parSEM_PREVISAO_ENTREGA, DIARIA_MENSAL =:parDIARIA_MENSAL, VALOR_MENSAL =:parVALOR_MENSAL, VENCIMENTO_MENSAL =:parVENCIMENTO_MENSAL');
DM.Qlocacao.ParamByName('parDATA_LOCACAO').AsDate := StrToDate(Datelocacao.Text);
DM.Qlocacao.ParamByName('parVALOR_TOTAL').AsFloat := 0.00;
DM.Qlocacao.ParamByName('parCOD_CLIENTE').AsInteger := StrToInt(cod_cli.Text);
DM.Qlocacao.ParamByName('parSTATUS').AsString := 'A';

if edtprevisao.Text = ' / / ' then
begin
DM.Qlocacao.ParamByName('parDATA_ENTREGA').AsDate := Null;
end;

if edtprevisao.Text = ' / / ' then
begin
DM.Qlocacao.ParamByName('parDATA_PREVISAO_ENTREGA').AsDate := Null;
end;

if edtprevisao.Text >= Datelocacao.Text then
begin
DM.Qlocacao.ParamByName('parDATA_PREVISAO_ENTREGA').AsDate := StrToDate(edtprevisao.Text);
end;

if Chprevisao.Checked then
DM.Qlocacao.ParamByName('parSEM_PREVISAO_ENTREGA').AsString := 'S'
else
DM.Qlocacao.Parambyname('parSEM_PREVISAO_ENTREGA').AsString := 'N';

if RadioButton1.Caption = 'Diario' then
DM.Qlocacao.ParamByName('parDIARIA_MENSAL').AsString := 'D'
else
DM.Qlocacao.ParamByName('parDIARIA_MENSAL').AsString := 'M';

if edtvlmensal.Text = '' then
DM.Qlocacao.ParamByName('parVALOR_MENSAL').Value := StrToCurr('0,00')
else
DM.Qlocacao.ParamByName('parVALOR_MENSAL').Value := StrToCurr(edtvlmensal.Text);

if edtprevisao.Text = ' / / ' then
begin
DM.Qlocacao.ParamByName('parVENCIMENTO_MENSAL').AsDate := Null;
end;

if edtprevisao.Text >= Datelocacao.Text then
begin
DM.Qlocacao.ParamByName('parVENCIMENTO_MENSAL').AsDate := StrToDate(edtprevisao.Text);
end;


DM.Qlocacao.ExecSQL;
DM.IBTransaction.CommitRetaining;

Panel1.Enabled := False;
GroupBox1.Enabled := True;
edtcodpro.SetFocus;

BitBtn1.Enabled := true;
SpeedButton6.Enabled := true;
SpeedButton7.Enabled := true;
SpeedButton5.Enabled := false;
SpeedButton4.Enabled := false;

edtcheck.Text :='';
end;

end;
GOSTEI 0
Eucatur Ltda.

Eucatur Ltda.

26/07/2013

DM.Qlocacao.ExecSQL;
DM.IBTransaction.CommitRetaining;

DM.Qlocacao.Close;
DM.Qlocacao.SQL.Clear;
DM.Qlocacao.SQL.Add('select * from locacao');
DM.Qlocacao.Open;

DM.Qlocacao.Last;

cod_locacao.Text := DM.Qlocacao.fieldbyname('cod_loc').AsString;


Amigão, imagina se na sua tabela Locacao tiver uns 2.000 cadastros... Toda vez vc abre todos e da um Last??? Ultimo!

Vou te dar uma dica...

//Crie uma função genéria no DM...
function GeradorID(const Generator: string): Integer;

//Abaixo o código
function TDM.GeradorID(const Generator: string): Integer;
var
  Q: TSQLDataSet;
begin
  Q := TSQLDataSet.Create(nil);
  try
    Q.SQLConnection := Conexao;
    Q.CommandText := 'SELECT GEN_ID(' + Generator + ',1) FROM RDB$DATABASE';
    Q.Open;

    Result := Q.Fields[0].AsInteger;
  finally
    FreeAndNil(Q);
  end;
end;


Como você poderá usar????

No seu
DM.Qlocacao.SQL.Add(INSERT INTO locacao ('cod_loc',..... e o restante... VALUES (:cod_loc, .....


Crie uma variável ai
var
  ID_Gerado: integer;
begin
//codigo antes...
ID_Gerado := DM.GeradorID('LOCACAO');
Adicionar novo parâmetro DM.Qlocacao.ParamByName('cod_loc').AsInteger := ID_Gerado;


Para finalizar....

Troque
DM.Qlocacao.Close;
DM.Qlocacao.SQL.Clear;
DM.Qlocacao.SQL.Add('select * from locacao');
DM.Qlocacao.Open;


Para
DM.Qlocacao.Close;
DM.Qlocacao.SQL.Clear;
DM.Qlocacao.SQL.Add('select * from locacao where cod_loc = :cod_loc');
DM.Qlocacao.ParamByName('cod_loc').AsInteger := ID_Gerado;
DM.Qlocacao.Open;


GOSTEI 0
Andre Ucella

Andre Ucella

26/07/2013

Fiz da forma que você me indicou, aparentemente agora ficou Ok, obrigado pela ajuda.
GOSTEI 0
Eucatur Ltda.

Eucatur Ltda.

26/07/2013

Fiz da forma que você me indicou, aparentemente agora ficou Ok, obrigado pela ajuda.


Que blz...

Precisando de algo estamos ae...

Abraços
GOSTEI 0
POSTAR