Reference a Column in a Dynamic Query

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Reference a Column in a Dynamic Query

    I'm creating a simulated a dynamic crosstab query from my SQL 2K table by
    looping over a recordset and creating my SQL statement. Everthing works fine
    until I try to output the query. Here is a bit of code.

    <cfquery name="list" datasource="#request.App.dsn#">
    SELECT distinct swa, legalValue
    FROM ActiveCL INNER JOIN CoreLV ON ActiveCL.swa = CoreLV.lKey
    WHERE ActiveCL.facility=<cfqueryparam cfsqltype="CF_SQL_INTEGER"
    value="#attributes.facility#">
    </cfquery>

    <cfset sqlString="">

    <cfoutput query="list">
    <cfset sqlString="#sqlString# sum(case b.swa when #list.swa# then 1 else 0
    end) as '#list.swa#',">
    </cfoutput>

    <cfset sqlString="#sqlString# 'placeHolder' as placeHolder">

    <cfquery name="Audit" datasource="#request.App.dsn#">
    SELECT distinct CLItems.clKey, min(CLItems.clDescription) as tclDescription,
    #preserveSingleQuotes(sqlString)#
    FROM CLItems LEFT JOIN
    (select Samples.clKey, Samples.swa
    from Samples
    where Samples.sampleTime > #createODBCDate(audit.lastRefresh)# and
    Samples.facility = #attributes.facility#) b
    ON CLItems.clKey = b.clKey
    WHERE CLItems.clCategory=#C_SAFETYAUDIT# AND CLItems.isActive=#C_YES#
    GROUP BY CLItems.clKey
    ORDER BY CLItems.clKey
    </cfquery>

    This creates a query with dynamic column names. I need to loop over this
    query "Audit" and output the results. I need to reference the dymanic column
    names in my output but cannot seem to get the syntax write.

    Here is what I have tried - and does not work...

    <cfoutput query="safetyAudit">
    <tr>
    <td>#safetyAudit.tclDescription#</td>
    <cfloop query=list>
    <cfset outField="safetyAudit.#List.swa#">
    <td>#evaluate(outField)#</td>
    </cfloop>
    </tr>
    </cfoutput>

    Any ideas?

    Thanks...

    GWG Guest

  2. Similar Questions and Discussions

    1. Dynamic Web Reference in Class Lib/GAC
      Hello. I have a shared assembly with a reference to one of our web services. This web reference needs to be dynamic so that we can use different...
    2. Reference Column Name by string...
      If you read half way through my question and think to yourself, "Huh?" you will know exactly how I feel right now. I am dynamically creating a...
    3. Move bound column to right of dynamic column in datagrid?
      I have a datatable that I am binding to a C# ASP.NET 1.1 web page. I also want to put an "Edit" column on the datagrid. However, whenever I use...
    4. How to reference cells once converted to Template Column
      Life is easy when I use a simple bound column. I refer to it as: MyString = e.Item.Cells(2).Text As soon as I convert this column to a Template...
    5. dynamic web reference
      change the 'URL Behavior' property of the web reference folder from static to dynamic.
  3. #2

    Default Re: Reference a Column in a Dynamic Query

    I've always been partial to lists in these situations. Nested loops can stall
    on the first outer occurence within the inner loop, although I don't know that
    that is what is happening to you. Try:

    <cfset colList = safetyAudit.ColumnList>
    <cfoutput query="safetyAudit">
    <tr>
    <td>#safetyAudit.tclDescription#</td>
    <cfloop index="ColName" list="#colList#">
    <td>#Evaluate("safetyAudit." & colName)#</td>
    </tr>
    </cfoutput>


    JMGibson3 Guest

  4. #3

    Default Re: Reference a Column in a Dynamic Query

    Worked great! Thanks
    GWG Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139