Link to home
Start Free TrialLog in
Avatar of diecasthft01
diecasthft01

asked on

Inserting data from one table to another with NULLS in SQL Server / Coldfusion

Good morning. I am hoping to get some assistance with an SQL / CF query. I have a CF app that part of it queries an existing table in SQL Server and then inserts that data into another empty SQL Server table. This worked fine in Oracle but in my migration to SQL it fails. The problem seems to be that the insert fails at a specific field (EXPENSES) becasue that field has NULLS in it. My SQL "EXPENSES" field is a numeric field (18,2). My code is below:
 
First I grab the data from the existing table:
<cfquery name="ListERP19" datasource="SQL_MCA">
SELECT * FROM MCA.ERP_CUR
</cfquery>
 
Then I insert it into the new table.
<CFLOOP query="ListERP19">
<cfquery name="COPYNEWcsv" datasource="SQL_MCA">
         INSERT INTO MCA.ERP_PREV(ALLOTMENT, COMMITMENT, OBLIGATION, EXPENSES, WBS_ALL)
         VALUES
                  ( '#ListERP19.ALLOTMENT#',
                   '#ListERP19.COMMITMENT#',
                   '#ListERP19.OBLIGATION#',
                   <cfqueryparam value="#ListERP19.EXPENSES#" scale="2" cfsqltype="CF_SQL_NUMERIC" null="no">,
                  '#ListERP19.WBS_ALL#' )   
   </cfquery>
 
I even tried this:
case when '#ListERP19.EXPENSES#' is null then 0 else '#ListERP19.EXPENSES#' end,
 
And it didnt work either. The first three insert values work, basically becasue there are no NULLS in those three values. Any help/guidance would be greatly appreciated
 
Avatar of Amir Azhdari
Amir Azhdari
Flag of United States of America image

Hi,
What if you replace the cfqueryparam value="#ListERP19.EXPENSES#"  line with the following: (change null="no" to null="yes")

 <cfqueryparam value="#ListERP19.EXPENSES#" scale="2" cfsqltype="CF_SQL_NUMERIC" null="yes">,
Avatar of diecasthft01
diecasthft01

ASKER

If I change NULL from No to Yes, then I get no error, but it also doesn't insert any of the "Expenses" records. It just puts NULL in every row.
ASKER CERTIFIED SOLUTION
Avatar of Amir Azhdari
Amir Azhdari
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Im afraid not.....there seems to be issue with these NULLS. I went into the raw data and changed a NULL to zero, and it got past that one record but then errored out at the next NULL.
Oh wait... I had a typo....yes I think that works....
Let me know how you get on then.
The link you provided worked out...I just had to twaek it a bit...but it works and is exactly what I needed. Thanks a lot!!!
You're very welcome- Thanks for the update. I'm glad it helped.
I know this is way after the fact (: .. but if all you need is to copy data between tables within the same database, no need for a cfloop or cfqueryparam at all. Just insert the data directly

<cfquery datasource="SQL_MCA">
         INSERT INTO MCA.ERP_PREV( ALLOTMENT, COMMITMENT, OBLIGATION, EXPENSES, WBS_ALL )
         SELECT ALLOTMENT, COMMITMENT, OBLIGATION, EXPENSES, WBS_ALL
         FROM     MCA.ERP_CUR
</cfquery>
 

Open in new window