Skip to main content

SQL: Does a Field Exist?

In the FoxPro world, checking to see if a field exists can be done a number of ways:

=AFIELDS(la)
IF ASCAN(la,"FIELDNAME")>0

ENDIF

or

IF TYPE("FIELDNAME") = "U"

(I'm sure there are lots of other ways especially if you're already working with a data dictionary tool)

In T-SQL, there is no real equivalent however, you can do the same by using the INFORMATION_SCHEMA table.

While there is no shortage of sites on the web that will show you similar code (like this one), I wanted to post it here for any VFP devs who may be switching between DBFs and SQL.

IF NOT EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = 'MyTable'
AND COLUMN_NAME='MyColumn')
ALTER TABLE myTable ADD MyColumn decimal(10,2) NULL

Of course, this assumes that the table already exists but you can use similar approaches for tables as well.

IF NOT EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'myTable')
CREATE TABLE .....

if NOT EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'CFG_Fees'
AND COLUMN_NAME='minmFee')
ALTER TABLE .....


Comments

Walt Krzystek said…
Very good stuff. I use the INFORMATION_SCHEMA views quite a bit for validation in my business objects. One of the nice parts is they are available to just about all users without security restrictions (except for firing the ALTER TABLE command). Another VFP native function I use for similar tasks is SQLCOLUMNS in which if you pass "NATIVE" as the 3rd parameter, you get the table structure information returned. SQLTABLES is another function to validate table existence.
Andrew MacNeill said…
I agree about SQLCOLUMNS but that is specific to a SQL Server connection. My quest began when I was trying to find a column name just as easily as I do in VFP. In VFP, I could call SQLCOLUMNS without a problem - and I know there are a few stored procs in SQL that can do it - but I have found this approach works best.
Unknown said…
How about using something like this?

SELECT * FROM table_name WHERE .f. INTO CURSOR junk

IF TYPE("junk.field_name") = "U"
* -- alter table here
ENDIF
USE IN SELECT("junk")
Andrew MacNeill said…
Brian,

That would work in VFP - but not in raw SQL.

Andrew

Popular posts from this blog

Elevating Project Specifications with Three Insightful ChatGPT Prompts

For developers and testers, ChatGPT, the freely accessible tool from OpenAI, is game-changing. If you want to learn a new programming language, ask for samples or have it convert your existing code. This can be done in Visual Studio Code (using GitHub CoPilot) or directly in the ChatGPT app or web site.  If you’re a tester, ChatGPT can write a test spec or actual test code (if you use Jest or Cypress) based on existing code, copied and pasted into the input area. But ChatGPT can be of huge value for analysts (whether system or business) who need to validate their needs. There’s often a disconnect between developers and analysts. Analysts complain that developers don’t build what they asked for or ask too many questions. Developers complain that analysts haven’t thought of obvious things. In these situations, ChatGPT can be a great intermediary. At its worst, it forces you to think about and then discount obvious issues. At best, it clarifies the needs into documented requirements. ...

Blogs and RSS come to Microsoft.com

MS has just introduced their portal and it's pretty comprehensive. Nothing quite like learning that some people use AIM instead of MSN messenger, or that there really may be a need for supporting 4 monitors ( Cyrus Complains ) However, it's really a great sign that MS is serious about supporting the blogging community which seems to have um, exploded in size in the past year. Blogs and RSS come to Microsoft.com

I’m Supposed to Know

https://programmingzen.com/im-supposed-to-know/ Great post for developers who are struggling with unrealistic expectations of what they should know and what they shouldn't. Thirty-forty years ago, it was possible to know a lot about a certain environment - that environment was MS-DOS (for non Mac/UNIX systems). . There was pretty much only a handful of ways to get things going. Enter networking. That added a new wrinkle to how systems worked. Networks back then were finicky. One of my first jobs was working on a 3COM + LAN and it then migrated to LAN Manager. Enter Windows or the graphical user interface. The best depiction of the complexity Windows (OS/2, Windows NT, etc) introduced that I recall was by Charles Petzold (if memory serves) at a local user group meeting. He invited a bunch of people on the stage and then acted as the Windows "Colonel", a nice play on kernel. Each person had a role but to complete their job they always had to pass things back to h...