The queryNew() and queryAddRow() functions are used to create a query data structure and add empty rows to it. To add record sets to the query, function querySetCell() is used. If there are say three columns and you want to add say five rows to the query, then you would have about sixteen lines of code. That's huge and it will increase with number of columns and the number of records that you want to add. In ColdFusion 10 there's a third parameter introduced in queryNew() and queryAddRow() functions. The third parameter can accept either a struct, an array of structs. Ray Camden had covered this in one of his POTW posts. One feedback that came from one of the ColdFusion users is to use an array or a two dimensional array to initialize or add rows to the query. This has now been implemented and is available in the public beta release. Here’s how it works:
As observed the third argument is a two dimensional array. If there is a need to add more rows to the above query then you can use the queryAddRow() function to do that:
Unlike initializing the query with a struct or an array of structs this doesn't require you to mention the column names in each row. However, if there are more number of columns then I suggest you to use array of structs to initialize the query.
<cfscript>
myQuery=queryNew("id,name","Integer,Varchar",[
[1,"One"],
[2,"Two"],
[3,"Three"]
]);
</cfscript>
As observed the third argument is a two dimensional array. If there is a need to add more rows to the above query then you can use the queryAddRow() function to do that:
<cfscript>
queryAddRow(myQuery,[
[4,"Four"],
[5,"Five"],
[6,"Six"]
]);
<cfscript>
Unlike initializing the query with a struct or an array of structs this doesn't require you to mention the column names in each row. However, if there are more number of columns then I suggest you to use array of structs to initialize the query.
Comments
Post a Comment