Solved

Update a table and add id number incrementally?

Posted on 2009-05-05
2
178 Views
Last Modified: 2013-12-24
Experts,

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
0
Comment
Question by:g118481
2 Comments
 
LVL 27

Accepted Solution

by:
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="...">
SELECT * FROM .....
</cfquery>

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">
INSERT INTO ...
(id, col1, col2, ..., colN)
VALUES
(#curID#, #myxlsdata.integer_col1#, '#myxlsdata.text_col2#', ..., #myxlsdata.colN#)
</cfquery>
<cfset curID = curID + 1>
</cfoutput>

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">
INSERT INTO ...
(id, col1, col2, ..., colN)
VALUES
(#curID#, #listGetAt(row, 1)#, '#istGetAt(row, 2)#', ..., #istGetAt(row, N)#)
</cfquery>
<cfset curID = curID + 1>
</cfloop>

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!

Azadi
0
 
LVL 1

Author Closing Comment

by:g118481
ID: 31577981
Excellent information.
Thank you very much!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now