Solved

Columns W Spaces in QueryAddColumn?

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Configuring Apache Camel to send and recieive from QPID Broker 2 34
LAMP problem identifier tool ? 9 127
Webserver access problem 5 77
WebSite Direction 1 62
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
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 insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…

749 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