[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2002-06-26
11
Medium Priority
?
531 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

650 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