Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use various SQL / MS SQL Server Routines Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
24-Dec-02
Category
Database-SQL
Language
Delphi 2.x
Views
183
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Lou Adler 

This is a bit of a departure from my normal style of Q & A, but I thought it would 
be useful for those of you who do a lot of database-related stuff.

Answer:

General Purpose Stuff

Face it, code reuse is big issue irrespective of programming in an object-oriented 
environment. Especially, with database application programming, the stuff you do 
over and over again can be a bit tedious. So, I've created a bunch of general 
purpose functions that I use in my code - a lot.

An On-the-Fly SELECT Statement Generator

The first function CreateSelect creates a SELECT or SELECT DISTINCT statement that 
can be used in whenever you create a TQuery for querying against a local table like 
Paradox. It's most useful for when you want to create a query from a table at 
runtime, but don't know the fields you want to include. For instance, I created an 
adhoc querying tool where users could select the fields they wanted displayed in a 
graph by dragging the field names from on list box into another. From there, I 
passed the selected fields list to CreateSelect and constructed a SELECT statement 
on the fly. It was a one-line call as opposed to the multiple lines I'd normally 
have to write for a query. Here's the code:
1   
2   {==========================================================
3    This function will create a SELECT or SELECT DISTINCT SQL
4    statement given input from a TStrings descendant like a
5    list. It will properly format the list into field decla-
6    rations of a SQL statement then, using the supplied
7    TableNm parameter, will construct an entire statement that
8    can be assigned to the SQL property of a TQuery.
9   
10   Params:  Distinct  SELECT DISTINCT or regular SELECT
11            TableNm   Table name: Should either be a fully
12                      qualified table name, or preceeded by
13                      an alias (ie, ':DbName:MyTable.db')
14            FieldList Any TStrings descendant will work here,
15                      like the Items property of a TListBox.
16   ==========================================================}
17  
18  function CreateSelect(Distinct: Boolean;
19    TableNm: string;
20    const FieldList: TStrings)
21    : TStrings;
22  var
23    Sql: TStringList;
24    I: Integer;
25    buf,
26      QueryType: string;
27  begin
28    //First, instantiate the SQL lines list
29    Sql := TStringList.Create;
30  
31    //Determine whether or no this is a regular SELECT
32    //or a SELECT DISTINCT query.
33    if Distinct then
34      QueryType := 'SELECT '
35    else
36      QueryType := 'SELECT DISTINCT ';
37  
38    buf := QueryType;
39  
40    try
41      //Now add the fields to the select statement
42      //Notice that if we're on the last item,
43      //we don't want to add a trailing comma.
44      for I := 0 to (FieldList.Count - 1) do
45        if (I <> FieldList.Count - 1) then
46          buf := buf + FieldList[I] + ', '
47        else
48          buf := buf + FieldList[I];
49  
50      //Now, put the query together
51      Sql.Add(buf);
52      Sql.Add('FROM "' + TableNm + '"');
53      Result := Sql;
54    finally
55      Sql.Free;
56    end;
57  end;


Here's a code snippet from one of my programs that implements the function:

58  var
59    qry: TQuery;
60  begin
61    qry := TQuery.Create(nil);
62    with qry do
63    begin
64      Active := False;
65      DatabaseName := 'MyLocalDatabase';
66      SQL := CreateSelect(True, ExtractFilePath(ParamStr(0))
67        + 'Process.DB', lbSelectedFlds.Items);
68      try
69        Open;
70        ....other code....
71      finally
72        Free;
73      end;
74    end;
75  end;


WHERE It Is....

I use a lot of TStrings types in my code because they make it easy to pass a bunch 
of values at once. Especially with selection criteria in queries, having the 
capability to select on multiple values is a boon to efficiency. The three 
functions below can be added to a WHERE clause in a SQL statement for 
multiple-value searching. All you need to do is assemble the search criteria into a 
TStrings type like a TStringList or use even the Items property of a TListBox to be 
able to use these functions. Respectively, they allow you to create a basic 
multiple-value criteria statement (i.e. (fldName = 'thisvalue') OR (fldName = 
'thatvalue'); an IN selection criteria statement; and a multiple-value LIKE 
selection criteria statement. Here they are:

{Returns a '(FldName = FldValue) OR (FldName = FldValue)' etc string from
 a list of values. Useful for translating TListBox values into SQL strings
 IsValString is a boolean to test if the list values are a value string; that is,
 it contains spaces, in which case, you would want double-quotes.}
76  
77  function BuildSQLSetString(fldName: string; const List: TStrings;
78    IsValString: Boolean): string;
79  var
80    I: Integer;
81    buf: string;
82  begin
83    Result := '';
84    for I := 0 to (List.Count - 1) do
85    begin
86      if IsValString then
87        buf := '(' + fldName + ' = ''' + List[I] + ''') OR '
88      else
89        buf := '(' + fldName + ' = ' + List[I] + ') OR ';
90  
91      Result := Result + buf;
92    end;
93    Result := Copy(Result, 1, Length(Result) - 4);
94  end;
95  
96  //This will build an IN statement
97  
98  function BuildSQLINString(fldName: string; const List: TStrings;
99    IsValString: Boolean): string;
100 var
101   I: Integer;
102   buf: string;
103 begin
104   Result := '';
105   for I := 0 to (List.Count - 1) do
106     if IsValString then
107       buf := buf + '''' + List[I] + ''', '
108     else
109       buf := buf + List[I] + ', ';
110 
111   buf := Copy(buf, 1, Length(TrimRight(buf)) - 1);
112 
113   Result := fldName + ' IN (' + buf + ')';
114 end;
115 
116 //This will build a LIKE statement
117 
118 function BuildSQLLikeString(fldName: string; const List: TStrings;
119   IsValString: Boolean): string;
120 var
121   I: Integer;
122   buf: string;
123 begin
124   Result := '';
125   for I := 0 to (List.Count - 1) do
126   begin
127     if IsValString then
128       buf := '(' + fldName + ' LIKE ''' + TrimRight(List[I]) + '%'') OR '
129     else
130       buf := '(' + fldName + ' LIKE ' + List[I] + '%) OR ';
131 
132     Result := Result + buf;
133   end;
134   Result := Copy(Result, 1, Length(Result) - 4);
135 end;


Notice that in addition to the field name and value list, there's a parameter in 
each function called IsValString. Set this to true if the field you're searching on 
is a string field. Remember, discrete string values in SQL must be delimited by 
single quotes.

Some MS SQL Server Stuff

A little over a year ago, I moved completely away from doing local table processing 
with Paradox and started doing all my processing under a Client/Server environment 
using MS SQL Server. I was in for a big surprise when I made the switch because 
many of the things that I could do with Paradox tables that I took for granted, 
weren't as easily done in SQL Server. Yikes! So, what I did was create some generic 
routines specifically for Client/Server applications to make my life much easier. 
Let's see what I've got...

How Many Was That?

For those of you who work with local tables, you know how easy it is to get the 
number of records in a table: Simply connect a TTable object to the table in 
question and query the RecordCount property. Well, in SQL Server, that's not so 
easy because the idea of a "record" is non-existent in this set-based data 
environment. But, just because you can't get the information from the table 
directly, doesn't mean that it doesn't exist. In fact, the information is stored in 
the system tables. Here's a function that will get you the number of rows contained 
in a SQL Server table:
136 
137 // =============================================================================
138 // Returns the SQL DB path of the specified BDE alias.
139 // =============================================================================
140 
141 function GetSQLDBPath(const AliasName: ShortString): ShortString;
142 var
143   ParamList: TStrings; // List of parms from the BDE
144   Path: ShortString; // Path returned from the BDE
145 begin
146   Result := '';
147   ParamList := TStringList.Create;
148   try
149     Session.GetAliasParams(AliasName, ParamList);
150     Path := ParamList[0];
151     Result := Copy(Path, Pos('=', Path) + 1, Length(Path) - Pos('=', Path) + 1);
152   finally
153     ParamList.Free;
154   end;
155 end;
156 
157 function GetSQLTableRows(SesName: string; DB: TDatabase; TableName: string): 
158 Integer;
159 var
160   qry: TQuery;
161 begin
162   qry := TQuery.Create(nil);
163   with qry do
164   begin
165     Active := False;
166     SessionName := sesName;
167     DatabaseName := DB.DatabaseName;
168     SQL.Add('SELECT DISTINCT I.Rows');
169     SQL.Add('FROM ' + GetSQLDBPath(DB.DatabaseName) + '..SysIndexes AS I INNER 
170 JOIN'
171     SQL.Add('     ' + GetSQLDBPath(DB.DatabaseName)
172       + '..SysObjects AS O ON (I.ID = O.ID)');
173     SQL.Add('WHERE (O.Type = ''U'') AND (O.Name = ''' + TableName + ''')');
174     try
175       Open;
176       Result := FieldByName('Rows').AsInteger;
177     finally
178       Free;
179     end;
180   end;
181 end;


Some of you might be wondering: "Of what use is this function?" Well, for some of 
you, indeed, it might be of no use whatsoever. But in the type of applications I 
write that query datasets with records numbering in the several millions, in some 
steps, I only want to continue processing if my result sets aren't empty. This 
function gives me a quick way of checking if I need to continue or not. So there!

To Transact or Not To Transact (SQL, that is)

If you're using SQL Server, but not using Transact SQL, you're missing out on a lot 
of functionality. I know, I know, there are those cross-platform junkies out there 
that will only write the most generic code so they can deploy their DB apps on 
other servers. That's all well and good, but for the most of us, we only have a 
single server platform, and to not use its native functionality is to deprive 
ourselves of a lot of functionality. For instance, how many of you create temporary 
tables when doing a series of queries? C'mon, raise yer hands high! Using strict 
ANSI SQL with Delphi, you would have to create three objects: a TQuery to perform 
the query, a TBatchMove to move the result table, and a TTable to receive the 
results. Yikes! But with SQL Server, you can perform a SELECT INTO query use only a 
single object: a TQuery. Check out the code snippet from one of my programs below:

182 var
183   qry: TQuery;
184 begin
185   qry := TQuery.Create(nil);
186   with qry do
187   begin
188     Active := False;
189     SessionName := ClassSession.SessionName;
190     DatabaseName := DBName;
191     SQL.Add('SELECT DISTINCT T.PatientID, T.Symptom);
192       SQL.Add('mp1);
193       SQL.Add('FROM ' + SymptomTable + ' T (TABLOCK), HoldingTable H (TABLOCK)');
194       SQL.Add('WHERE (H.PatientID = T.PatientID) AND (Age > 65) ');
195       SQL.Add('AND (Thiazides_Now IS NULL) AND (GOUT IS NULL)');
196       try
197         ExecSQL;
198       finally
199         Free;
200       end;
201   end;
202   // ....the rest of the code....

This simple query above accomplishes with one object using Transact-SQL that it 
normally takes three objects to do with standard ANSI SQL. Notice that the query is 
executed using ExecSQL as opposed to Open. If you're using Transact-SQL, everything 
happens on the server, so you get no cursors back. That might present a problem in 
some circumstances, but for the most part, since you're doing nothing but set 
logic, having a cursor to a result set isn't necessary.

Not only does Transact-SQL make it shorter to do extracts, it has some cool syntax 
that allows you to really short-circuit both code and performance. For instance, 
one of the things that has always irked me about SQL is doing a two-table update; 
that is, updating a field in a table from values contained in another table. In 
Paradox, it's simple QBE query with matching example elements. In ANSI SQL, it 
involves a subquery. Let's look at an example:

Standard ANSI SQL two-table update:

UPDATE ClaimsTbl
SET History = (SELECT Activity
               FROM ActivityTbl AS A
               WHERE (ClaimsTbl.Patient = A.Patient))

Not too complex, but let me tell you, it's as slow as molasses on SQL Server. On 
the other hand, the following Transact-SQL UPDATE statement works lickety-split!

UPDATE ClaimsTbl
FROM ClaimsTbl C, Activity A
SET C.History = A.Activity
WHERE (C.Patient = A.Patient)

Not much different from the example above, but since SQL Server is equipped to deal 
with this syntax in an optimized fashion, it works much faster.

Why have I spent so much time covering this stuff? Well, I'm a real proponent of 
creating systems that run in the most optimized fashion I can make them. And that 
means that I use all the tools available to me. Yes, it can be argued that I'm 
locking myself into a specific platform's functionality, but since I only have one 
platform that I'm dealing with, why not use it? I realize that I haven't covered 
hardly any of the Transact-SQL syntax. If you've got it, I'll leave it up to you to 
go hunting for the information (BTW, a great source is the SQL Server Books Online 
reference that comes with the software). The bottom line is this: If you're 
building systems that address a single server platform, make the best use of that 
server's capabilities.

But Wait! There's More!

Below are some general purpose routines that I use on a regular basis for accessing 
my SQL Server databases. Check 'em out:
203 
204 //Uses Session.GetTableNames to get a list of tables from the specified database
205 
206 procedure GetTableList(DBName: string; const TableList: TStrings);
207 var
208   db: TDatabase;
209 begin
210   db := TDatabase.Create(Application);
211   with db do
212   begin
213     DatabaseName := DBName;
214     LoginPrompt := False;
215     Connected := True;
216   end; { with }
217   try
218     Session.GetTableNames(db.DatabaseName, '', False, False, TableList);
219     db.Close;
220   finally
221     db.Free;
222   end;
223 end;
224 
225 //Uses GetTableList to see if a particular table exists in database.
226 
227 function TableExists(var ProcSession: TSession; var DB: TDatabase;
228   TableName: string): Boolean;
229 var
230   TableList: TStrings;
231 begin
232   Result := False;
233   TableList := TStringList.Create;
234   try
235     ProcSession.GetTableNames(DB.DatabaseName, '', False, False, TableList);
236     if (TableList.IndexOf(TableName) > 0) then
237       Result := True;
238   finally
239     TableList.Free;
240   end;
241 end;
242 
243 //Performs a series of drops for all table names contained in the input array.
244 //Very useful for cleaning up a bunch of temporary tables at once.
245 
246 procedure CleanMSSQLTbl(var ProcSession: TSession; DBName: string;
247   TableNames: array of string); overload;
248 var
249   sqlEI: TQuery;
250   I: Integer;
251 begin
252   for I := Low(TableNames) to High(TableNames) do
253   begin
254     sqlEI := TQuery.Create(nil);
255     with sqlEI do
256     begin
257       SessionName := ProcSession.SessionName;
258       DatabaseName := DBName;
259       with SQL do
260       begin
261         Add('if exists (select * from sysobjects where ' +
262           'id = object_id(''' + TableNames[I] + ''') and sysstat & 0xf = 3)');
263         Add('drop table ' + TableNames[I]);
264         try
265           ExecSQL;
266         finally
267           Free;
268         end;
269       end;
270     end;
271   end;
272 end;
273 
274 //Will return a TStrings list containing the results of a query
275 
276 function SQLLoadList(SesName, TblName, FldName: string): TStrings;
277 var
278   qry: TQuery;
279 begin
280   Result := TStringList.Create;
281   qry := TQuery.Create(nil);
282   with qry do
283   begin
284     Active := False;
285     if (SesName <> '') then
286       SessionName := SesName;
287     DatabaseName := ExtractFilePath(TblName);
288     SQL.Add('SELECT DISTINCT ' + FldName);
289     SQL.Add('FROM "' + TblName + '"');
290     try
291       Open;
292       while not EOF do
293       begin
294         Result.Add(FieldByName(FldName).AsString);
295         Next;
296       end;
297     finally
298       Free;
299     end;
300   end;
301 end;


Notice in most of the functions above, that they either require a TSession or a 
TSession.SessionName as one of their formal parameters. This is because these 
functions are thread-safe. In order to use these in a multi- threaded system, you 
need to create a TSession instance for every thread, and database access within the 
scope of the thread require a session name to operate under. Otherwise you'll get 
access violations. Not good.

Well, that's it for now. I encourage you to use these functions and concepts in your own code. Believe me, they've saved me a lot of time!

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC