0
Inserting null values into database

/t5/coldfusion-discussions/inserting-null-values-into-database/td-p/113466
Nov 15, 2008
Nov 15, 2008
Copy link to clipboard
Copied
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/inserting-null-values-into-database/m-p/113467#M10911
Nov 15, 2008
Nov 15, 2008
Copy link to clipboard
Copied
set the default value for numeric arguments to 0 and string
arguments to
'' (empty string)
then use the NULL attribute in your <cfqueryparam> tags:
for numeric arguments:
<cfqueryparam value="#arguments.amt#" cfsqltype="cf_sql_money"
null="#yesnoformat(arguments.amt)#"> - 0 evaluates to FALSE/NO as
boolean value)
for text/string arguments:
<cfqueryparam value="#arguments.sometextvalue#"
cfsqltype="cf_sql_varchar"
null="#yesnoformat(len(trim(arguments.sometextvalue)))#"> -
len(trim('')) is 0, which will evaluate to FALSE/NO
hth
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
'' (empty string)
then use the NULL attribute in your <cfqueryparam> tags:
for numeric arguments:
<cfqueryparam value="#arguments.amt#" cfsqltype="cf_sql_money"
null="#yesnoformat(arguments.amt)#"> - 0 evaluates to FALSE/NO as
boolean value)
for text/string arguments:
<cfqueryparam value="#arguments.sometextvalue#"
cfsqltype="cf_sql_varchar"
null="#yesnoformat(len(trim(arguments.sometextvalue)))#"> -
len(trim('')) is 0, which will evaluate to FALSE/NO
hth
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/inserting-null-values-into-database/m-p/113468#M10912
Nov 16, 2008
Nov 16, 2008
Copy link to clipboard
Copied
What if the intent is to submit a value of 0?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/coldfusion-discussions/inserting-null-values-into-database/m-p/113469#M10913
Nov 17, 2008
Nov 17, 2008
Copy link to clipboard
Copied
I couldn't get a most satisfactory answer, but here's what I
ended up doing for numeric arguments. In cfargument, I remove the
requirement for the input to be numeric so that I can set the
default to "" (empty string). However, if a user tries to enter in
a non-numeric value, an error will get triggered by cfqueryparam
which requires the input to be numeric.
<cfargument name="amt" required="no" default="">
<cfqueryparam value="#arguments.amt#" null="#not len(arguments.amt)#" cfsqltype="cf_sql_money">
Thanks!
Min
<cfargument name="amt" required="no" default="">
<cfqueryparam value="#arguments.amt#" null="#not len(arguments.amt)#" cfsqltype="cf_sql_money">
Thanks!
Min
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
LATEST
/t5/coldfusion-discussions/inserting-null-values-into-database/m-p/113470#M10914
Nov 17, 2008
Nov 17, 2008
Copy link to clipboard
Copied
Since one of the unsatisfactory answers was mine, maybe I'll
give you a better one. This will work for any data type.
<cfscript>
var OptionalArguments = "textfield,numberfield,datefield";
var textfield_null = false;
var numberfield_null = false;
var datefield_null = false;
<cfscript>
<cfloop list = "#OptionalArguments#" index = "idx">
<cfscript>
if (isDefined("Arguments.#idx#") is false)
variables[idx & "_null"] = true;
</cfscript>
Then in your query, use null = "#textfield_null#", etc in your cfqueryparam tag.
<cfscript>
var OptionalArguments = "textfield,numberfield,datefield";
var textfield_null = false;
var numberfield_null = false;
var datefield_null = false;
<cfscript>
<cfloop list = "#OptionalArguments#" index = "idx">
<cfscript>
if (isDefined("Arguments.#idx#") is false)
variables[idx & "_null"] = true;
</cfscript>
Then in your query, use null = "#textfield_null#", etc in your cfqueryparam tag.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

