Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8379695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:15:34+00:00 2026-06-09T16:15:34+00:00

I usually create update statements like this. But I know there is a better

  • 0

I usually create update statements like this. But I know there is a better way. How can I approve upon this? *Note the sample below is pseudo demo, may not run.

<cffunction name="updateEmp" returntype="void">
    <cfargument name="empId" required="yes" hint="empId">
    <cfargument name="firstName" required="yes" hint="firstName">
    <cfargument name="lastName" required="yes" hint="lastName">

    <!--- Get emp details in db --->
   <cfquery datasource="#ds#" name="getEmployee">
        SELECT *
        FROM Employee
        WHERE  emp_id = <cfqueryparam
                value="#arguments.empId#" 
                CFSQLType="CF_SQL_INTEGER">
    </cfquery>

    <!--- If employee is in db or if emp db details are different --->
    <cfif getEmployee.recordCount eq 1 
            and getEmployee.firstName neq trim(arguments.firstName) 
             or getEmployee.lastName  neq trim(arguments.lastName)>

        <cfquery name="UpdateExistingEmployee" datasource="#ds#">

                UPDATE Employee
                SET 1 = 1
                    <cfif getEmployee.firstName neq trim(arguments.firstName)>
                        ,firstName = <cfqueryparam 
                                    value="#arguments.firstName#" 
                                    CFSQLType="CF_SQL_VARCHAR" >
                    </cfif>

                    <cfif getEmployee.lastName neq trim(arguments.lastName)>
                        ,lastName = <cfqueryparam 
                                    value="#arguments.lastName#" 
                                    CFSQLType="CF_SQL_VARCHAR" >
                    </cfif>

                WHERE emp_id=<cfqueryparam
                    value="#emp_id#" 
                    CFSQLType="CF_SQL_INTEGER">

        </cfquery>

    </cfif>
    <!--- maybe return success? --->
</cffunction>

Edited:

<cffunction name="updateEmp" returntype="void">
        <cfargument name="empId" required="yes" hint="empId">
        <cfargument name="firstName" required="yes" hint="firstName">
        <cfargument name="lastName" required="yes" hint="lastName">

            <cfquery name="UpdateExistingEmployee" datasource="#ds#">

                    UPDATE Employee
                        SET firstName = <cfqueryparam 
                                        value="#arguments.firstName#" 
                                        CFSQLType="CF_SQL_VARCHAR" >

                            ,lastName = <cfqueryparam 
                                        value="#arguments.lastName#" 
                                        CFSQLType="CF_SQL_VARCHAR" >

                    WHERE emp_id=<cfqueryparam
                        value="#emp_id#" 
                        CFSQLType="CF_SQL_INTEGER">

            </cfquery>

        <!--- maybe return success? --->
    </cffunction>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:15:35+00:00Added an answer on June 9, 2026 at 4:15 pm

    Not sure what you are trying to improve. If you just want to write less code you might just do an update:

    <cffunction name="updateEmp" returntype="void">
        <cfargument name="empId" required="yes" hint="empId">
        <cfargument name="firstName" required="yes" hint="firstName">
        <cfargument name="lastName" required="yes" hint="lastName">
        <cfquery name="UpdateExistingEmployee" datasource="#ds#">
          UPDATE Employee
          SET firstName = <cfqueryparam value="#arguments.firstName#" CFSQLType="CF_SQL_VARCHAR">
              ,lastName = <cfqueryparam value="#arguments.lastName#" CFSQLType="CF_SQL_VARCHAR" >
          WHERE emp_id=<cfqueryparam value="#emp_id#" CFSQLType="CF_SQL_INTEGER">
        </cfquery>
    </cffunction>
    

    The check for an existing record is pretty much superfluous since the where clause will prevent any action if no emp_id matches, and why bother checking if the names match? If they do, then you just updated them to be the same, if they don’t you’re going to update them anyway. There’s no logical reason to be doing all that checking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems dead simple, as to create an NSURLConnection I usually do this: NSURL
Is there any handy tool that can make updating tables easier? Usually I got
I usually use sql parameters with queries, but in this case I need to
I would like to know the best approach to create a Common Information Model
I have an algorithm where I create two bi-dimensional arrays like this: TYPE TPtrMatrixLine
What solutions,patterns usually used for this? I want to get rid of if/else statements
I have a MySQL table like this: CREATE TABLE IF NOT EXISTS `mytable` (
I have a view like this: CREATE VIEW MyView AS SELECT Column FROM Table
I usually create a pixel format using wglChoosePixelFormatARB() with these arguments (among others): WGL_DOUBLE_BUFFER_ARB
I usually create a gridview based on data so it will have the rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.