Skip to main content
May 25, 2012
Answered

Cfsettting twice- ok or wasteful?

  • May 25, 2012
  • 1 reply
  • 810 views

I have an update query and an add query that are used according to a condition. They both have the same cfset values. Can I just put the cfset tags outside the cfif blocks and get rid of the duplicate code? (the example below has two cfset tags, in reality I have about 30 in each so it is a lot of duplication) Thank you

<!--- UPDATE QUERY --->
<cfif isDefined("form.UPDATEADDBTN")>
<cfif FORM.ENTID GTE 1>
<cfset isCOMPANYNull = iif(len(trim(form.COMPANY)) EQ 0, true, false)>
<cfset isSTATE1Null = iif(len(trim(form.STATE1)) EQ 0, true, false)>

<CFQUERY name="updatecompany" datasource="salesdb">
update COMPANY
SET COMPANY = <cfqueryparam cfsqltype="cf_sql_longvarchar" value="#trim(form.COMPANY)#" null="#isCOMPANYNull#" />,
   STATE1= <cfqueryparam cfsqltype="cf_sql_integer" value="#trim(form.STATE1)#" null="#isSTATE1Null#" />
WHERE ENTID = #FORM.ENTID#
</CFQUERY>
<CFLOCATION URL="member_welcome.cfm?begin=1">
<cfelse>
<cfset isCOMPANYNull = iif(len(trim(form.COMPANY)) EQ 0, true, false)>
<cfset isSTATE1Null = iif(len(trim(form.STATE1)) EQ 0, true, false)>

<!--- ADD COMPANY QUERY --->
<CFQUERY name="addcompany" datasource="salesdb">
INSERT INTO COMPANY(
COMPANY,
STATE1

VALUES (
<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#trim(form.COMPANY)#" null="#isCOMPANYNull#" />,
<cfqueryparam cfsqltype="cf_sql_integer" value="#trim(form.STATE1)#" null="#isSTATE1Null#" />
)
</CFQUERY>
<CFLOCATION URL="member_welcome.cfm?begin=1">
</cfif>
</cfif>

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    Execution time is not affected because the tags only execute once.  Personally, I only put stuff in if/else blocks if the results are different in each block.

    1 reply

    Dan_BracukCorrect answer
    Inspiring
    May 25, 2012

    Execution time is not affected because the tags only execute once.  Personally, I only put stuff in if/else blocks if the results are different in each block.

    May 27, 2012

    Thanks Dan!