How to convert from SQL Server table to Flat file (txt file)

I need To ask question how convert from SQL Server table to Flat file txt file

Hi
1. Import/Export wizened
2. Bcp utility
3. SSIS 
1.Import/Export Wizard
First and very manual technique is the import wizard.  This is great for ad-hoc and just to slam it in tasks.
In SSMS right click the database you want to import into.  Scroll to Tasks and select Import Data…
For the data source we want out zips.txt file.  Browse for it and select it.  You should notice the wizard tries to fill in the blanks for you.  One key thing here with this file I picked is there are “ “ qualifiers.  So we need to make
sure we add “ into the text qualifier field.   The wizard will not do this for you.
Go through the remaining pages to view everything.  No further changes should be needed though
Hit next after checking the pages out and select your destination.  This in our case will be DBA.dbo.zips.
Following the destination step, go into the edit mappings section to ensure we look good on the types and counts.
Hit next and then finish.  Once completed you will see the count of rows transferred and the success or failure rate
Import wizard completed and you have the data!
bcp utility
Method two is bcp with a format file http://msdn.microsoft.com/en-us/library/ms162802.aspx
This is probably going to win for speed on most occasions but is limited to the formatting of the file being imported.  For this file it actually works well with a small format file to show the contents and mappings to SQL Server.
To create a format file all we really need is the type and the count of columns for the most basic files.  In our case the qualifier makes it a bit difficult but there is a trick to ignoring them.  The trick is to basically throw a field into the
format file that will reference it but basically ignore it in the import process.
Given that our format file in this case would appear like this
9.0
9
1 SQLCHAR 0 0 """ 0 dummy1 ""
2 SQLCHAR 0 50 "","" 1 Field1 ""
3 SQLCHAR 0 50 "","" 2 Field2 ""
4 SQLCHAR 0 50 "","" 3 Field3 ""
5 SQLCHAR 0 50 ""," 4 Field4 ""
6 SQLCHAR 0 50 "," 5 Field5 ""
7 SQLCHAR 0 50 "," 6 Field6 ""
8 SQLCHAR 0 50 "," 7 Field7 ""
9 SQLCHAR 0 50 "n" 8 Field8 ""
The bcp call would be as follows
C:Program FilesMicrosoft SQL Server90ToolsBinn>bcp DBA..zips in “C:zips.txt” -f “c:zip_format_file.txt” -S LKFW0133 -T
Given a successful run you should see this in command prompt after executing the statement
Starting copy...
1000 rows sent to SQL Server. Total sent: 1000
1000 rows sent to SQL Server. Total sent: 2000
1000 rows sent to SQL Server. Total sent: 3000
1000 rows sent to SQL Server. Total sent: 4000
1000 rows sent to SQL Server. Total sent: 5000
1000 rows sent to SQL Server. Total sent: 6000
1000 rows sent to SQL Server. Total sent: 7000
1000 rows sent to SQL Server. Total sent: 8000
1000 rows sent to SQL Server. Total sent: 9000
1000 rows sent to SQL Server. Total sent: 10000
1000 rows sent to SQL Server. Total sent: 11000
1000 rows sent to SQL Server. Total sent: 12000
1000 rows sent to SQL Server. Total sent: 13000
1000 rows sent to SQL Server. Total sent: 14000
1000 rows sent to SQL Server. Total sent: 15000
1000 rows sent to SQL Server. Total sent: 16000
1000 rows sent to SQL Server. Total sent: 17000
1000 rows sent to SQL Server. Total sent: 18000
1000 rows sent to SQL Server. Total sent: 19000
1000 rows sent to SQL Server. Total sent: 20000
1000 rows sent to SQL Server. Total sent: 21000
1000 rows sent to SQL Server. Total sent: 22000
1000 rows sent to SQL Server. Total sent: 23000
1000 rows sent to SQL Server. Total sent: 24000
1000 rows sent to SQL Server. Total sent: 25000
1000 rows sent to SQL Server. Total sent: 26000
1000 rows sent to SQL Server. Total sent: 27000
1000 rows sent to SQL Server. Total sent: 28000
1000 rows sent to SQL Server. Total sent: 29000
bcp import completed!
BULK INSERT
Next, we have BULK INSERT given the same format file from bcp
CREATE TABLE zips (
Col1 nvarchar(50),
Col2 nvarchar(50),
Col3 nvarchar(50),
Col4 nvarchar(50),
Col5 nvarchar(50),
Col6 nvarchar(50),
Col7 nvarchar(50),
Col8 nvarchar(50)
GO
INSERT INTO zips
SELECT *
FROM OPENROWSET(BULK 'C:Documents and SettingstkruegerMy Documentsblogcenzuszipcodeszips.txt',
FORMATFILE='C:Documents and SettingstkruegerMy Documentsblogzip_format_file.txt'
) as t1 ;
GO
That was simple enough given the work on the format file that we already did.  Bulk insert isn’t as fast as bcp but gives you some freedom from within TSQL and SSMS to add functionality to the import.
SSIS
Next is my favorite playground in SSIS
We can do many methods in SSIS to get data from point A, to point B.  I’ll show you data flow task and the SSIS version of BULK INSERT
First create a new integrated services project.
Create a new flat file connection by right clicking the connection managers area.  This will be used in both methods
Bulk insert
You can use format file here as well which is beneficial to moving methods around.  This essentially is calling the same processes with format file usage.  Drag over a bulk insert task and double click it to go into the editor.
Fill in the information starting with connection.  This will populate much as the wizard did.
Example of format file usage
Or specify your own details
Execute this and again, we have some data
Data Flow method
Bring over a data flow task and double click it to go into the data flow tab.
Bring over a flat file source and SQL Server destination.  Edit the flat file source to use the connection manager “The file” we already created.  Connect the two once they are there
Double click the SQL Server Destination task to open the editor.  Enter in the connection manager information and select the table to import into.
Go into the mappings and connect the dots per say
Typical issue of type conversions is Unicode to non-unicode.
We fix this with a Data conversion or explicit conversion in the editor.  Data conversion tasks are usually the route I take.  Drag over a data conversation task and place it between the connection from the flat file source to the SQL Server destination.
New look in the mappings
And after execution…
SqlBulkCopy Method
Sense we’re in the SSIS package we can use that awesome “script task” to show SlqBulkCopy.  Not only fast but also handy for those really “unique” file formats we receive so often
Bring over a script task into the control flow
Double click the task and go to the script page.  Click the Design script to open up the code behind
Ref.
Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

Similar Messages

  • How to import  from sql server to oracle 10g

    Hi,
    I have given some file with no extention and the client told me that
    it has two tables in it emp and dept. which he extracted from sql server.
    Now I have the file with me.
    I have a local oracle database .
    Is it poosible to load that file into the oracle 10g database.
    If possible please send me the details. If not please tell me the details reqd to do the things.
    Thanks in Advance

    Well, what format is the file in? If it is a text file you should be able to use SQL*Loader.
    Can you view the file using (for example) Wordpad?
    Or from the command line (ie in a DOS box) just say
    TYPE myfileThen post the format for a few lines here, between and (use square brackets instead of curly)
    Regards Nigel

  • Error in Oracle 10 Procedure - converted from SQL Server

    I have converted the folowing script from SQL Server proc to Oracle Proc; it compiles with a warning "30 Hint: Comparision with NULL in 'sp_MyEmployee'"
    When this proc is executed; it generates an error "ORA-00900: invalid SQL statement"
    Please help!
    CREATE OR REPLACE PROCEDURE osp_DMEmployee(v_ahRole IN VARCHAR2 DEFAULT null,
                        v_Exclusion IN varchar2 DEFAULT null) as
    v_MyString varchar2(32767);
    v_spot NUMBER(5,0);
    v_str varchar2(8000);
    v_email varchar2(5000);
    SWV_Exclusion varchar2(5000);
    BEGIN
    SWV_Exclusion := v_Exclusion;
    v_MyString := 'SQLWAYS_EVAL# exprsnlid, dmdescription, dmregion, dmfieldloc, dmbusentity,
                                  dmemployeenum, dmusername, dmtitle, dmemail, ahrole
                             FROM dm_Employee WHERE dmEmail is NOT NULL';
    if v_ahRole is not NULL then
    IF v_ahRole = 'PLANNER' then
    v_MyString := v_MyString || 'SQLWAYS_EVAL# AND (DMTitle like ''%Coord%'' OR DMTitle like ''%Planner%'' OR DMTitle like ''%Svc Leader%'') ';
    end if;
    IF v_ahRole = 'TECHNICIAN' then
    v_MyString := v_MyString || 'SQLWAYS_EVAL# ''' || v_ahRole || 'SQLWAYS_EVAL# is NOT NULL ';
    end if;
    IF v_ahRole = 'SUPERVISOR' then
    v_MyString := v_MyString || 'SQLWAYS_EVAL# ''' || v_ahRole || 'SQLWAYS_EVAL# is NOT NULL ';
    end if;
    end if;     
    IF SWV_Exclusion is NOT NULL then
    /* WHILE SUBSTR(CAST(SWV_Exclusion AS VARCHAR2),1,4000) <> '' LOOP */
    WHILE SWV_Exclusion <> '' LOOP
    v_spot := instr(SWV_Exclusion,',');
    IF v_spot > 0 then
    v_str := SUBSTR(SWV_Exclusion,1,v_spot -1);
    SWV_Exclusion := SUBSTR(SWV_Exclusion,-(LENGTH(SWV_Exclusion) -v_spot));
    ELSE
    v_str := SWV_Exclusion;
    SWV_Exclusion := '';
    end if;
    IF v_email is NOT NULL then
    v_email := v_email || ',' || ' ''' || SUBSTR(v_str,1,100) || ''' ';
    ELSE
    v_email := ' ''' || SUBSTR(v_str,1,100) || ''' ';
    end if;
    END LOOP;
    end if;
    v_MyString := v_MyString || 'SQLWAYS_EVAL# IN (' || v_email || ')';
    v_MyString := v_MyString || 'SQLWAYS_EVAL# ail';
    DBMS_OUTPUT.PUT_LINE(SUBSTR(v_MyString,1,250));
    EXECUTE IMMEDIATE v_MyString;
    RETURN;
    END;

    1) When discussing an error, please copy the actual error message. Usually you will get a line number to assist investigation;
    2) Please refer to the FAQ (upper right corner of page) to show how to preserve code and make your question more readable;
    3) Please ask the question in a forum that deals with the topic - such as "Database - General" (look for "Forum Home" link at top left of page)
    4) Please note that the title of this forum is "Community Feedback and Suggestions (Do Not Post Product-Related Questions Here)"
    (And ... did you display the SQL that you were attempting to execute. Execute Immediate is a last resort style of programming - not nice for most Oracle apps.)

  • Fetch the data from sql server table to array

    In the following script i am fetching the servers details from text file. Please anyone help me to get the same information from sql server database table. with using this query
    "SELECT DISTINCT [server_name]
    FROM
    Servers] where
    Status='1'"
    $ServerName =Get-Content "c:\servers\servers.txt"
     foreach ($Server in $ServerName) {
         if (test-Connection -ComputerName $Server -Count 4 -Delay 2 -Quiet ) {
           write-output "$Server is alive and Pinging `n"
           } else {
    Write-output "TXUE $Server seems dead not pinging"

    i have tested it is not working..
    =@"
    SELECT DISTINCT [server_name] FROM Servers] where Status='1'
    $connection
    =new-objectsystem.data.sqlclient.sqlconnection(
    "Data Source=xxx;Initial Catalog=xxx;Integrated Security=SSPI;”)
    $adapter
    =new-objectsystem.data.sqlclient.sqldataadapter($query,
    $connection)
    $table
    =new-objectsystem.data.datatable
    $adapter
    .Fill($table)
    | out-null
    $compArray
    =@($table)
    ##### Script Starts Here ######
    foreach($Serverin$ServerName)
    if(test-Connection-ComputerName$Server-Count4
    -Delay2
    -Quiet)
    write-output"$Server
    is alive and Pinging `n"
    else{
    $query

  • How to insert into SQL server table form oracle forms

    I created a form with oracle as my database. But there one trigger where I need to insert the data into a sql server table.
    Is this possible. If so can any help me out.
    Thanks in advance.
    Asha

    Hi,
    You can insert into sql server database using the following steps
    Note: Check wether you are using Forms 32 bit drivers. If not the Odbc data source will not work.
    step 1: Create ODBC data source for SQL server(one time creation);
    step 2: Logout from Oracle and login to SQL server giving the user name,password and host string as odbc:<odbc datasource name>;
    step 3: use EXEC SQL statement to insert the values into the SQL server and then logout and login again to your oracle database.
    Second Method.
    Check the sql server documentation to insert the values using command line parameters. Then you can call the host command to execute this.
    Third Method.
    Write a VB exe to enter the values in the sql server making two connections one to oracle another to SQL server and then getting values from Oracle and putting in the SQL server database. You can call this exe using the Host command.
    Hope this will help You.
    Regards
    Gaurav Thakur

  • How to migrate from sql server to oracle

    Hi ,
    My requirement is we are planning to migrate the sql server data to oracle.
    what is the way to do migration from sql server to oracle. Please let me know if any tools are there for migration.Previous we used sql developer( which is provided by oracle i.e. freeware )
    tool but it was created only table structures not ported any data.

    Hi,
    Oracle sql developer is a great tool for migration from sql server to oracle..
    Refer the link,
    http://www.oracle.com/technetwork/database/migration/sqlserver-095136.html

  • Migration Workbench is messing up data in converted from SQL Server image fields.

    I am working on porting a SQL Server 7 database to Oracle 8i (8.1.6 release 2). In the SQL Server database I have several tables that contain image fields that hold binary data (like Word documents, and other files). When I port the database to Oracle, the data in these fields gets garbled (the resultant records contain the correct amount of data (bytes), yet the data is different). Data in image fields that contain text oriented data ports correctly (i.e. if you just saved a long string in the field). Has anyone else posted a similar problem? Is there a workaround here to get the data stored in these image fields to port correctly?

    The thing is DDL of SQL Server and Oracle are quite different, without the help of MW from SQL Developer. You can't convert SQL server version of DDL to Oracle version. Of course you can accomplish the same thing using other tools like ERwin and Visio. Since SQL Developer is free why bother?

  • Convert from SQL server to Oracle?

    How do I do this query in Oracle?
    In SQL Server, it looks like this.
    select
    SELECT top 1 t1.mpm
    FROM TblMtk T1
    where t1.wc = '34819'
    and t1.ValidFrom <= '2012-09-12'
    order by t1.ValidFrom desc
    ) R1
    SELECT top 1 t2.mpm
    FROM TblMtk T2
    where t2.wc = '34819'
    order by t2.ValidFrom desc) R2

    A follow-up question ...
    If the table tblmtk for example, there are three rows like this:
    WC        MPM        Valid_from
    34819     1          2010-06-01
    34819     2.5        2011-01-01
    34819     1.5        2012-01-01If the value is outside of the validity period (valid_from) shall it take the last valid_from value. How?
    select max(MPM) keep(dense_rank first order by case when valid_from  <= '2007-05-10' then valid_from else null end desc nulls last) r1
      from tblmtk
    where wc = '34819'I want the result to look like this:
    R1= 1.5
    But now I get R = 2.5

  • Unable To Select From SQL Server table with more than 42 columns

    I have set up a link between a Microsoft SQL Server 2003 database and an Oracle 9i database using Heterogeneous Services (HSODBC). It's working well with most of the schema I'm selecting from except for 3 tables. I don't know why. The common denominator between all the tables is that they all have at least 42 columns each, two have 42 columns, one has 56, and the other one, 66. Two of the tables are empty, one has almost 100k records, one has has 170k records. So I don't think the size of the table matters.
    Is there a limitation on the number of table columns you can select from through a dblink? Even the following statement errors out:
    select 1
    from "Table_With_42_Cols"@sqlserver_db
    The error message I get is:
    ORA-28500: connection from ORACLE to a non-Oracle system returned this message [Generic Connectivity Using ODBC]
    ORA-02063: preceding 2 lines from sqlserver_db
    Any assistance would be greatly appreciated. Thanks!

    Not a very efficient and space friendly design to do name-value pairs like that.
    Other methods to consider is splitting those 1500 parameters up into groupings of similar parameters, and then have a table per group.
    Another option would be to use "vertical table partitioning" (as oppose to the more standard horizontal partitionining provided by the Oracle partition option) - this can be achieved (kind of) in Oracle using clusters.
    Sooner or later this name-value design is going to bite you hard. It has 1500 rows where there should be only 1 row. It is not scalable.. and as you're discovering, it is unnatural to use. I would rather change that table and design sooner than later.

  • How to Migrate from SQL server to oracle 11g

    Hi,
    i have many tables in sql server and i want to link them in oracle

    Welcome to the forum,
    Hi,
    To link Oracle with SQL Server follow this steps:
    1-Install Oracle ODBC drivers on the server (your local Oracle database) that will access the remote SQL Server database using the database link.
    2. Setup the ODBC connection on the local Oracle database using the Windows ODBC Data Source Administrator
    3. Test the ODBC drivers to ensure that connectivity is made to the SQL Server database.
    4. Ensure that your global_names parameter is set to False.
    5. Configure the Oracle Heterogeneous services by creating an initodbc.ora file within the Oracle database.
    7. Modify the Listener.ora file.
    SID_NAME is the DSN for the remote database.
    ORACLE_HOME is the actual Oracle home file path.
    PROGRAM tells Oracle to use heterogeneous services.
    SID_LIST_LISTENER =
    (SID_LIST =
    (SID_DESC =
    (SID_NAME=Cas30C) -- Enter the DSN on this line
    (ORACLE_HOME = c:\oracle10gdb) -- Enter your Oracle home on this line
    (PROGRAM = hsodbc) ) )
    8. Modify the Tnsnames.ora file. This is the Oracle database installation accessed by the users to connect to the ODBC drivers
    (DESCRIPTION=
    (ADDRESS_LIST=
    (Address=(PROTOCOL=TCP)
    (HOST=
    -- (Server x)
    (PORT=1521))) -- Enter the port on which the server x Oracle installation
    -- is listening
    (CONNECT_DATA=(SID=Cas30c)) - Enter the DSN name
    (HS=OK) -- Enter this value. It tells Oracle to use hetergeneous services
    9. Reload the listener on local Oracle database
    10. Create a database link on the local Oracle installation that accesses the heterogeneous connection, which, in turn, connect to SQL Server.
    11. Run a SQL Server Select statement from the Oracle installation using the database link
    Thanks
    BelMan :)

  • Need to delete specific Months Data from SQL Server Table

    Greetings Everyone,
    So i have one table which contains 5 years old data, now business wants to keep just one year old data and data from qurter months i.e. (jan, mar, June, sep and December), i need to do this in stored procedure. how i can achive this using month lookup table.
    Thank you in advance
    R

    Hi Devin,
    In a production environment, you should be double cautious about the data. I have no idea why you’re about to remove the data just years old. In one of the applications I used to support, the data retention policy is like to keep raw data for latest month
    and the elder data would get rollup as max, min, average and so on to store in another table. That’s a good example for data retention.
    In your case I still suggest you keep the elder data in another table. If the data size is so huge that violates  your storage threshold, get the data rollup and store the aggregated would be a good option.
    Anyway if you don’t care about the elder data, you can just delete them with code like below.
    DELETE
    FROM yourTable
    WHERE YEAR(dateColumn) < YEAR(CURRENT_TIMESTAMP) OR (MONTH(dateColumn) not in (1,3,6,9,12) AND YEAR(dateColumn) = YEAR(CURRENT_TIMESTAMP))
    In some cases to remove data from very large table, DELETE performs bad. TRUNCATE would be a better option which works faster. Read more by clicking
    here. In your case, if necessary, you can reference the below draft code.
    SELECT * INTO tableTemp FROM yourTable WHERE YEAR(dateColumn) = YEAR(CURRENT_TIMESTAMP) AND MONTH(dateColumn) IN(1,3,6,9,12)
    TRUNCATE yourTable;
    INSERT INTO yourTable SELECT * FROM tableTemp
    As you mentioned, you need to do the deletion in Stored Procedure(SP). Can you post your table DDL with sample data and specify your requirement details so that I can help to compose your SP.
    If you have any question, feel free to let me know.
    Best regards,
    Eric Zhang

  • How to convert MS SQL Server data to XML data using Java

    Hi all!
    How do I generate XML document for SQL Server data using java / jsp.
    Thanks in advance

    http://www.fdsapi.com

  • Migrating from Sql Server tables with column name starting with integer

    hi,
    i'm trying to migrate a database from sqlserver but there are a lot of tables with column names starting with integer ex: *8420_SubsStatusPolicy*
    i want to make an offline migration so when i create the scripts these column are created with the same name.
    can we create rules, so when a column like this is going to be migrated, to append a character in front of it?
    when i use Copy to Oracle option it renames it by default to A8420_SubsStatusPolicy
    Edited by: user8999602 on Apr 20, 2012 1:05 PM

    Hi,
    Oracle doesn't allow object names to start with an integer. I'll check to see what happens during a migration about changing names as I haven't come across this before.
    Regards,
    Mike

  • How to select data from Sql server 2005 database tableinto oracle database table

    Hi,
    I have table text1 in sql server database and text2 in oracle database (11g). Now how to move data from SQL Server table into oracle table. So please help me how to do it.
    Thanks a lot in advance.
    rk
    OS: Windows 7 professional

    Hi,
    you can use export/import wizard and specify sql server as a source and oracle as destination.
    I hope this is helpful.
    Please Mark it as Answered if it answered your question
    OR mark it as Helpful if it help you to solve your problem
    Elmozamil Elamir Hamid
    MCSE Data Platform
    MCITP: SQL Server 2008 Administration/Development
    MCSA SQL Server 2012
    MCTS: SQL Server Administration/Development
    MyBlog

  • How to connect to SQL Server from Forms 10g?

    Hello all,
    How do we connect to SQL Server database from Forms 10g?
    In Oracle Metalink site they have suggested using Transparent Gateway for SQL Server as a solution.
    But is there a way we can connect directly to SQL Server from Forms using an ODBC connection
    without installing anything on the database server?
    Pls help!
    Regards,
    Sam

    Hello all,
    I was able to connect to SQL Server from Oracle using Generic Connectivity (HSODBC).
    Transparent gateway was not required.
    Followed metalink note 109730.1
    So, after I created a dblink to SQL Server, I created a synonym for the dblinked SQL Server table.
    When I used the synonym as the table source in Oracle Forms, I got the following error while querying.
    "ORA-02070 - ROWID is not supported in this context."
    This is because Forms has an invisible ROWID field and when data is fetched from SQL Server table
    no Rowid is fetched since SQL Server table doesn't have one.
    Is there a way to overcome this issue or do we have populate the block manually using a SQL query ?
    Pls suggest.
    Regards,
    Sam

Maybe you are looking for

  • Flash Player problems

    I have problem with flash player the games  I play tells me I hav´nt flash player 10 or more I have installed flash 11,1 something but  its dont work.. and where can I clearing my cache on adope?

  • J2sdk 1.4.1 Subject.doAs() odd (?) behaviour.

    I browsed through the forums and it seems like several other people have noticed the same, but nobody has explained it. See threads: http://forum.java.sun.com/thread.jsp?forum=61&thread=302862 http://forum.java.sun.com/thread.jsp?forum=61&thread=3190

  • Change from working offline to online

    I keep getting the message that I'm working offline how can I change this to online. I can't print from adobe reader, or word.

  • Siri on iPhone 4?

    Is it possible so me to somehow get Siri on my iPhone 4 iOS 5.0.1?

  • Facial Recognition per se for Thumbnails

    I have about 500 images of people for an online directory and also for MS Outlook 2010 contact images. I need to generate images 95 x 95 pixels of only their faces. Most images vary in size so the standard crop does not work buy using guides and hard