Skip to main content
Inspiring
August 20, 2024
Question

I want to insert a value if a column exists in query else insert null

  • August 20, 2024
  • 1 reply
  • 1186 views

Hi ,

Right now I'm using something like this,

My query is ,

<cfloop query="local.Q_Data>

insert into ...values(

 
                                       
                                         <cfif QueryKeyExists(local.Q_Data,"FILE_OPERATOR")><cfqueryparam cfsqltype="cf_sql_varchar" value="#TRIM(FILE_OPERATOR)#" null="#this.comp('Imports').checkIsNull(FILE_OPERATOR)#"><cfelse>NULL</cfif>
.
.
.
                                       

</cfloop>

 

Now I want to change the code to something like ,

 

  , <cfqueryparam cfsqltype="cf_sql_varchar" value="#IIF(QueryKeyExists(local.Q_Data,'FILE_OPERATOR'),TRIM(FILE_OPERATOR),NULL)#"> null="#this.comp('Imports').checkIsNull(FILE_OPERATOR)#">

 

But it is not working. I'm getting error like FILE_OPERATOR is undefined.

Please help. Thank you.

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    August 20, 2024

    The following structure can be improved using the ternary operator

     

    <cfloop query="local.Q_Data>
        <cfif QueryKeyExists(local.Q_Data,"FILE_OPERATOR")>
        </cfif>
    </cfloop>

     

    There is yet another place where the code can be improved. The code invokes a component to check for a null value: this.comp('Imports').checkIsNull(FILE_OPERATOR). This is not only expensive, it is also unnecessary.

     

    At this point in the code we have local.Q_data. Therefore we know which values of local.Q_Data['FILE_OPERATOR'] are null or the empty string. (Note: in ColdFusion, null may be stored as empty-string within a query-result. )

     

    So, let's put the two suggestions together. To do so, I shall assume that what you want to achieve is similar to the following:

     

    INSERT INTO MyTable
      ( Column1, Column2, fileOperator, column4, column5)
    VALUES
     (1, 103, 'Operator One', 'Mumbai', 'India'), 
     (2, 105, 'Operator Two', 'Beijing', 'China'), 
     (3, 107, 'Operator Three', 'Paris', 'France'), 
     (4, 109, 'Operator Four', 'London', 'UK'), 
     (5, 111, 'Operator Five', 'New York', 'USA'),
     (6, 113, 'Operator Six', 'Milan', 'Italy'),
     (7, 115, 'Operator Seven', 'Rio de Janeiro', 'Brazil'),
     (8, 117, 'Operator Eight', 'Johannesburg', 'South Africa')

     

    That is to say, I am assuming that you intend to use a loop to insert each row of values. Then the required code will be something like:

     

    <cfquery >
    	INSERT INTO MyTable (
    		Column1, 
    		Column2, 
    		fileOperator, 
    		Column4,
    		Column5)
    	VALUES 
    	<cfloop query="local.Q_Data">		
    		<cfset currentFileOperatorValue=queryKeyExists(local.Q_Data,"FILE_OPERATOR") ? "#FILE_OPERATOR#" : "">
    		
    		(
    			#Column1#,
    			#Column2#,
    			<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(currentFileOperatorValue)#" null="#trim(currentFileOperatorValue) eq ''#">,
    			'#Column4#',
    			'#Column5#')
    
    	   <cfif CURRENTROW LT RECORDCOUNT>
    	   		,
    	   </cfif>
        </cfloop>
    </cfquery>
    

     

    Some explanations:

    • value="#trim(currentFileOperatorValue)#" null="#trim(currentFileOperatorValue) eq ''#" means that null will be inserted into the database if trim(currentFileOperatorValue) eq '' is true. Otherwise, trim(currentFileOperatorValue) will be inserted instead.
    • <cfif CURRENTROW LT RECORDCOUNT> ,</cfif> ensures that a comma is added between the rows that are to be inserted.

    That's it.

    You should also have a look at the INSERT INTO SELECT statement.

    Inspiring
    August 21, 2024

    Thank you for your reply. But is there a way to use iif or cfif tag inside cfqueryparam? 

    BKBK
    Community Expert
    August 21, 2024
    quote

    Thank you for your reply. But is there a way to use iif or cfif tag inside cfqueryparam? 


    By @Vishnu22410012h6s8

    Yes. What I had given was just a suggestion to improve the original code you posted. I infact expected that you would further improve the code. In any case, your question has drawn my attention to some typos and duplicated code, which I have corrected.

     

    As an example, you could go one step further, and improve the code as follows:

     

     

    <cfquery >
    	INSERT INTO MyTable (
    		Column1, 
    		Column2, 
    		fileOperator, 
    		Column4,
    		Column5)
    	VALUES
    	<cfloop query="local.Q_Data">
    		 (
    			#intVar1#,
    			#intVar2#,
    			<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(FILE_OPERATOR)#" null="#NOT queryKeyExists(local.Q_Data,'FILE_OPERATOR') OR local.Q_Data['FILE_OPERATOR'][CURRENTROW] EQ ''#">,
    			'#var4#',
    			'#var5#')
    
    	   <cfif CURRENTROW LT RECORDCOUNT>
    	   		,
    	   </cfif>
        </cfloop>
    </cfquery>