Solved

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

Posted on 2002-06-26
11
507 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
  • 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
 
LVL 19

Accepted Solution

by:
Rimvis earned 50 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Server 2008 Cluster Fail-over Errors 5 53
Updating variable table 9 17
select over clause 1 15
Join vs where 2 11
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 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

14 Experts available now in Live!

Get 1:1 Help Now