ABC of SQL with TQuery


Sample code listed below demonstrate the basic's of SQl with TQuery and it is assumed that you have setup a TQuery, at least one(1) DBGrid and a DataSource to make all this happen.....


procedure TForm1.SpeedButton1Click(Sender: TObject);

begin

    Close;

end;


procedure TForm1.ChkTab (Sender: TObject);

begin

        { page control property's

         ActivePageIndex

         always start's at zero(0)

        }

    if pagecontrol1.ActivePageIndex = 0

        then

        StatusBar1.SimpleText := 'Select *

                from Customer.db';

end;


procedure TForm1.FormCreate (Sender: TObject);

begin

{       when set to true, allows one line

        text display for the status bar

}

        Statusbar1.SimplePanel := true;

        label10.Caption := 'select * from Orders where CustNo IN (1221, 1231, 1351, 1354)';

end;


procedure TForm1.SpeedButton2Click (Sender: TObject);

begin

        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select * from Orders order by SaleDate');

        datm1.Query2.Open;

        statusbar1.SimpleText := 'select * from Orders order by SaleDate';


{       1st line, closes the Query to accept

        SQL statement

}

{       2nd line, clear's the SQL property      }

{       3rd line, put the SQL statement into the SQL property }

{       4th line, execute the SQL query statement }

{       5th line, display text on Status Bar }

end;


procedure TForm1.SpeedButton3Click (Sender: TObject);

begin

        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select CustNo, OrderNo, ItemsTotal from '+ 'Orders where ItemsTotal > 20000 '+ 'or CustNo = 1351 order by CustNo');


        datm1.Query2.Open;

        statusbar1.SimpleText := 'select OrderNo, ItemsTotal from ' + ''Orders where ItemsTotal > 2000 '+  'or CustNo = 1352 Order By CustNo';

end;


procedure TForm1.SpeedButton4Click

(Sender: TObject);

begin

        {  Aggregators functions are used to summarize data for reporting and analysis purposes.

            these are as follows:


                COUNT, SUM, MIN, MAX and AVG

        }

       {  it is not recommended that more than one(1) of these functions be mix in one statement  }


        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select CustNo,count(OrderNo) as TotalOrders '' + 'from Orders Group by CustNo');


        datm1.Query2.Open;

        statusbar1.SimpleText := 'select CustNo,count(OrderNo) as TotalOrders  ' + 'from Orders Group by CustNo';

end;


procedure TForm1.SpeedButton5Click

(Sender: TObject);

begin

        { MultiTable Joins, is to join or combine two(2) or more valid tables based on a logical link on all  tables involved.


           The sample below's basis is

           c.CustNo = o.CustNO

        }

        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select c.Company, o.OrderNo ' +  'from customer c, orders o ' +  'where o.CustNo = c.CustNo ' +  'Order by c.Company');


        datm1.Query2.Open;

        statusbar1.SimpleText :=  '{multitable join) o.custno = c.custno'

end;


procedure TForm1.SpeedButton6Click

(Sender: TObject);

begin

        { Subqueries, or nested queries are now allowed in the BDE on local Paradox tables as well. sample below uses the SQL command DISTINCT to make sure

          that there are NO duplicate copy of 'company' field from 'Customer' table

        }

        { Exists, SQL command that evaluate values

          as TRUE else will abort the process

        }

        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select DISTINCT company from customer ' +  'where Exists (select * from Vendors  ' + 'where (customer.state = vendors.state))');


        datm1.Query2.Open;

        statusbar1.SimpleText :=  '(subqueries) where Exists clauses';

end;


procedure TForm1.SpeedButton7Click

(Sender: TObject);

begin

        { Any and All the ANY keyword is not directly supported by Delphi, but a comparable SQL substitution will do the trick. by using the

          keyword IN or NOT IN

        }

        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select * from Orders where ' +  'CustNo IN (1221, 1231, 1351, 1354)');


        datm1.Query2.Open;

        statusbar1.SimpleText := 'select * from Orders where ' +  'CustNo IN (1221, 1231, 1351, 1354)';

end;


procedure TForm1.SpeedButton8Click

(Sender: TObject);

begin

        { Extract one can get any DATE related parts  from the Extract function provided by the BDE and possible combination are as follows:


          extract(YEAR from 0.saledate) - is

                year value

          extract(MONTH from o.saledate) - is

                  value

          extract(DAY from o.saledate) - is

                day value

       

                extract(Keyword from source) as

                display column

        }

        datm1.Query2.Close;

        DatM1.Query2.SQL.Clear;

        datm1.Query2.sql.Add('select c.company, ' + 'extract(MONTH from o.saleDate) as SalesMonth, ' + 'extract(YEAR from o.saleDate) as SalesYear ' + 'from Customer c,

        Orders o where o.custno = c.custno '' + 'order by c.company');


        datm1.Query2.Open;

        statusbar1.SimpleText := 'getting date with the Extract function';

end;