Skip to main content
WolfShade
Legend
October 16, 2012
Answered

cfqueryparam altering query??

  • October 16, 2012
  • 3 replies
  • 2254 views

Hello, everyone.

Got a weird issue (IMHO)..

I have a medium sized SELECT query that takes four dynamic values - three for the WHERE clause, one for the ORDER BY.

If I run the query within a CFC method without using CFQUERYPARAM, it runs exactly as expected.

If I use CFQUERYPARAM on any/all of the WHERE clause, it ignores the ORDER BY and just returns data as it is.

What is happening???

Thanks,

^_^

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    The query is about 12 lines long and goes all the way across the screen.

    Basically

    SELECT a.colA, a.colB, a.colC, b.colC, b.colD, c.colA, c.colC, c.colD, d.colA, d.colF

    FROM tableA a LEFT OUTER JOIN tableB b on b.id = a.id

       LEFT OUTER JOIN tableC c on c.id = a.id

       LEFT OUTER JOIN tableD d on d.tid = c.tid

       LEFT OUTER JOIN tableE e on e.org = b.org

    WHERE d.tid = [training id] AND c.auth = [authorization id] AND b.year = [4 digit year]

    ORDER BY #by#

    by can be last name, date completed, or organization.


    If this:

    SELECT a.colA, a.colB, a.colC, b.colC, b.colD, c.colA, c.colC, c.colD, d.colA, d.colF

    FROM tableA a LEFT OUTER JOIN tableB b on b.id = a.id

    LEFT OUTER JOIN tableC c on c.id = a.id

    LEFT OUTER JOIN tableD d on d.tid = c.tid

    LEFT OUTER JOIN tableE e on e.org = b.org

    WHERE d.tid = [training id] AND c.auth = [authorization id] AND b.year = [4 digit year]

    ORDER BY #by#

    resembles your actual query, you have a logic problem.  You are specifying left joins to tables b,c, and d and then using those tables in your where clause.  This changes your outer join to an inner join.  You fix it by moving those filters to your from clause, like this:

    from tablea a left join tableb b on b.id = a.id and b.year = [4 digit year]

    3 replies

    WolfShade
    WolfShadeAuthor
    Legend
    October 16, 2012

    I was panicky, for a bit.  I had never encountered something like that, before.  But, then, I don't use JOINs, very often.

    Looking at some of my co-worker's code, putting a CFQUERY inside a loop seems to be SOP; but I've never liked that.  So I'm trying to break that and use JOINs wherever possible.

    Thanks, everyone, for your time.  I do appreciate it.

    ^_^

    Inspiring
    October 16, 2012

    Looking at some of my co-worker's code, putting a CFQUERY inside a loop seems to be SOP; but I've never liked that.

    Only if SOP in this case means "Sh!tty Operating Procedure" ;-)

    You should aim to hit the DB only when necessary.  As many times as necessary, sure, but make sure the trip is necessary first.

    It is seldom (if ever) necessary to loop over something and then hit the DB each iteration, using values from the iteration to pass to the DB.  What you're doing tin rying to make it a single hit is the correct way to go about things, if poss.

    This - of course - is a sweeping generalisation, but it's gonna be right far more often than it's wrong.

    --

    Adam

    WolfShade
    WolfShadeAuthor
    Legend
    October 16, 2012

    Adam Cameron. wrote:


    This - of course - is a sweeping generalisation, but it's gonna be right far more often than it's wrong.

    I completely agree.  Highly inefficient, hogs bandwidth, etc.  And just plain lazy, if you ask me.  I wish they would let us create SPs, but they don't use SPs.  :/

    ^_^

    12Robots
    Participating Frequently
    October 16, 2012

    You can't use cfqueryparam with the value going into the ORDER BY clause. cfqueryparam is only for statements in the WHERE clause.

    You'll need to find another way to sanitize the input for ORDER BY.

    Jason

    WolfShade
    WolfShadeAuthor
    Legend
    October 16, 2012

    @Adam: Unfortunately, I cannot copy/paste code - dev system is isolated from internet, no cd burning permissions, flash drives are proscribed.

    @Jason: CFQUERYPARAMS are only going on the WHERE clauses, I hadn't got to the ORDER BY sanitization, yet.  Was going to work on that after I get this issue resolved.

    Thank you, both, for your help.

    ^_^

    12Robots
    Participating Frequently
    October 16, 2012

    And you cannot retype a sample of the code?

    Jason

    Inspiring
    October 16, 2012

    Impossible to comment sensibly without seeing the code.

    --

    Adam