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
 
ColdFusion LanguageSQLMicrosoft SQL Server

Avatar of undefined
Last Comment
_agx_
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of diecasthft01
diecasthft01

ASKER

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.
Avatar of diecasthft01
diecasthft01

ASKER

Oh wait... I had a typo....yes I think that works....
Avatar of Amir Azhdari
Amir Azhdari
Flag of United States of America image

Let me know how you get on then.
Avatar of diecasthft01
diecasthft01

ASKER

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!!!
Avatar of Amir Azhdari
Amir Azhdari
Flag of United States of America image

You're very welcome- Thanks for the update. I'm glad it helped.
Avatar of _agx_
_agx_
Flag of United States of America image

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

Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo