Solved

Update a table and add id number incrementally?

Posted on 2009-05-05
2
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ColdFusion Rereplace 3 97
DNS, website, godaddy 6 101
Point a domain to a 4rd party web host without changing nameservers 2 57
Domain hosting question about hiding URL 9 53
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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