queryNew

Creates a new query object. The query can be populated with data using functions queryAddRow, querySetCell, or by passing it in to the rowData argument.

queryNew(columnList [, columnTypeList [, rowData]]) → returns query

Argument Reference

columnList string
Required

A string or a variable that contains one. Delimited list
of column names, or an empty string.

columnTypeList string

CF 7+ Comma-delimited list specifying column data types.
Values:
  • Integer
  • BigInt
  • Double
  • Decimal
  • VarChar
  • Binary
  • Bit
  • Time
  • Date
  • Timestamp

rowData any

CF 10+ Data to populate the query. Can be a struct (with keys matching column names), an array of structs, or an array of arrays (in same order as columnList)

Examples
Sample code invoking the queryNew function

Using Script with the queryAddRow querySetCell functions to populate the query.

news = queryNew("id,title", "integer,varchar");
queryAddRow(news);
querySetCell(news, "id", "1");
querySetCell(news, "title", "Dewey defeats Truman");
queryAddRow(news);
querySetCell(news, "id", "2");
querySetCell(news, "title", "Men walk on Moon");
writeDump(news);

Using CFML Tags with the queryAddRow querySetCell functions to populate the query.

<cfset news = queryNew("id,title", "integer,varchar")>
<cfset queryAddRow(news)>
<cfset querySetCell(news, "id", "1")>
<cfset querySetCell(news, "title", "Dewey defeats Truman")>
<cfset queryAddRow(news)>
<cfset querySetCell(news, "id", "2")>
<cfset querySetCell(news, "title", "Men walk on Moon")>
<cfset writeDump(news)>

CF 10+ Passes an array of structs to create a new query.

news = queryNew("id,title",
    "integer,varchar",
    [ {"id":1,"title":"Dewey defeats Truman"}, {"id":2,"title":"Man walks on Moon"} ]);
writeDump(news);

CF 10+ If you only need one row you can pass a single struct instead of an array into the rowData argument.

news = queryNew("id,title",
    "integer,varchar",
    {"id":1,"title":"Dewey defeats Truman"});
writeDump(news);

CF2018u5+ Directly assigns columns and values with an array of structs.

news = queryNew([
    {"id":1,"title":"Dewey defeats Truman"},
    {"id":2,"title":"Man walks on Moon"}
]);
writeDump(news);

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub