Question
Inserting null values into database
Hi,
I am creating a function that inserts a row into a database table. I would like to make some of the function arguments optional; if no values are passed these arguments are not entered.
<cffunction name="insertRow">
<cfargument name="tid" type="numeric" required="yes">
<cfargument name="amt" type="numeric" required="no">
<cfquery name="insertTableRow">
insert into myTable(tid
<cfif isDefined('arguments.amt')>,amount</cfif>)
values(<cfqueryparam value="#arguments.tid#" cfsqltype="cf_sql_integer">
<cfif isDefined('arguments.amt')>,<cfqueryparam value="#arguments.amt#" cfsqltype="cf_sql_money"></cfif>)
</cfquery>
</cffunction>
Since I will have many optional arguments, doing <cfif> for each one of them will get my code unwieldy. Is there a better way to do this?
I tried to set the default value of the optional arguments to null, but that creates error with both <cfargument> and <cfqueryparam>
<cfargument name="amt" type="numeric" required="no" value="null">
<cfqueryparam value="#arguments.amt#" cfsqltype="cf_sql_money">
Any suggestions?
Thanks,
Min
I am creating a function that inserts a row into a database table. I would like to make some of the function arguments optional; if no values are passed these arguments are not entered.
<cffunction name="insertRow">
<cfargument name="tid" type="numeric" required="yes">
<cfargument name="amt" type="numeric" required="no">
<cfquery name="insertTableRow">
insert into myTable(tid
<cfif isDefined('arguments.amt')>,amount</cfif>)
values(<cfqueryparam value="#arguments.tid#" cfsqltype="cf_sql_integer">
<cfif isDefined('arguments.amt')>,<cfqueryparam value="#arguments.amt#" cfsqltype="cf_sql_money"></cfif>)
</cfquery>
</cffunction>
Since I will have many optional arguments, doing <cfif> for each one of them will get my code unwieldy. Is there a better way to do this?
I tried to set the default value of the optional arguments to null, but that creates error with both <cfargument> and <cfqueryparam>
<cfargument name="amt" type="numeric" required="no" value="null">
<cfqueryparam value="#arguments.amt#" cfsqltype="cf_sql_money">
Any suggestions?
Thanks,
Min
