Solved

Convert Integer to String before Import to MS SQL with vbs

Posted on 2008-10-24
6
696 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

15 Experts available now in Live!

Get 1:1 Help Now