Solved

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

Posted on 2002-06-26
11
524 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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
 
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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 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