Update a table and add id number incrementally?

Posted on 2009-05-05
Last Modified: 2013-12-24

I have a spreadsheet with data that I need to write its contents to a sql server table.  And when I write the records from the spreadsheet to the table, I need to start the id field with the number 2147 and increment by 1, for the remainder of the records.

Also, I am using CF 5.0

What is the best process/approach to do this.  
Please provide a simple example if possible.

Thank you
Question by:g118481
LVL 27

Accepted Solution

azadisaryev earned 500 total points
ID: 24304351
1) if cf5 supports ODBC data sources based on Excel (i do not know - i started with cf6...), create a system datasourse pointing to your xls file (in Control Panel -> Administrative Tools -> Data Sources (ODBC) -> System DSN -> Add), then create a new cf dsn pointing to this windows system dsn

1-1) query this new cf dsn using <cfquery> tag:
<cfquery name="myxlsdata" datasource="...">

1-2) <cfset> a var for your id value:
<cfset curID = 2147>

1-3) <cfoutput> the myxlsdata query and insert data into your sql server table, incrementing curID value with every loop iteration:
<cfoutput query="myxlsdata">
<cfquery name="insertdata" datasource="your-sql-server-dsn">
(id, col1, col2, ..., colN)
(#curID#, #myxlsdata.integer_col1#, '#myxlsdata.text_col2#', ..., #myxlsdata.colN#)
<cfset curID = curID + 1>

2) if cf5 does not allow the above, export your xls file as csv (comma-separated values) file

2-1) read the file into a variable using <cffile>:
<cffile action="read" file="full\path\to\csv\file" variable="mycsvfile">

2-2) <cfset> a var for your id value:
<cfset curID = 2147>

2-3) <cfloop> over the above variable, treating it as a CR+LF-delimited list, and insert data into your sql server table in each loop iteration and incrementing curID variable:

<cfloop list="#mycsvfile#" delimiters="#chr(13)&chr(10)#" index="row">

<!--- now ROW variable (loop index) represents each line in your csv file, which in turn is a comma-delimited list of values to be inserted. use listGetAT() function to get individual row elements --->

<cfquery name="insertdata" datasource="your-sql-server-dsn">
(id, col1, col2, ..., colN)
(#curID#, #listGetAt(row, 1)#, '#istGetAt(row, 2)#', ..., #istGetAt(row, N)#)
<cfset curID = curID + 1>

that's the gist of it...

make sure the ID column in your sql server table allows you to insert data into it!

there are, however, numerous possible pitfalls to watch out for, especially with CSV approach, the biggest one being empty cell in xls file:
since csv file's lines (rows of data from your xls sheet) are treated as a comma-delimited list, and CF treats consecutive list delimiters as one list element, you MUST make sure each and every cell in your xls file has a value - empty cells should probably be filled with NULLs/0/spaces whatever is appropriate

another pitfall is: you must make sure that TEXT values you are inserting are enclosed in ' (single quotes) and NUMERIC values are not. you basically have to have a very detailed knowledge of the data in your xls file and how it corresponds to the columns in your sql server table.

hope this helps!


Author Closing Comment

ID: 31577981
Excellent information.
Thank you very much!

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Coldfusion update database with CFLOOP 5 64
How can I install php on a ngxin server on droplet 5 136
Website being blocked? 3 127
PHP in Apache server 20 96
A web service ( is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question