?
Solved

Importing a text/excel file in sql server table (no bcp)

Posted on 2002-06-26
11
Medium Priority
?
526 Views
Last Modified: 2012-06-22
i need to import data from text/excel file in a table in sql server but the field structure of tables differ from the one in the file. so i think i can't use bcp or DTS. is it possible to read files through procedures? or if u can provide any alternate solution.
0
Comment
Question by:Amitabh
[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
  • 4
  • 4
  • 2
  • +1
11 Comments
 

Author Comment

by:Amitabh
ID: 7112771
i wish to further clarify that i am looking for a solution in sql and not through VB and i am looking for some file manipulaton (file reading  and writing )technique in sql.
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 7112871
Even if field structures differ in file and DB, you can still use DTS.
After choosing Source and Destination, you can Transform your data. You can ignore some columns in source or destination, or you can use VBScript or JScript to do some additional transformations.
0
 

Author Comment

by:Amitabh
ID: 7112973

"or you can use VBScript or JScript to do some additional transformations." would be obliged if you Could plz pour some more light on this. Also (right now i don't have sql server) can i use DTS for importing files without bothering about any VBscript or Javascript coding ?
thanks...

amitabh
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 19

Accepted Solution

by:
Rimvis earned 200 total points
ID: 7112994
I presume you use DTS Wizard from SQL Server Enterprise Manager, right?

So as I mentioned previously, after selecting Source (text file in your case) and Destination (MS SQL Server or MSDE), you are in dialog where Source and Destination tables. Gues in text file case you see only one entry. Anyway, there's Transform button in that dialog. There you can do column mappings (eg. ignore some columns in Source or Destination) OR do Transformations. So here is the sample:

Function Main()
     DTSDestination("Col001") = DTSSource("Col001")
     DTSDestination("Col002") = (DTSSource("Col002") + DTSSource("Col004")) / 2
     DTSDestination("Col003") = Trim(DTSSource("Col003"))
     Main = DTSTransformStat_OK
End Function

In this example 1st column is mapped directly, 2nd column in DB is average result of 2nd and 4th column in text file, and 3rd column has leading and trailing spaces removed. You can operate columns as Variant variables.

Without script coding, you can use DTS for any column mapping. Scripts provide additional possibilities, when simple mapping is not enough.
0
 

Author Comment

by:Amitabh
ID: 7113011
thanks for this info Rimvis infact you have already answered the main part but a small question like in oracle where we have packages to read or write in file and even create files isn't there a way in sql server to do any file manipulations.
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 7113022
I'm not sure I understand what do you want to do :o/

Do you want do imort text files in Oracle instead of MS SQL? No problem, just specify your Oracle server as Destination.

If you want to EXPORT data into text files, just switch Source and Destination, and re-map columns.

I hope this will help.
0
 

Author Comment

by:Amitabh
ID: 7113125
no no....i am talking about sql server....i just gave example of oracle....
ok let me ask it this way....can i create a text file thru a stored procedure in sql server. or read a text file. i mean file manipulation which we do in VB.
since in oracle one can do it, i want to know if it is possible in MSSQL also.
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 7113194
I've just found an article in SQL Books Online regarding your issue. If you have SQL BO installed, search for "Using a Data File with Fewer Fields" article. It's about using bcp utility to copy data from file to DB by using format file. Haven't done it myself, so I can't provide more help than this article.

As for exporting data to text file, use same bcp utility, just format SQL query in suitable way.
0
 

Expert Comment

by:Bowthy
ID: 7113205
It's been a while since I used SQL Server DTS, but if I remember correctly, it allows you to import/export to/from SQL Server using many file formats - e.g. other SQL Server DBs, Oracle DBs, text files, MS Access Databases,  MS Excel spreadsheets etc etc etc.
0
 

Expert Comment

by:Bowthy
ID: 7113208
...and the DTS interface is wizard-based so is intuitive to use.  You can do more complex data transformations by adding a small-ish (depends on what you want to do) amount of vb code during the wizard process.
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 7113617
Rimvis has already answered this question
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

777 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