?
Solved

Columns W Spaces in QueryAddColumn?

Posted on 2004-08-09
4
Medium Priority
?
592 Views
Last Modified: 2013-12-20
I currently have the following code, which generates an error:

<cfset qNew = QueryNew("Col1") />
<cfset myArray = ArrayNew(1) />
<cfset userInput = "Col 2" />
<cfset QueryAddColumn(qNew,userInput,myArray) />
<cfdump var="#qNew#" />

The error is:
The column name "Col 2" is invalid.  
Column names must be valid variable names. They must start with a letter and can only include letters, numbers, and underscores.  


In the code userInput is actually a parameter specified by the user and needs to support variables defined by the user which have spaces. ColdFusion supports field names that have spaces and returned from a database just fine. ColdFusion supports it when a query is returned from a database where the fieldnames have had Alias names with spaces applied to them.  Is there an easy way to 'inject' a variable name with a space into the query structure?
0
Comment
Question by:libel_vox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 21

Expert Comment

by:pinaldave
ID: 11757308
you can use regular sql
insert into tablename ( column name, name2) values(value1, value2)
it should do the work.
0
 
LVL 5

Accepted Solution

by:
Seth_Bienek earned 750 total points
ID: 11757394
Some databases allow spaces in column names, but ColdFusion does not allow spaces in variable names.  Once data comes back from a query, it is placed in a variable, so the variable rules apply as opposed to the database rules.

The fact that CF allows you to work with columns that have invalid characters (i.e. spaces) in them is a compatibility feature only, and doesn't stretch forward into the rest of the CFML language.  Good coding practice suggests that no variable (even database columns) should have spaces if it can be helped.

That being said, the solution to your problem is to add the column with a valid name (maybe an underscore in place of the space), and work with it that way.  If the data eventually needs to go back into the table, yuo CAN use spaces in queries, depending on your database (using pinaldave's suggestion above).

I hope this sheds some light on the issue and gets you headed in the right direction.  More questions, just ask.

Seth
0
 
LVL 2

Expert Comment

by:russmichaels
ID: 11757565
As seth says, you should replace the space with an underscore.
use the coldfusion Replace() function to do this.

Also you can have column name swith spaces in the database, it is not a good idea, just like using reserved words, you can do it though.
The method you have to us ein a query when select columns that are basically invalid is

select [columnName]

the [] allow you to implicity specify the name regardless of the spaces or the fact it may be a reserved word.

Regards

Russ Michaels
0
 

Author Comment

by:libel_vox
ID: 11764915
Most knowledgable, thoughtful answer. Points to Seth.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question