FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Test for a SQL Field name
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Test for a SQL Field name
Posted: Thu Jan 05, 2012 05:50 PM

To All

Is there a way to test a SQL table to find out if there is a specific field name .. I need to be able to Alter a table and add a column if the column name I am looking for does not exist ?

I have thought about opening a recordset and with a try catch test if a value exists ??

Try
cTest := oRs:Fields("FieldLookingFor"):Value
Catch
// failed .. so add the column with the Alter table command
End try

The above is not very elegant .. Any suggestions ?

Thanks
Rick Lipkin

Posts: 883
Joined: Thu Dec 24, 2009 12:46 AM
Re: Test for a SQL Field name
Posted: Thu Jan 05, 2012 05:53 PM
Try this... Found with google
http://snippets.dzone.com/posts/show/4663

=====>

Bayron Landaverry
xBasePHP.com
(215)2226600 Philadelphia,PA, USA
MayaBuilders@gMail.com
Guatemala

FWH25.06--Harbour 3.0.0--BCC7.7--UEstudio 10.10
Windows 10

FiveWin, One line of code and it's done...

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test for a SQL Field name
Posted: Thu Jan 05, 2012 06:38 PM
Bayron

Code (fw): Select all Collapse
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ‘TEST’ AND COLUMN_NAME = ‘TEST_DATE’)
BEGIN
   ALTER TABLE TEST ADD TEST_DATE DATETIME
END


Interesting .. I was trying to keep from having to Select * to open an entire table .. this is an MS Access table. There is not a schema involved .. just curious how I would adapt this .. prob use a oCn connection rather than a recordset

Let me play with this ..

Thanks
Rick Lipkin
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test for a SQL Field name
Posted: Thu Jan 05, 2012 08:19 PM

To All

Thanks to Enrico and the xHarbour forum .. this seems like the best solution :

> FOR i = 0 TO oRs:Fields:Count - 1
> ? oRs:Fields( i ):Name
> NEXT

I add that the above technique works fine at least using Access, MSSQL and
MySQL (personally tested).

EMG

--
EMAG Software Homepage: http://www.emagsoftware.it
The EMG's ZX-Spectrum Page: http://www.emagsoftware.it/spectrum
The Best of Spectrum Games: http://www.emagsoftware.it/tbosg
The EMG Music page: http://www.emagsoftware.it/emgmusic

Enrico

Thank you ... I have been thinking about this and it is in two
parts .. the Select statement and the field test .. Found this Sql
statement that will open the table which will always open a eof
recordset ...

Select * from table where 1 = 2

The above logic will open a recordset with no records .. then I can
use your logic to test for the Field names !!

Regards
Rick Lipkin

Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Re: Test for a SQL Field name
Posted: Fri Jan 06, 2012 07:03 PM
Rick;

At least with Advantage you can do this:

Code (fw): Select all Collapse
Select * from system.columns where parent = 'tablename'


That will return a cursor with all field information from table "tablename".

That's exactly what I'm using to keep table structures updated.


Reinaldo.

Continue the discussion