Solved

Columns W Spaces in QueryAddColumn?

Posted on 2004-08-09
4
541 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
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 250 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Two nodes for updates and forwarding 8 51
Website being blocked? 3 122
Windows Tool to Build Android and iOS App 3 54
Why is my Splunk Web URL not working? 2 52
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
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 make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now