CF QoQ is throwing runtime error. "Column reference is not a column in any of the tables of the FROM table list." - coldfusion

In my code, I first create the Query Object:
<cfset memberData = QueryNew('slug,pos,firstname,lastname,email') />
<cfset temp = QueryAddRow(memberData, #numMembers#) />
<!--- LOOP POPULATES QUERY OBJECT --->
<cfloop...</cfloop>
I can then verify that it has been populated by running the following (which outputs as expected):
<cfoutput query="memberData">
#slug# - #pos#<br>
</cfoutput>
I then try to query the memberData Query Object and all hell breaks loose. If I run:
<cfquery name="members" dbtype="query">
SELECT slug,pos,firstname,lastname
FROM memberData
WHERE slug = #slug#
</cfquery>
I get this error:
Query Of Queries runtime error.
The select column reference [university] is not a column in any of the tables of the FROM table list.
In the output test mentioned above, I can verify that "university" is one of the values in the slug column. Clearly I'm missing something in my approach, but I'm baffled as to what it might be. Any help would be greatly appreciated!

Query Of Queries runtime error.
The select column reference
[university] is not a column in any of the tables of the FROM table
list
Your error was caused by absence of quotes in where clause and nothing else:
This expression find rows where the value in slug column equals the value in CF slug variable (String):
WHERE slug = '#slug#'
On the other hand this expression means find rows where value in the slug column equals the value contained in the column named in the CF slug variable (String):
WHERE slug = #slug#
The most likely cause of why you needed to change to SELECT * is CF query caching. So changing it back now should solve the problem. And always use <cfqueryparam .../> as suggested by "Al Everett"

Well, it's not quite answering the question asked, but it's close enough for what I needed:
<cfquery name="members" dbtype="query">
SELECT *
FROM memberData
WHERE slug = '#slug#'
</cfquery>
I had tried the wrapping #slug# in single quotes prior to posting with no success, but doing that plus changing the query to SELECT * fixed the issue. For my content, * only adds one more value retrieved, so not really a problem in slowing down the process.

Related

How can I get particular Row in a query variable using ColdFusion?

Take the following query example:
<cfquery name="Test" Datasource = "TestDB">
Select * from Table_Test
</cfquery>
Assume that the "Test" query returns 10 rows. I want to show single row on current time.
Note: I do not want to change the SQL statement.
If you know your row number, Test.columnName[RowNumber] will show you the value of the columnName in specified row number.
If you want one random row from the query:
<cfset start = randRange(1, Test.recordCount)>
<cfoutput>
#Test.name[start]# #Test.email[start]#<br>
</cfoutput>
No need to loop.
NOTE: It is more efficient to modify the query to get a random row.
How to request a random row in SQL?

PreparedStatement.setNString(int,java.lang.String) error trying to use Query of Queries

I have a simple Query of Queries statement like this:
<cfquery name="rsProductTypeID" dbtype="query">
SELECT
ProductTypeID
FROM
rsProductTypes
WHERE
ProductType = <cfqueryparam value="Computers" cfsqltype="cf_sql_nvarchar"/>;
</cfquery>
All it is doing is asking for the primary key value (ProductTypeID) from the recordset rsProductType depending on which ProductType we are asking for.
For some reason the above query gives me this error:
PreparedStatement.setNString(int,java.lang.String)
But if I change the Query of Queries and remove the <cfqueryparam> part and replace it with normal text, then it works fine. Here is the working code:
<cfquery name="rsProductTypeID" dbtype="query">
SELECT
ProductTypeID
FROM
rsProductTypes
WHERE
ProductType = 'Computers';
</cfquery>
I can't tell what the difference is between the code that is causing it to fail when using <cfqueryparam> and not.
I think I figured it out... when ColdFusion returns a recordset, it doesn't maintain the original datatype from the database. In my database ProductType is defined as nvarchar(50) but ColdFusion returns it as varchar format. So the cfsqltype attribute was causing the error. I changed the cfsqltype attribute to cf_sql_varchar and it works fine. Very annoying but what can you do.
I posted it as a self-answer in case anyone else gets this error. Not sure if that's against the rules or not.
cf_sql_nvarchar is not a valid value for the cfsqltype attribute of cfqueryparam
Docs: https://wikidocs.adobe.com/wiki/display/coldfusionen/cfqueryparam

Getting the Value of the Query to Populate Form Fields

I had a Query Where i am Getting 14 records and i need to Populate this records in 4 form fields, they are coming as
select id, name, value from mytable
check the attached Screenshot.
I cannot do cfoutput query as it will loop the form fields 14 times,
i tried something like
queryname.name[currentow], but it not worked, checked this URL of cflib http://cflib.org/udf/queryGetRow
but that did not seems to get work. need to populate the value of the name column in the form field
need to know what is the right way
From this comment only,
"honestl my friend, i think i got confused what to ask; I will try to explain again: Auume i have two columns in my table {name & Value}. It has four rows. Now i have to populate the form fields of the rows 1 to 4, which contains the the details from the value column of the table"
I would do this to build my form:
<cfquery name="data" datasource="whatever">
select name, value
from mytable
</cfquery>
<form action="somewhere.cfm" method="post">
<cfoutput query="data">
<input name="name_#name# value="#value#">
</cfoutput>
</form>
Processing the form is outside the scope of the question so I won't do it. However, I will mention some potential problems. If name is a text field, and it's values contain spaces, puncuation marks, etc, your page will crash due to an invalid variable name. If your table has an id field that is either uuid or numeric, that's a better field to use.
Ok, I did it, Here is if someone encounters this, i may not be able to correctly tell what is required. So here is my Solution:
<cfset rsSettings= initial.getSettings()>
<cfloop array="#rsSettings#" index="k">
<cfset settings[k['name']] = k['value']>
</cfloop>
My Query is returning me an array, Using ben's post
http://www.bennadel.com/blog/124-Ask-Ben-Converting-a-Query-to-an-Array.htm
so to fill the form i used like this and it worked
<input type="text" name="pri" class="text" value="<cfoutput>#settings['pri']#</cfoutput>" />
if you guys have better solution, please share

CFDump of query column does not display all values

The dumping results for the following QoQ are perfectly fine:
<cfquery datasource = "XX.XX.X.XX" name="master2">
SELECT DATE(Timedetail) as FIRSTCONN
, count(Timedetail) as FIRSTOccurances
, EVENTS
FROM MyDatabase
WHERE EVENTS = "FIRST" GROUP BY FIRSTCONN
<!--- LIMIT 0,10 --->;
</cfquery>
<cfdump var="#master2#">
<cfquery dbtype="query" name="detail2">
SELECT *
FROM master2
WHERE FIRSTCONN >= <cfqueryparam value="#startdate#" cfsqltype="cf_sql_date">
AND FIRSTCONN < <cfqueryparam value="#enddate#" cfsqltype="cf_sql_date">;
</cfquery>
Dumping Result: <cfdump var="#detail2#"><cfabort>
However, when I try to use the following check on the QoQ:
Dumping Result: <cfdump var="#detail2.FIRSTCONN#"><cfabort>
I don't see the full list of FIRSTCONN values. Instead I just see one line:
Dumping Result: {ts '2013-06-29 00:00:00'}
Ideally I should see the list of all the FIRSTCONN in my browser, shouldn't I?
You are looking at the default behaviour of coldfusion. When you output or dump queryname.fieldname, and don't specify a row number, you get the value from the first row. If you want to see all the rows, your choices are:
Look at the value list
Output/dump the entire query
Do another q of q for just that column and cfdump it.
Use cfoutput with a query attribute and just output that field
If you are looking to produce the same structured output that cfdump produces when dumping a query, I have two suggestions:
My First Inclination:
<cfdump var="#ListToArray(ValueList(queryName.columnName))#" />
That one is obviously a, very, minor continuation on Dan's suggestion.
The second is available for CF 8+ and it is
Exactly What You Wanted:
<cfdump var="#queryName#" show="columnName"/>
You may specify either columns to display in the output via the show attributes or you can hide specific columns by assigning a value to the hide attribute.
CFAbort in CF Docs
Granted, this post is almost 18 months old but maybe this will help someone that stumbles onto this page.
This is a little off topic, but I'd like to point out that in my instance of CF2016 the cfdump function suppresses the output of columns (and makes them appear empty) that have lots of text in them (or have the option to have lots of text). I'm not sure if it's the nVarChar(max) setting of the table field or what, but in order to see the content of these big fields, I actually have to make a separate query that selects only this one field, and use a separate cfdump in order to see its contents. This is only for debugging purposes, but it will keep you from going crazy and rewriting your update and insert statements over and over (because they appear to not be working all the way)...

Is there a way to escape and use ColdFusion query reserved words as column names in a query of query?

I'm working with a query that has a column named "Date."
The original query returns okay from the database. You can output the original query, paginate the original query, get a ValueList of the Date column, etc.
Query of Query
<cfquery name= "Query" dbtype= "query">
select
[Query].[Date]
from [Query]
</cfquery>
Response from ColdFusion
Query Of Queries syntax error. Encountered "Date. Incorrect Select
List,
Typically, I use descriptive names so I haven't run across this issue previously.
In this case, I'm working with a stored procedure that someone else wrote. I ended up modifying the stored procedure to use a more descriptive column name.
I have a service I use for transforming, searching and sorting queries with ColdFusion. I'm curious to know the answer to my original question, so that I can modify my service to either throw a better error or handle reserved words.
Is there a way to escape and use ColdFusion query reserved words as column names in a query of query?
The following code works fine for me:
<cfset query = queryNew("date")>
<cfdump var="#query#">
<cfquery name= "Query" dbtype= "query">
select
[Query].[Date]
from [Query]
</cfquery>
<cfdump var="#query#">
In standard mysql you'd "escape" the fields by using the ` character.
So for example:
select `query`.`date` from `query`
Try that and see if it works?