Вопрос

There have been several times when I've created a cfquery and for no good reason, it doesn't work. For example, I recently had a query like this:

<cfquery name="get_projects" datasource="#application.dsn#">
        SELECT *
        FROM   projects
        WHERE  project_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#project_id#">

          <cfif start_date NEQ "">
               AND project_start_date = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#start_date#">
          </cfif>

        ORDER BY project_name
    </cfquery>

I confirmed the start_date variable was an empty string. However, I'll get an error that points to that line and says "Value can not be converted to requested type" even though it should never have gotten to that line.

But here's the really weird thing.... If I add "AND 1=1" to the WHERE clause, the query works perfectly.

I've had this happen sporadically on a number of queries, but I can't say I recognize a pattern to it. Always the query is written perfectly. In some cases, the query was working previously, and suddenly it stops, possibly when there is a change somewhere else in the file. All I have to do is add "AND 1=1" to the WHERE clause, and it works again. Anyone else run into this or have ideas how to fix it?

Это было полезно?

Решение

I've seen it happen where the query gets compiled incorrectly and changing any part of the text in it (such as adding 1=1) re-compiles it.

Другие советы

Its a shortcut a lot of developers use, here is an example

select * from table where 1 = 1
<cfif x EQ 1>and x = 1</cfif>
<cfif y EQ 1>and y = 1</cfif>
<cfif z EQ 1>and z = 1</cfif>

This always works. There is a conditional and in 3 ifs. But if all the ands are false you end up with the query:

select * from table where 1=1

ie Return all records

If you have one or multiple ands being true, and you don't have the 1=1, you end up with:

select * from table where and x = 1

.. which doesn't work. So its just a simple shortcut to ensure all the ands work regardless of whether you need 0 or all of them.

I don't think that query is correct anyway - there is no AND or OR combining the two parts in the WHERE clause.

I don't see how your query above would run as is.

I added the AND in your CFIF statement. This should run (when you repair the cfqueryparams.

SELECT *
FROM   projects
WHERE  project_id = <cfqueryparam>
       <cfif start_date NEQ "">
          AND project_start_date = <cfqueryparam>
      </cfif>
ORDER BY project_name

I often use the "AND 1=1" statement like this, where I start the WHERE clause with the 1 = 1:

SELECT *
FROM   projects
WHERE  1 = 1
       AND project_id = <cfqueryparam>
       <cfif start_date NEQ "">
          AND project_start_date = <cfqueryparam>
      </cfif>
ORDER BY project_name

IIRC, a Timestamp is not the same as a Date. If you're using MSSQL Server, see http://msdn.microsoft.com/en-us/library/ms182776(v=sql.90).aspx

<cfqueryparam cfsqltype="cf_sql_timestamp" value="#start_date#">

should be

<cfqueryparam cfsqltype="cf_sql_date" value="#start_date#">

Adding AND 1=1 probably causes the server's query optimizer to ignore the entire WHERE predicate.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top