A number of code examples in the ColdFusion Developer's Guide reuse code, particularly queries. To illustrate the advantages of CFCs, these examples invoke the appropriate method in the CFC that appears in the following example. Although Adobe recommends using CFCs to create structured, reusable code, some code examples in this manual contain queries within a CFML page, rather than invoking a CFC, in order to clearly illustrate a particular element of ColdFusion.
<cfcomponent> <cffunction name="allemployees" access="public" output="false" returntype="query"> <cfset var getNames=""> <cfquery name="getNames" datasource="cfdocexamples"> SELECT * FROM Employee </cfquery> </cffunction> <cffunction name="namesalarycontract" access="public" output="false" returntype="query"> <cfset var EmpList=""> <cfquery name="EmpList" datasource="cfdocexamples"> SELECT Firstname, Lastname, Salary, Contract FROM Employee </cfquery> </cffunction> <cffunction name="fullname" access="public" output="false" returntype="query"> <cfset var engquery=""> <cfquery name="engquery" datasource="cfdocexamples"> SELECT FirstName || ' ' || LastName AS FullName FROM Employee </cfquery> </cffunction> <cffunction name="bydept" access="public" output="false" returntype="query"> <cfset var deptquery=""> <cfquery name="deptquery" datasource="cfdocexamples"> SELECT Dept_ID, FirstName || ' ' || LastName AS FullName FROM Employee ORDER BY Dept_ID </cfquery> </cffunction> <cffunction name="employeebyURLID" access="public" output="false" returntype="query"> <cfset var GetRecordtoUpdate=""> <cfquery name="GetRecordtoUpdate" datasource="cfdocexamples"> SELECT * FROM Employee WHERE Emp_ID = #URL.Emp_ID# </cfquery> </cffunction> <cffunction name="deleteemployee" access="public" output="false" returntype="void"> <cfset var DeleteEmployee=""> <cfquery name="DeleteEmployee" datasource="cfdocexamples"> DELETE FROM Employee WHERE Emp_ID = #Form.Emp_ID# </cfquery> </cffunction> <cffunction name="distinctlocs"access="public" output="false" returntype="void"> <cfset var GetDepartments=""> <cfquery name="GetDepartments" datasource="cfdocexamples"> SELECT DISTINCT Location FROM Departmt </cfquery> </cffunction> </cfcomponent>