Using a wildcard in cfqueryparam - Coldfusion - sql

Having a bit of trouble inserting a LIKE and using a wildcard within my parameter in my query using coldfusion. Here is what currently works:
<cfquery name="sample" datasource="database"><!---Take the query here--->
SELECT * <!---select all--->
FROM table <!---from the table "table"
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(selectList2,i)#" /> <!---
search column name = query parameter
using the same index in both lists
(selectList1) (selectList2) --->
<cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but
the very last element of the list (in that
case we don't need an "AND"--->
</cfloop>
</cfquery>
My goal is to get the code somewhere where I can type in a parameter like so (but it is not working currently - Error reads cannot execute query)
<cfquery name="sample" datasource="database"><!---Take the query here--->
SELECT * <!---select all--->
FROM table <!---from the table "table"
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" /> <!---
search column name = query parameter
using the same index in both lists
(selectList1) (selectList2) --->
<cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but
the very last element of the list (in that
case we don't need an "AND"--->
</cfloop>
</cfquery>

Your syntax looks fine. I use the same kind of logic in some of my code running against Microsoft SQL Server.
Try this to debug your code and see the SQL that is being generated.
<!--- Comment out your query tag to debug <cfquery name="sample" datasource="database"> --->
<cfoutput> <!--- add a cfoutput tag to "see" your generated code --->
SELECT *
FROM table
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" />
<cfif i neq listLen(selectList1)>AND</cfif>
</cfloop>
</cfoutput> <!--- add a cfoutput tag to "see" your generated code --->
<!--- Comment out your query tag to debug </cfquery> --->
This will most likely break the rest of your code that is dependent on the query. I usually just stick a <cfabort> tag after the closing </cfoutput> tag to stop processing and just show me the SQL.
The purpose of this is to output the generated SQL code to your browser. Look it over and see if you can spot the error. You could also copy and paste the generated SQL into your query tool (query analyzer) and test it that way.

Related

Debugging Coldfusion Query with cfloop inside

I'm trying to debug a cf query and cannot do this because of his complex structure.The code is following:
<cfquery name="qQuery" datasource="#variables.datasource#">
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
<cfelseif IsStruct(aSQL[i])>
<cfset aSQL[i] = queryparam(argumentCollection=aSQL[i])>
<cfswitch expression="#aSQL[i].cfsqltype#">
<cfcase value="CF_SQL_BIT">
#getBooleanSqlValue(aSQL[i].value)#
</cfcase>
<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
#CreateODBCDateTime(aSQL[i].value)#
</cfcase>
<cfdefaultcase>
<!--- <cfif ListFindNoCase(variables.dectypes,aSQL[i].cfsqltype)>#Val(aSQL[i].value)#<cfelse> --->
<cfqueryparam value="#aSQL[i].value#" cfsqltype="#aSQL[i].cfsqltype#" maxlength="#aSQL[i].maxlength#" scale="#aSQL[i].scale#" null="#aSQL[i].null#" list="#aSQL[i].list#" separator="#aSQL[i].separator#">
<!--- </cfif> --->
</cfdefaultcase>
</cfswitch>
</cfif>
</cfloop>
</cfquery>
If I run <cfdump var="#qQuery#"> it's not working nor cfoutput, I get undefined qQuery error. How can I find what query is executing behind ? I don't want to use MS SQL profiler.
Thanks,
Take everything inside the query and wrap it in a cfsavecontent instead. Output that result.
If you place the cfsavecontent inside the cfquery tags, you don't even need to worry about the cfqueryparam tags barfing, although you do need to re-output that saved content inside the query. See http://coldflint.blogspot.com/2016/01/debugging-queries-dirty-way.html
Basically, you should have this:
<cfquery name="qQuery" datasource="#variables.datasource#">
<cfsavecontent variable="sqlContent">
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
<cfelseif IsStruct(aSQL[i])>
<cfset aSQL[i] = queryparam(argumentCollection=aSQL[i])>
<cfswitch expression="#aSQL[i].cfsqltype#">
<cfcase value="CF_SQL_BIT">
#getBooleanSqlValue(aSQL[i].value)#
</cfcase>
<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
#CreateODBCDateTime(aSQL[i].value)#
</cfcase>
<cfdefaultcase>
<!--- <cfif ListFindNoCase(variables.dectypes,aSQL[i].cfsqltype)>#Val(aSQL[i].value)#<cfelse> --->
<cfqueryparam value="#aSQL[i].value#" cfsqltype="#aSQL[i].cfsqltype#" maxlength="#aSQL[i].maxlength#" scale="#aSQL[i].scale#" null="#aSQL[i].null#" list="#aSQL[i].list#" separator="#aSQL[i].separator#">
<!--- </cfif> --->
</cfdefaultcase>
</cfswitch>
</cfif>
</cfloop>
</cfsavecontent>
#sqlContent#
</cfquery>
<pre>#sqlContent#</pre>
Do make sure to put everything back to normal once you're done debugging.
If this question is more about HOW to debug or get some output you can work with, cftry and cfcatch are your friends.
<cftry>
---code logic---
<cfcatch>
<cfdump var="#cfcatch#">
</cfcatch>
</cfctry>
This should provide a complete dump of whatever errors ColdFusion encounters as well as SQL statements that were attempted, if there is indeed a syntax error generated by the loopy logic.
You have to work from the inside out this.
As I look at this query, I note that it is split on IsSimpleValue() and IsStruct(). So run this
<cfquery name="qQuery" datasource="#variables.datasource#">
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
</cfif>
</cfloop>
</cfquery>
Note that temp is never used.
The rest of the code creates <cfqueryparam>s
Conclusion
This code cannot work. It cannot create valid SQL.
Your error has nothing to do with the sql being generated by the code in the cfquery block. It's an undefined variable. If there was a problem with the code you displayed, the error message would be different.
Troublehoot as follows. First comment out all the code inside that query and replace it with:
select 1 record
This is valid for MS SQL. Leave everything else unchanged.
Running the page will produce the same error. You then have to determine why the query is not running. Likely, you have something like this going on:
<cfif some Condition is met>
run the query
</cfif>
dump the query
You'll have to determine why your condition wasn't met and make sure your page runs properly when it isn't.
The solution was to put <cftry> outside <cfloop> but not outside <cfquery>.I found that I forgot to send one parametter.
So the code is following:
<cfquery name="qQuery" datasource="#variables.datasource#">
<cftry>
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
<cfelseif IsStruct(aSQL[i])>
<cfset aSQL[i] = queryparam(argumentCollection=aSQL[i])>
<cfswitch expression="#aSQL[i].cfsqltype#">
<cfcase value="CF_SQL_BIT">
#getBooleanSqlValue(aSQL[i].value)#
</cfcase>
<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
#CreateODBCDateTime(aSQL[i].value)#
</cfcase>
<cfdefaultcase>
<cfqueryparam value="#aSQL[i].value#" cfsqltype="#aSQL[i].cfsqltype#" maxlength="#aSQL[i].maxlength#" scale="#aSQL[i].scale#" null="#aSQL[i].null#" list="#aSQL[i].list#" separator="#aSQL[i].separator#">
</cfdefaultcase>
</cfswitch>
</cfif>
</cfloop>
<cfcatch>
<cfdump var="#cfcatch#" >
</cfcatch>
</cftry>
</cfquery

Why is this SQL Update failing ("column name is not valid")?

I've got a SQL Server CE table like so:
...and I'm trying to update its solitary record like so:
update workTables
set fileType = "INV"
Yet I get:
Why?
UPDATE
Please see a related question here
Here check Microsoft support for yor error.
http://support.microsoft.com/kb/825392
This is from the site:
SYMPTOMS:
When you run a query on a Microsoft SQL Server 2000 Windows CE Edition version 2.0 database, and the query has a column that contains one or more space characters, the query may not be successful. Additionally, you may receive the following error message:
FAILED: select <Column Name> from <Table Name>
Error: 0x80040e14 DB_E_ERRORSINCOMMAND
Native Error: (25503)
Description: The column name is not valid. [,,,Node name (if any),Column name,]
Interface defining error: IID_ICommand
Param. 0: 0
Param. 1: 0
Param. 2: 0
Param. 3:
Param. 4: col1
Param. 5:
RESOLUTION:
To resolve this problem, enclose the column name that contains spaces in quotation marks (" "), and then run the query. For example, you can run the following query, and the query results are displayed successfully:
SELECT "col1 " FROM testtable
Your query should be:
update [workTables]
set [fileType] = 'INV'
Note: single quotes ^^^^

Coldfusion CFPDF

I am trying to use cfpdf and keep getting the following error:
String index out of range: -1
I don't understand why. I'm running ColdFusion 11 on Debian Linux.
<CFIF FileExists("#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf")>
<cfpdfform
action="read"
source="#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf" xmldata="x"
result="r">
</cfpdfform>
<cfdump var="#x#" label="XMLData">
<cfdump var="#r#" label="Result">
<CFELSE>
File doesn't exist
</CFIF>
Exact error:
String index out of range: -1
The error occurred in /var/www/www.test.com/test.cfm: line 2
1 : <CFIF FileExists("#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf")>
2 : <cfpdfform
action="read"
source="#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf"
xmldata="x" result="r">
3 : </cfpdfform>
4 : <cfdump var="#x#" label="XMLData">
I have had similar issues in the past. The problem has been using non Adobe programs to create the pdf - making a pdf form in Libreoffice, for example, can cause this issue. I assume some internal formatting in the file that CF is looking for is missing.
You can try opening in Acrobat and resaving it.

Find a whole sentence by SQL Contains command

Search for a whole sentence in a file stored in file table using CONTAINScommand ?
For example if user enter "SQL Server" sentence I want to search for files that just contain exactly "SQL Server" not "SQL SOME TEXT Server"
I get the following error : Syntax error near 'Server' in the full-text search condition 'SQL Server'
select *
from files
where contains(file_stream,'SQL Server')
Then I try this one :
select *
from files
where contains(file_stream,'SQL AND Server')
But this returned files containing "SQL SOME TEXT Server" too.
How do I correct this?
I believe you want this:
select * from files where contains(file_stream,'"SQL Server"')
notice the double quote.

ColdFusion: No data was received in the uploaded file

My error report told me that an error has occurred when an user tried uploading an empty file to my server (don't ask why the user did that - I don't know) and now I want to catch that exception which said "No data was received in the uploaded file". I wonder if there is a better way than putting a <CFTRY> around the <CFFILE action="upload"> like this:
<CFTRY>
<CFFILE action="upload" destination="#expandpath("upload")#" filefield="form.file" nameconflict="makeunique" />
<CFCATCH>
<!--- handle that error --->
</CFCATCH>
</CFTRY>
Try/Catch is the way I usually handle it.
<cftry>
<cffile action="upload" ...>
<cfcatch type="any">
<cfif Find("Saving empty (zero-length) files is prohibited", CFCatch.Detail) GT 0>
<!--- Create a zero length file on disk and continue processing as usual --->
<cffile action="write" file="..." output="">
<cfelse>
<cfrethrow>
</cfif>
</cfcatch>
</cftry>