bz.morvarid
bz.morvarid

Reputation: 177

import two columns from a text file into a sql table

I have a table in SQL Server with some columns and a text file. I need to import data of two columns of text file into SQL table (two columns exist in SQL table for do it and no need two insert columns). How can I do it?

Upvotes: 1

Views: 3220

Answers (2)

Yashveer Singh
Yashveer Singh

Reputation: 1977

SQL Server Management Studio (SSMS) provides the Import Wizard task which you can use to copy data from one data source to another. You can choose from a variety of source and destination data source types, select tables to copy or specify your own query to extract data, and save your work as an SSIS package. In this section we will go through the Import Wizard and import data from an Excel spreadsheet into a table in a SQL Server database.

https://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/

FOR CSV // THIS IS THE DATA IN THE CSV FILE

 Name,Class
 Prabhat,4
 Prabhat1,5
 Prabhat2,6

// end OF CSV FILE

THE QUERY

        CREATE TABLE CSVTest  (Name varchar(100) , class varchar(10)) 

        BULK
        INSERT CSVTest
        FROM 'C:\New folder (2)\testcsv.txt'
        WITH
        (
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n'
        )
        GO
        --Check the content of the table.
        SELECT *
        FROM CSVTest
        GO
        --Drop the table to clean up database.
        DROP TABLE CSVTest
        GO

Upvotes: 1

Melanie
Melanie

Reputation: 584

Use the SQL Server Importing Wizard and just ignore the columns in the mapping that are not required.

See link.

Upvotes: 1

Related Questions