Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   For Select ..... ??? (https://www.clubdelphi.com/foros/showthread.php?t=19586)

jwmoreira 18-03-2005 18:30:23

For Select ..... ???
 
Hola a todos, espero me puedan ayudar con esto, tengo el siguiente procedimiento:

Código:

create procedure proc1
as
begin
for select codigo from table1 into :codigo
do
  insert into tabla2 ......;
suspend;
end


Todo bien hasta aqui, este procedimiento lee de una tabla n registros y los inserta en otra de acumulados, pero ahora quiero validar si ya existe en la tabla de acumulados, para esto le agrego un select y ahi viene el problema, solo lee un registro:

Código:

create procedure proc1
returns (a integer)
as
begin
for select codigo from table1 into :codigo
do
  select codigo from table2 into ....;
  if ( codigo2 is null ) then
          insert into table2 ......;
  else
          update table 2 set ....;
suspend;
end


Ayuda por favor, que tengo mal.

Saludos,
Jorge.
end

StartKill 21-03-2005 18:24:36

Wnas,

Revisale esta lineas y ajustale a tu necesidad: :cool:

Código SQL [-]
CREATE PROCEDURE AA01 
AS
DECLARE VARIABLE XCODIGO CHAR(3);
DECLARE VARIABLE XNUMBER INTEGER;
begin
  for select fcodigo, fnumber from aa1 into :xcodigo, :xnumber do begin
    insert into aa2 (fcodigo, fnumber) values (:XCODIGO, :xnumber);
  end
  suspend;
end

Código SQL [-]
CREATE PROCEDURE AA02 
AS
DECLARE VARIABLE XCODIGO CHAR(3);
DECLARE VARIABLE XCOD2 CHAR(3);
DECLARE VARIABLE XNUMBER INTEGER;
begin
  for select fcodigo, fnumber from aa1 into :xcodigo, :xnumber do begin
      select fcodigo from aa2 where fcodigo=:xcodigo into :xcod2;
      if (:xcod2 <>:xcodigo) then
          insert into aa2 (fcodigo, fnumber) values (:XCODIGO, :xnumber);
      else
          update aa2 set fnumber=:xnumber where fcodigo=:xcodigo;
  end
  suspend;
end

Your friend,

StartKill
Lima-Perú

jwmoreira 23-03-2005 19:50:08

Gracias Startkill, tenía algo parecido pero no funcionaba, asi que no me quedo otra que mostrar los resultados como campos de returns y pude comprobar u par de fallitas quedando de la siguiente manera:

Código SQL [-]
CREATE PROCEDURE AA02
AS
DECLARE VARIABLE XCODIGO CHAR(3);
DECLARE VARIABLE XCOD2 CHAR(3);
DECLARE VARIABLE XNUMBER INTEGER;
begin
  for select fcodigo, fnumber from aa1 into :xcodigo, :xnumber do begin
      xcod2='';
      select fcodigo from aa2 where fcodigo=:xcodigo into :xcod2;
      if (:xcod2 = '' ) then
          insert into aa2 (fcodigo, fnumber) values (:XCODIGO, :xnumber);
      else
          update aa2 set fnumber=:xnumber where fcodigo=:xcodigo;
      suspend;
  end
end
Cuando no encuentra te devuelve NULL y al preguntar:
Código SQL [-]
if(:xcod2<>:xcodigo) then
no entraba, y el suspend debe ir dentro del begin-end.

Saludos,

Jorge.

PD:Puedes ayudarme con el siguiente hilo:
http://www.clubdelphi.com/foros/showthread.php?t=19537


La franja horaria es GMT +2. Ahora son las 05:41:10.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi