Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Articles
Recherche: 

SQL Commands
Hilmar Zonneveld, April 1, 2002
SQL is a standard language used to manipulate databases. Several of the SQL commands are integrated into the Visual FoxPro language. Select This is a very flexible command, used to select data from a table, or from several tables. This command has options to get totals from several record...
SQL is a standard language used to manipulate databases. Several of the SQL commands are integrated into the Visual FoxPro language.

Select

This is a very flexible command, used to select data from a table, or from several tables. This command has options to get totals from several records into a single record, to sort the results, and several more. It is frequently used as a first step to obtain a report.

The result goes to a "cursor", which, in this case, means a temporary table. As soon as the cursos is closed, it is deleted automatically.

Both the act of selecting data, and the result, are called query.

Data from a single table

This is the simplest case.

SELECT <field_list> FROM <table> WHERE <condition>

An asterisk can be used for the field list; this will select all fields.

SELECT Client_Code, Names, CurrentDebt;
  FROM Client;
  WHERE CurrentDebt >= 1000;
  ORDER BY CurrentDebt DESCENDING;
  INTO CURSOR TempResult

Data from two or more tables

SELECT Invoice.InvoiceNumber, Invoice.Date, Invoice.InvoiceTotal,Invoice.ClientCode, Client.Names;
  FROM Invoice LEFT JOIN Client ON Invoice.ClientCode = Client.ClientCode;
  INTO CURSOR TempResult

The LEFT clause specifies that you want to include all data from the table on the left side, in this case, all invoices, even those that have no related data in the other table (invoices without a client).

To obtain results based on more than two tables, you can specify up to ten tables in a single SELECT command; however, in actual practice, I found that Visual FoxPro is much faster if you separate this into several SELECT commands.

Get totals of several records

To get totals for several records (join several records into a single one), you use the GROUP BY clause.

SELECT ClientCode, sum(InvoiceTotal) as ClientTotal;
  FROM Invoice;
  GROUP BY ClientCode;
  INTO CURSOR TempResult

Subqueries

To include a query (a SELECT command) within another one is a powerful tool that makes it possible, for instance, to see what records in a table exist, or don't exist, in another table.

The following table searches for invoices with invalid client codes (that is, codes that don't exist in the client table):

SELECT * FROM Invoice;
  WHERE ClientCode NOT IN (SELECT ClientCode FROM Client)

UNION

The UNION clause makes it possible to join the result of two queries vertically. The main difficulty, in practice, is that the result of both individual queries has to have exactly the same structure. The name of the fields doesn't matter, but each field of the query has to have the same type and the same size as the corresponding field in the other query.

DELETE

Visual FoxPro supports the DELETE command from the SQL language, used frequently to delete records that fulfil certain conditions.

Warning: If you don't specify a condition, all records from the table will be deleted.

DELETE FROM Invoice;
  WHERE InvoiceDate < {^1995-01-01}

Insert

The INSERT command of the SQL language is used to add a record. It is said to be faster that the alternative that doesn't use SQL syntax (that is, use the APPEND BLANK command, followed by REPLACE).

INSERT INTO Client (ClientCode, Names) VALUES (1, "John Doe")

UPDATE

The UPDATE command of the SQL language is used to modify existing records.

Warning: If you don't specify a condition, all records in the table will be modified.

Example (increase the price of all articles by 10%):

UPDATE Product SET CurrentPrice = CurrentPrice * 1.1

Data Definition

The aforementioned SQL commands are used to modify data in existing tables, therefore, they are part of the "data manipulation language". There is also a "data definition language" in standard SQL.

The CREATE TABLE command is used to create a new table.

CREATE TABLE Client (NumClient I, Names C(30), Current Debt Y)

The ALTER TABLE command is used to modify the definition of an existing table, for instance, add or delete columns (fields).

ALTER TABLE Client ALTER COLUMN Names C(35)

The DROP TABLE command is used to erase a table.

Views

A view is an updatable query: you select data from a table to a temporary table, do some changes, and then send the results back to the original table.

Views are defined with the view definer, in the database. You can also define views through commands, but for most requirements, the interactive view designer is satisfactory.

Once you define a view, you open it like any table, with the USE command. And just like any table, changes are updated with TableUpdate() and cancelled with TableRevert().

You can define parameters in a view, to get a subset of the data. Paramters are usually defined with a question mark, "?", in front of the variable name.

Views can solve certain speed problems: a grid is a component that you place on a form, that lets you see several records at a time. If you combine a grid with a filter, to see a small subset of a large table (for instance, the details for a a single invoice), things will get quite slow. Using a parameterized view as a data source for the grid solves these speed problems.

Views can be local (to access DBF tables), or remote (to access a database server).

Hilmar Zonneveld, Independent Consultant
Hilmar Zonneveld works in programming since 1986, using dBASE, FoxPro and Visual FoxPro. He is available as an independent consultant. He currently works as a programmer at Bata Shoe Organization; also as an instructor at Cisco Networking Academy. You can contact him through the Universal Thread, or, via e-mail, at hilmarz@yahoo.com. Personal Web page (mainly in Spanish): www.geocities.com/hilmarz.
More articles from this author
Hilmar Zonneveld, May 1, 2003
An audit-trail is a record of who did what changes, and when. In Visual FoxPro, this can easily be accomplished through triggers. I hinted at the possibility of doing an audit-trail, in my article on triggers - now, as a reaction to questions in the Universal Thread, I want to present a sample...
Hilmar Zonneveld, December 6, 2001
(The latest update contains minor edits only.) Five easy and fun ways to get yourself into trouble with inheritance. A frequent source of problems in OOP is called "breaking inheritance". This document briefly describes what inheritance is, how it applies to properties and methods, and how it ...
Hilmar Zonneveld, July 1, 2002
Introduction Buffering is a feature in Visual FoxPro that allows us to give the user "undo" and "save" capabilities. In the old FoxPro 2.x days, programmers either didn't provide this capability, or edited memory variables, and copied information between these variables and the table fiel...
Hilmar Zonneveld, October 6, 2005
Due to a recent Windows security fix, users can no longer access a CHM file on a server. The table of contents appears, but the individual pages are replaced by error messages. Access to CHM files in specific folders can be explicitly allowed through special registry settings.
Hilmar Zonneveld, July 20, 2001
(The last update contains minor edits only.) The idea is to have several controls on a form controlled with an array. Thus, you can quickly go through all the controls on the form, managing the array. The sample code included will help you get started quickly. You can easily adapt it to manage...
Hilmar Zonneveld, September 1, 2002
With Automation, you can control all aspects of Excel, Word, or other programs that provide this feature, from Visual FoxPro. In this article, I will concentrate on Excel. Its purpose is to provide a starting point, especially for people new to automation. Introduction With automation, you bas...
Hilmar Zonneveld, March 1, 2003
Introduction One common task in programming is to keep track of what problems are pending. For this purpose, I use a "hierarchical to-do list": a list of items, each of which can have sub-items. All you need is Microsoft Word. Alternatives are available as freeware or shareware, but in t...
Hilmar Zonneveld, October 7, 2005
This is a step-by-step tutorial to show inheritance, specifically in Visual FoxPro forms, as a guidance for people who are not familiar with inheritance in general, or who don’t know how to implement it in Visual FoxPro. The basic idea of inheritance is that all your forms, or several of your for...
Hilmar Zonneveld, May 30, 2004
The code shows how to quickly obtain the greatest common factor, and the least common multiple. Both functions are used when manipulating fractions, among others. Several methods are possible; the method usually taught in school involves prime numbers, but this code will execute much faster (and it ...
Hilmar Zonneveld, November 1, 2006
A standard requirement in a production system, or in systems for cost calculation, is to add up all the raw materials for a number of finished articles, to get the total cost, or simply to purchase the materials. In this article, Hilmar outlines how to do this with multiple levels of intermediate ar...
Hilmar Zonneveld, August 1, 2002
Overview The purpose of this article is to give an overview of normalization. Basically, normalization refers to having an efficient table structure. I will not discuss the famous "first to fifth normal forms" - if you want that information, enough texts exist about it in other places (search sit...
Hilmar Zonneveld, November 8, 2001
The following function will open any document, with its default association (the same application that will be called when you double-click on the file, in Windows Explorer). Use it to open a text-file, a Word or Excel document, an image, etc., with an external application.
Hilmar Zonneveld, May 1, 2002
Introduction This document explains the meaning of primary key, foreign key and candidate index in Visual FoxPro. A discussion of natural and surrogate keys (keys visible, or not visible, to the end-user) is included, including the advantages of each approach, as well as different methods for o...
Hilmar Zonneveld, January 1, 2003
Continuing my series of introductory articles, this article presents an introduction of a simple yet powerful programming concept: recursion. Introduction "To understand recursion, you must first understand recursion." "To make yogurt, you need milk and yogurt." If you are not accustomed...
Hilmar Zonneveld, December 1, 2002
Introduction This article presents an introduction to coding shortcuts in Visual FoxPro - when to use them, and when not to. Notes on coding in general This article is about coding shortcuts; however, I should first emphasize that making the code as small as possible is usually not the number...
Hilmar Zonneveld, July 20, 2001
Rushmore Optimization can help make queries much faster. However, "Full Rushmore Optimization" is not always a desirable goal. "Partial Optimization" is sometimes much faster. It is often believed that to speed things up, you need to have as many indices as possible. This article explains that so...
Hilmar Zonneveld, June 7, 2002
If you need to check elapsed time with seconds() or a datetime value, this function allows you to display the elapsed time in a human-readable format, that is, hours:minutes:seconds, instead of the total number of seconds. Just pass a number of seconds as a parameter.
Hilmar Zonneveld, August 1, 2003
In this article, I will show several ways to manipulate text-files. Knowledge of these methods is often important to import and export specific formats. Some of the techniques can also be used to work with files of any content; however, this article will concentrate on text-files. When ...
Hilmar Zonneveld, June 1, 2002
The purpose of this article is to show how to use some aspects provided by the Visual FoxPro database engine, to control our data. Indices Perhaps most readers already know indices; anyway, I find it convenient to include a brief summary of the topic, since this is a requisite to understan...
Hilmar Zonneveld, November 1, 2002
A help file can be used either for interactive help, or as an online manual. In this article, I will give an overview over creating help files in the new help format (CHM), for Visual FoxPro 6 and later. This article is introductory and assumes no prior knowledge of the Help Compiler, or of HTML cod...
Hilmar Zonneveld, February 1, 2003
Introduction Any real-world application will sooner or later misbehave. It is important to be able to find those problems. Visual FoxPro's built-in debugger can help a lot to find out why your program doesn't work as you thought it would. Most of the material in this article applies to Visual...
Hilmar Zonneveld, May 1, 2006
This article is an introduction to VisioModeler. This is a free CASE tool, that can help you design your database, in the process sharing the information with the client in a visual, easy-to-understand, format.