Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi Filtering two IBTable and insert or edit IBTable (https://www.delphipraxis.net/179341-filtering-two-ibtable-insert-edit-ibtable.html)

danten 28. Feb 2014 09:47

Datenbank: Firebird • Version: 2.3 • Zugriff über: 1

Filtering two IBTable and insert or edit IBTable
 
I need to recursively insert data from one table to another table by index (USERID =>> ID) common to both tables.
My function does not complete the operation.
Delphi-Quellcode:
function TFrm_main.Add_UserName:boolean;
var
  i:Integer;
begin
  ds_users.DataSet.First;
  for I := 0 to ds_users.DataSet.RecordCount -1 do
  begin
    Application.ProcessMessages;
    ds_attendant.DataSet.Filtered := False;
    ds_attendant.DataSet.Filter := 'USERID = '+QuotedStr(ds_users.DataSet.FieldByName('ID').Value);
    ds_attendant.DataSet.Filtered := True;
    Application.ProcessMessages;
  while not ds_attendant.DataSet.Eof do
  begin
  if (ds_attendant.DataSet.RecordCount > 0) then
  begin
    Application.ProcessMessages;
    ds_attendant.DataSet.Edit;
    ds_attendant.DataSet.FieldByName('NAME').Value := ds_users.DataSet.FieldByName('USERNAME').Value;
    ds_attendant.DataSet.Post;
    Application.ProcessMessages;
  end else
  begin
    ds_users.DataSet.Next;;
  end;
  end;
    ds_users.DataSet.Next;
  end;
  Result := True;
  ds_attendant.DataSet.Filtered := False;
end;

DeddyH 28. Feb 2014 10:06

AW: Filtering two IBTable and insert or edit IBTable
 
This looks a little weird. Are you sure you navigate through both datasets simultaniously? Why not use SQL? Untested:
SQL-Code:
UPDATE
  ATTENDANT A
SET
  NAME = (
    SELECT
      USERNAME
    FROM
      USERS
    WHERE
      USERID = A.ID)

danten 28. Feb 2014 11:17

AW: Filtering two IBTable and insert or edit IBTable
 
Work, thanks :)
update attendant a set name = (select username from users where id = a.userid)

danten 2. Mär 2014 16:36

AW: Filtering two IBTable and insert or edit IBTable
 
Hey, they face a new problem of filtering.
I still have the error.
Use components IBTable and join the Firebird database.
I need to filter by date.
My code:
Delphi-Quellcode:
tab_attendant.Filter := 'select * from ATTENDANT where "WHEN" >= '+QuotedStr('17.2.2014 00:00:00')+' and "WHEN" < '+QuotedStr('20.2.2014 00:00:00');

mkinzler 2. Mär 2014 16:41

AW: Filtering two IBTable and insert or edit IBTable
 
Why an IBTable?
The Filter attribute don't asks for a SQL statement but only the filter condition

danten 2. Mär 2014 17:01

AW: Filtering two IBTable and insert or edit IBTable
 
OK, does not work example:
tab_attendant.Filter := 'WHEN >= '+QuotedStr('17.2.2014 00:00:00')+' and WHEN < '+QuotedStr('20.2.2014 00:00:00');

mkinzler 2. Mär 2014 17:02

AW: Filtering two IBTable and insert or edit IBTable
 
Is when a fieldname?
Is it part of the join?

danten 2. Mär 2014 17:07

AW: Filtering two IBTable and insert or edit IBTable
 
"WHEN" is FieldName.

DeddyH 2. Mär 2014 17:11

AW: Filtering two IBTable and insert or edit IBTable
 
I personally prefer using queries rather than tables. Using a statement like:
SQL-Code:
SELECT
  *   
FROM
  ATTENDANT
WHERE
  "WHEN" BETWEEN :start AND :end
and filling the parameters as follows:
Delphi-Quellcode:
Query.ParamByName('start').Value := EncodeDate(2014, 2 ,17);
Query.ParamByName('end').Value := EncodeDate(2014, 2, 20);
Query.Open;
should return the expected result.

mkinzler 2. Mär 2014 17:15

AW: Filtering two IBTable and insert or edit IBTable
 
To make it more flexible ( no filter) you could extend the when condition:

SQL-Code:
WHERE
( :start is null) or ("WHEN" BETWEEN :start AND :end);

danten 2. Mär 2014 17:45

AW: Filtering two IBTable and insert or edit IBTable
 
OK, ERROR:
"Unsupported Feature"
Delphi-Quellcode:
Query.Params[0].Name := 'start';
Query.Params[0].DataType := ftTimeStamp;
Query.Params[0].ParamType := ptUnknown;
Query.Params[0].Value := EncodeDate(2014, 2 ,17);
Query.Params[1].Name := 'end';
Query.Params[1].DataType := ftTimeStamp;
Query.Params[1].ParamType := ptUnknown;
Query.Params[1].Value := EncodeDate(2014, 2 ,20);

DeddyH 2. Mär 2014 17:48

AW: Filtering two IBTable and insert or edit IBTable
 
Did you try my (much shorter) code? What is the datatype of "WHEN"?

mkinzler 2. Mär 2014 18:07

AW: Filtering two IBTable and insert or edit IBTable
 
I would change the name when to a not reserved name. The paramters should be created automatically, when ParamCheck is true.

danten 3. Mär 2014 16:21

AW: Filtering two IBTable and insert or edit IBTable
 
Zitat:

Zitat von DeddyH (Beitrag 1250289)
Did you try my (much shorter) code? What is the datatype of "WHEN"?

"WHEN" =>> ftDateTime
Delphi-Quellcode:
SELECT
  * 
FROM
  ATTENDANT
WHERE
  "WHEN" BETWEEN :start1 AND :end1

New Error:
"Dynamic SQL Error.
SQL error code=-206
Column unknown
START1
At line 1, column 48."

sx2008 3. Mär 2014 19:04

AW: Filtering two IBTable and insert or edit IBTable
 
Zitat:

Zitat von mkinzler (Beitrag 1250295)
I would change the name when to a not reserved name.

WHEN is a reserved word.
Using reserved words in SQL could lead to VERY STRANGE errors.
I (and mkinzler) recommend to change the name of the field.

PS: don't set ParamType to ptUnknown! Set it to ptInput.

danten 5. Mär 2014 07:20

AW: Filtering two IBTable and insert or edit IBTable
 
:-D Thank you to all friends 8-)
Delphi-Quellcode:
function Tfrm_main.Attendant_Filter:boolean;
var
  myYear,myMonth,myDay:word;
  set_day, my_day,
  set_week,start_week,end_week,
  set_month,start_month,end_month,
  day_month,
  set_year, start_year,end_year:string;
begin

  DecodeDate(NOW, myYear, myMonth, myDay);

  ///////////////////////////////////////////////////////////

    (* Filter day *)
  my_day := DateToStr(NOW);
  set_day := '("WHEN" >= ' + QuotedStr(my_day) + ')';

  (* Filter week *)
  start_week := IntToStr(dayofweek(now -1))+'.'+ IntToStr(myMonth)+'.'+IntToStr(myYear);
  end_week := IntToStr(dayofweek(now -1)+7)+'.'+ IntToStr(myMonth)+'.'+IntToStr(myYear);

  set_week := '("WHEN" >= ' + QuotedStr(start_week)
              + ') and ("WHEN" <= ' + QuotedStr(end_week)
              + ')';

  (* Filter month *)
  start_month := '1.'+ IntToStr(myMonth)+ '.' + IntToStr(myYear);
//  day_month := IntToStr(MonthDays[IsLeapYear(myYear)][2]); //
  end_month := '1.' + IntToStr(myMonth + 1) + '.' + IntToStr(myYear);

  set_month := '("WHEN" >= ' + QuotedStr(start_month)
              + ') and ("WHEN" <= ' + QuotedStr(end_month)
              + ')';

  (* Filter year*)
  start_year := '1.1.'+ IntToStr(myYear);
  end_year := '1.1.'+ IntToStr(myYear +1);

  set_year := '("WHEN" >= ' + QuotedStr(start_year)
              + ') and ("WHEN" <= ' + QuotedStr(end_year)
              + ')';

  ///////////////////////////////////////////////////////////////

  ShowMessage(QuotedStr(start_year) + ' : '+ QuotedStr(end_year));
  ShowMessage(QuotedStr(start_month) + ' : '+ QuotedStr(end_month));
  ShowMessage(QuotedStr(start_week) + ' : '+ QuotedStr(end_week));
  ShowMessage(QuotedStr(my_day));
  ........
  Result := True;

end;

DeddyH 5. Mär 2014 07:24

AW: Filtering two IBTable and insert or edit IBTable
 
Now set ShotDateFormat to e.g. "MM/DD/YYYY" and try it again.

danten 5. Mär 2014 15:06

AW: Filtering two IBTable and insert or edit IBTable
 
Thank you perfect. :)

Last question: How do I know the days last week.?
start_old_week := ????? return = 24.2.2014 ?????
end_old_week := ????? return = 2.3.2014 ?????

DeddyH 5. Mär 2014 15:09

AW: Filtering two IBTable and insert or edit IBTable
 
I don' t know what you mean exactly :gruebel:. You might have a look at the "DateUtils"-Unit, maybe there' s a function for your purpose.

danten 5. Mär 2014 15:27

AW: Filtering two IBTable and insert or edit IBTable
 
Find out last week, when it started and when it ended. Return date.

DeddyH 5. Mär 2014 15:34

AW: Filtering two IBTable and insert or edit IBTable
 
As I said, have a look at Delphi-Referenz durchsuchenDateUtils. Possibly StartOfTheWeek and EndOfTheWeek are the functions you are looking for.

danten 5. Mär 2014 16:18

AW: Filtering two IBTable and insert or edit IBTable
 
OK.
Delphi-Quellcode:
var
  sod: TDateTime;
  set_week,start_week,end_week, set_old_week, start_old_week, end_old_week,sec: string;
  begin
(* Filter week *)
  start_week := IntToStr(dayofweek(now -1))+'.'+ IntToStr(myMonth)+'.'+IntToStr(myYear);
  end_week := IntToStr(dayofweek(now -1)+7)+'.'+ IntToStr(myMonth)+'.'+IntToStr(myYear);

  set_week := '("WHEN" >= ' + QuotedStr(start_week)
              + ') and ("WHEN" <= ' + QuotedStr(end_week)
              + ')';

  (* Filter old week *)
  sod := StrToDate(start_week) -7;
  sec := DateToStr(sod);

  start_old_week := sec;
  end_old_week := start_week;

  set_old_week := '("WHEN" >= ' + QuotedStr(start_old_week)
              + ') and ("WHEN" <= ' + QuotedStr(end_old_week)
              + ')';
.................


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:24 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz