User Guide Cancel

QueryAddRow

 

Description

Adds a specified number of empty rows to a query. The function accepts either number of rows or any data structure (struct, arrays or structs, arrays of arrays).

Returns

The number of rows in the query

Category

Query functions

Function syntax

QueryAddRow(query [, number])
QueryAddRow(query [, number])
QueryAddRow(query [, number])
QueryAddRow(query [, data])
QueryAddRow(query [, data])
QueryAddRow(query [, data])

See also

QueryAddColumnQuerySetCellQueryNewCreating a recordset with the QueryNew() function in the Developing ColdFusion Applications

Parameters

Parameter

Description

query

Name of an executed query.

number

Number of rows to add to the query. The default value is 1.

Usage

As previously mentioned, ColdFusion lets you specify a struct, an array of structs, or arrays with single or multiple dimensions to add rows to the query as shown in the following example:

queryAddRow(myQuery1,
[
{id=2,name="Two"},
{id=3,name="Three"},
{id=4,name="Four"}
]);
queryAddRow(myQuery2,{id=4,name="Four"});
queryAddRow(myQuery1,
[
[1,"One"],
[2,"Two"],
[3,"Three"]
]);
queryAddRow(myQuery1, [ {id=2,name="Two"}, {id=3,name="Three"}, {id=4,name="Four"} ]); queryAddRow(myQuery2,{id=4,name="Four"}); queryAddRow(myQuery1, [ [1,"One"], [2,"Two"], [3,"Three"] ]);
queryAddRow(myQuery1, 
[ 
{id=2,name="Two"}, 
{id=3,name="Three"}, 
{id=4,name="Four"} 
]); 
queryAddRow(myQuery2,{id=4,name="Four"});



queryAddRow(myQuery1, 
[ 
[1,"One"], 
[2,"Two"], 
[3,"Three"] 
]);

Example

<h3>QueryAddRow Example</h3>
<!--- start by making a query --->
<cfquery name = "GetCourses" datasource = "cfdocexamples">
SELECT Course_ID, Number, Descript
FROM Courses
</cfquery>
<p>The Query "GetCourses" has <cfoutput>#GetCourses.RecordCount#</cfoutput> rows.
<cfset CountVar = 0>
<cfloop CONDITION = "CountVar LT 15">
<cfset temp = QueryAddRow(GetCourses)>
<cfset CountVar = CountVar + 1>
<cfset Temp = QuerySetCell(GetCourses, "Number", 100*CountVar)>
<cfset Temp = QuerySetCell(GetCourses, "Descript",
"Description of variable #Countvar#")>
</cfloop>
<P>After the QueryAddRow action, the query has <CFOUTPUT>#GetCourses.RecordCount#</CFOUTPUT>
records.
<CFOUTPUT query="GetCourses">
<PRE>#Course_ID# #Number# #Descript#</pre>
</cfoutput>
<h3>QueryAddRow Example</h3> <!--- start by making a query ---> <cfquery name = "GetCourses" datasource = "cfdocexamples"> SELECT Course_ID, Number, Descript FROM Courses </cfquery> <p>The Query "GetCourses" has <cfoutput>#GetCourses.RecordCount#</cfoutput> rows. <cfset CountVar = 0> <cfloop CONDITION = "CountVar LT 15"> <cfset temp = QueryAddRow(GetCourses)> <cfset CountVar = CountVar + 1> <cfset Temp = QuerySetCell(GetCourses, "Number", 100*CountVar)> <cfset Temp = QuerySetCell(GetCourses, "Descript", "Description of variable #Countvar#")> </cfloop> <P>After the QueryAddRow action, the query has <CFOUTPUT>#GetCourses.RecordCount#</CFOUTPUT> records. <CFOUTPUT query="GetCourses"> <PRE>#Course_ID# #Number# #Descript#</pre> </cfoutput>
<h3>QueryAddRow Example</h3> 

<!--- start by making a query ---> 
<cfquery name = "GetCourses" datasource = "cfdocexamples"> 
SELECT Course_ID, Number, Descript 
FROM Courses 
</cfquery> 

<p>The Query "GetCourses" has <cfoutput>#GetCourses.RecordCount#</cfoutput> rows. 

<cfset CountVar = 0> 
<cfloop CONDITION = "CountVar LT 15"> 
<cfset temp = QueryAddRow(GetCourses)> 
<cfset CountVar = CountVar + 1> 
<cfset Temp = QuerySetCell(GetCourses, "Number", 100*CountVar)> 
<cfset Temp = QuerySetCell(GetCourses, "Descript", 
"Description of variable #Countvar#")> 
</cfloop> 

<P>After the QueryAddRow action, the query has <CFOUTPUT>#GetCourses.RecordCount#</CFOUTPUT> 
records. 
<CFOUTPUT query="GetCourses"> 
<PRE>#Course_ID# #Number# #Descript#</pre> 
</cfoutput>

Example

The following example first creates an empty recordset. Then it creates a struct with key-value pairs that represent rows in the query recordset. Finally the rows are populated with the struct data.

<cfscript>
myTeam=QueryNew("Jersey,Player_Name,Matches_Played","integer,varchar,integer"); //Create empty recordset myTeam
//Display query headers
WriteOutput("Empty query recordset:");
WriteDump(myTeam);
/*Now add rows and populate rows with data. Create a struct with data*/
RealMadrid=StructNew();
RealMadrid={Jersey=1,Player_Name="Keylor Navas",Matches_Played=50};
QueryAddRow(myTeam,RealMadrid);
//Add more data to Real Madrid
RealMadrid={Jersey=7,Player_Name="Cristiano Ronaldo",Matches_Played=75};
//Add the updated struct as data in myTeam
QueryAddRow(myTeam,RealMadrid);
WriteOutput("Record with rows added:");
WriteDump(myTeam);
</cfscript>
<cfscript> myTeam=QueryNew("Jersey,Player_Name,Matches_Played","integer,varchar,integer"); //Create empty recordset myTeam //Display query headers WriteOutput("Empty query recordset:"); WriteDump(myTeam); /*Now add rows and populate rows with data. Create a struct with data*/ RealMadrid=StructNew(); RealMadrid={Jersey=1,Player_Name="Keylor Navas",Matches_Played=50}; QueryAddRow(myTeam,RealMadrid); //Add more data to Real Madrid RealMadrid={Jersey=7,Player_Name="Cristiano Ronaldo",Matches_Played=75}; //Add the updated struct as data in myTeam QueryAddRow(myTeam,RealMadrid); WriteOutput("Record with rows added:"); WriteDump(myTeam); </cfscript>
<cfscript>
       myTeam=QueryNew("Jersey,Player_Name,Matches_Played","integer,varchar,integer"); //Create empty recordset myTeam
       //Display query headers
       WriteOutput("Empty query recordset:");
       WriteDump(myTeam);
       /*Now add rows and populate rows with data. Create a struct with data*/
       RealMadrid=StructNew();
       RealMadrid={Jersey=1,Player_Name="Keylor Navas",Matches_Played=50};
       QueryAddRow(myTeam,RealMadrid);
       //Add more data to Real Madrid
       RealMadrid={Jersey=7,Player_Name="Cristiano Ronaldo",Matches_Played=75};
       //Add the updated struct as data in myTeam
       QueryAddRow(myTeam,RealMadrid);
       WriteOutput("Record with rows added:");
       WriteDump(myTeam);
</cfscript>

Output

Using member function

The member function returns the updated query, not the number of rows as per the function version.

<cfscript>
myQuery=QueryNew("ID, Name, Age","integer,varchar,integer");
// Add rows and populate rows with struct data
people=StructNew();
people={ID=50,Name="John Doe",Age=30};
added=QueryAddRow(myquery,people)
writeOutput(added) // Returns 1
//break;
peopleAnother={ID=51,Name="Jane Doe",Age=32};
myQuery.addRow(peopleAnother);
WriteDump(myQuery);
</cfscript>
<cfscript> myQuery=QueryNew("ID, Name, Age","integer,varchar,integer"); // Add rows and populate rows with struct data people=StructNew(); people={ID=50,Name="John Doe",Age=30}; added=QueryAddRow(myquery,people) writeOutput(added) // Returns 1 //break; peopleAnother={ID=51,Name="Jane Doe",Age=32}; myQuery.addRow(peopleAnother); WriteDump(myQuery); </cfscript>
<cfscript>
       myQuery=QueryNew("ID, Name, Age","integer,varchar,integer");
       // Add rows and populate rows with struct data
       people=StructNew();
       people={ID=50,Name="John Doe",Age=30};
       added=QueryAddRow(myquery,people)
       writeOutput(added) // Returns 1
       //break;
       peopleAnother={ID=51,Name="Jane Doe",Age=32};
       myQuery.addRow(peopleAnother);
       WriteDump(myQuery);
</cfscript>

Get help faster and easier

New user?