Solved

Convert Integer to String before Import to MS SQL with vbs

Posted on 2008-10-24
6
699 Views
Last Modified: 2012-05-05
I have a csv file that gets generated nightly and I have a simple vbs script that imports the records from the csv into a table in MS SQL Server 2005 Express. This was working fine until my company started to use numbers for Salesman_ID the csv and vbs sees these files first and assumes that they are integers. I need to have these appear as a string value prior to import. I have the field set up in the SQL table as Text and have tried nchar(4), text and just about everything I can think of. I can get the import to function with the script below but the issue is the file exports with the integer based Salesman_ID export first and the Insert statement only brings them in all records come in but the string based Salesman_ID field is blank....I am stumped with this one. I have included the current code below...
Set conn = CreateObject("ADODB.Connection")
DBstring = "Provider=SQLNCLI;Server=.\SQLEXPRESS;Database=ComRep;Integrated Security=SSPI;Persist Security Info=True;" 
conn.Open DBstring	
 
sconnstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\gmcgifts\qcs_upload;Extended Properties=""text;HDR=Yes;FMT=Delimited"""
Set objConn = CreateObject("ADODB.Connection")
     objConn.Open sConnString
          set rs=objConn.execute("SELECT * FROM [DPU893R_test.csv]")
		  
	    rs.MoveFirst
               While Not rs.EOF
		   
if RS.Fields.Item(3).Value = "ms" then
 
	if IsNull(RS.Fields.Item(17).Value) or RS.Fields.Item(17).Value = "" then
	
	REC_NAME = "NO Name"
	
	else
	
	REC_NAME = RS.Fields.Item(17).Value
	
	end if
	
	if IsNull(RS.Fields.Item(20).Value) or RS.Fields.Item(20).Value = "" then
	
	PROD_DESC = "NO Name"
	
	else
	
	PROD_DESC = RS.Fields.Item(20).Value
	
	end if
	
		REC_NAME = REPLACE(REC_NAME, "'","")
		PROD_DESC = REPLACE(PROD_DESC, "'","")
			  
		
  
  theSQL = "INSERT INTO ReleaseOrder"
  theSQL = theSQL &"(SLSM_NO, MAIL_NAME, COMM_CODE, ORD_NO, EARN_ORD_PRICE, EARN_SLSM_COST, EARN_COMM_AMT, ORD_TYPE, CUST_NO, DATE_ENTER, CUST_INV_DATE, WIP_CODE, "
  theSQL = theSQL &"PROG_ID, RLS_ORD_NO, CUST_NAME, SEQ, ITEM_NO, ITEM_DESC, COMM_PRICE_FL, STATUS)"
  theSQL = theSQL &"Values('"&RS.Fields.Item(0).value&"', '"&RS.Fields.Item(1).Value&"', '"&RS.Fields.Item(3).Value&"', '"&RS.Fields.Item(5).Value&"', '"&RS.Fields.Item(6).Value&"', '"&RS.Fields.Item(7).Value&"',"
  thesql = thesql &" '"&RS.Fields.Item(8).Value&"', '"&RS.Fields.Item(9).Value&"', '"&RS.Fields.Item(10).Value&"', '"&RS.Fields.Item(11).Value&"',"
  theSQL = theSQL &" '"&RS.Fields.Item(12).Value&"', '"&RS.Fields.Item(13).Value&"', '"&RS.Fields.Item(15).Value&"', '"&RS.Fields.Item(16).Value&"', '"&REC_NAME&"', '"&RS.Fields.Item(18).Value&"',"
  theSQL = theSQL &" '"&RS.Fields.Item(19).Value&"', '"&PROD_DESC&"', '"&RS.Fields.Item(21).Value&"', '"&RS.Fields.Item(25).Value&"')"
 
  conn.execute(theSQL)
  Set theSQL = Nothing
  
  
  end if
  		
  
  
   
 
 
rs.MoveNext
   Wend
 
 
msgbox("Complete")

Open in new window

0
Comment
Question by:panhead802
[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
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:CCongdon
ID: 22796135
Can we see a sample of your data in the CSV file?
By the way...VBScript does not 'see' integers or strings. All variables in VBScript are variants (or arrays). From MS's scripting site:
VBScript is a typeless language. This means that variables cannot be restricted to a single data type. VBScript does not allow you to specify in advance that a particular variable can hold only a particular kind of data. Instead, VBScript uses a single kind of variable, known as a variant, which can store any kind of data.
So, most likely the problem is in your table getting the Insert or in the data being saved to the CSV file.
0
 

Author Comment

by:panhead802
ID: 22796259
I had to convert the csv to a text file in order to upload it. I didn't think vbs saw strings or integers...I am grasping at straws now.
I also attached a smaller script I was using to test. It simply is supposed to display the salesman number in a message box, it works until it sees a string value...then it returns a null  value error. I am stumped. I thought at first it was an issue with my table but this script eliminated that.

Thanks in advance for any help
Dan
sconnstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\gmcgifts\qcs_upload;Extended Properties=""text;HDR=Yes;FMT=Delimited"""
Set objConn = CreateObject("ADODB.Connection")
     objConn.Open sConnString
          set rs=objConn.execute("select * FROM [DPU893R_test.csv]")'This opens the file uploaded
		  
		  
		  	   rs.MoveFirst'Move to the first record....
               While Not rs.EOF
			   
			   
			   
			   
			   
			   
			   msgbox(RS.Fields.Item(0).Value)
			   
			  
			   
			   rs.MoveNext
   			   Wend
 
 
 
msgbox("Deleted")

Open in new window

DPU893R-test2.txt
0
 
LVL 9

Expert Comment

by:CCongdon
ID: 22796402
I bet it's something in JET doing it to you. Since JET is database, it's probably having to assume it is one data type or another. And so it sets it to one data type...and then gets upset when it starts looking like another.
I modified your test script...see if it returns the SLSM_NO column for everything.

sconnstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\gmcgifts\qcs_upload;Extended Properties=""text;HDR=Yes;FMT=Delimited"""
Set objConn = CreateObject("ADODB.Connection")
objConn.Open sConnString
set rs=objConn.execute("select CSTR(SLSM_NO) FROM [DPU893R_test.csv]")'This opens the file uploaded
 
rs.MoveFirst'Move to the first record....
While Not rs.EOF
   msgbox(RS.Fields.Item(0).Value)
   rs.MoveNext
Wend
msgbox("Deleted")

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:panhead802
ID: 22796510
Tried the script exactly as posted. Null error went away but the msgbox returned a value for the integer type values in the csv and no value for the string type values.

It is very strange, if I remove the int type values from the csv all goes as planned if I move them to the end of the file all as planned.
0
 
LVL 9

Accepted Solution

by:
CCongdon earned 500 total points
ID: 22796664
OK, that confirms it for me. The problem is in JET itself.
Edit your data file. Put double-quotes around all of the SLSM_NO data points. Run your script again.
The problem appears to be this....JET is a database engine. DB engines MUST have typed data. Text file, by nature, is typeless, so JET must make an implicit conversion when it creates the 'table' in memory. It sees numeric data in the field, so it creates the table with that column as a numeric data type...and then chokes somewhat when it gets to a string.
 
0
 

Author Comment

by:panhead802
ID: 22797097
It is definitely a jet issue, I have been looking into using a registry mod on the server.

Just tested and it worked like a charm:

changed
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\ImportMixedTypes from majority to text
and
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\MaxScanRows from 19 to 0

Seems to have fixed the Jet problem.
I am going to give you the points because you pointed me to the Jet issue. I don't think I would have caught that issue.
Thansks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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