Solved

How to create a SQL Server Table using CSV text file

Posted on 1998-07-30
3
135 Views
Last Modified: 2010-05-03
How can i create a table using CSV text file specification
I have a CSV file a I want to use it to create a table in SQL Server. The file has variable number of columns in each line (example: 1'st line = 12 columns, 12'th line = 14 columns).
0
Comment
Question by:labrego
  • 2
3 Comments
 
LVL 9

Expert Comment

by:cymbolic
ID: 1467289
Are you interested in creatng a general purpose program to be used in an unattended mode on a repetitive basis, or is this just a one time special conversion?  

Anyway, if you ship me a small part of the front of your file, I'll take a look and see if I can whip out a small routine from the megs of VB/Basic code under my belt here at cymbolic@aol.com.

Just attach it to a note with enough specific information so I can get the job done.
0
 
LVL 1

Expert Comment

by:bharris1
ID: 1467290
I think you could use the Distributed Managment Objects(SQL-DMO) that should be on the SQL Server CD to create these.  I know a guy who wrote code that creates tables and stored procs based on class design.  He used SQL-DMO and found it to be pretty easy and useful.

0
 
LVL 9

Accepted Solution

by:
cymbolic earned 200 total points
ID: 1467291
You can use transact SQL and the Create Table syntax in the .execute method of RDO to create a table.  You can also use SQL-DMO by including the SQLOLE object in your project.  It also supports the use of Transact SQL and provides database and table objects and collections, if you're into that metaphor.  A complete SQL-DMO Object model can be downloaded from the MSDN site on the Web.

Here's a copy of an SQL Script that creates a table:

/****** Object:  Table dbo.PchFilLst    Script Date: 7/9/98 3:09:22 PM ******/
if exists (select * from sysobjects where id = object_id('dbo.PchFilLst') and sysstat & 0xf = 3)
      drop table dbo.PchFilLst
GO

/****** Object:  Table dbo.PchFilLst    Script Date: 7/9/98 3:09:22 PM ******/
CREATE TABLE dbo.PchFilLst (
      PchNum char (15) NOT NULL ,
      FilNum int NOT NULL ,
      FilNam char (25) NULL ,
      FilTyp char (1) NULL ,
      BldVer char (15) NULL ,
      LodMsk char (15) NULL ,
      Lap char (1) NULL ,
      UsrAdd char (1) NULL ,
      WrkStn char (1) NULL ,
      Reg char (1) NULL ,
      LapFilLoc char (255) NULL ,
      SrvFilLoc char (255) NULL ,
      CONSTRAINT PK___3__44 PRIMARY KEY  CLUSTERED
      (
            PchNum,
            FilNum
      )
)
GO

The variable number of columns is another problem, but you can solve that yourself by reading the file in VB, and creating your own Insert Into <table> SQL for each row and doing the inserts yourself using the .execute method.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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