Solved

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

Posted on 2002-06-26
11
510 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

912 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

22 Experts available now in Live!

Get 1:1 Help Now