Import selected columns from a CSV files to SQL Server table - sql

I am trying to import data from a CSV file to a SQL Server 2008 table. Data upload is working, but I want to import only selected columns, not all, and add them to a a new table, of same no. of columns, using the wizard, but its not happening, the wizard is selecting all the columns.
So is it possible using wizard that I only import selected columns.

If you are using the Import / Export wizard, when you get to Select Source Tables and Views click on the button "Edit Mappings" on the bottom left of the screen. That opens column mappings screen; on the destination column select Ignore to remove the column you don't want to import.

Since most people searching this question in 2019 aren't going to be using SQL2008, I'll add this for SQL Server 2016:
When you choose your Flat File data source, there is an Advanced option to the left of the window. Click that.
You will see a list of field names, and just below that there are 3 buttons. One of them is Delete.
Highlight the field you do not want to include and click the Delete button. Do this for all fields you do not wish to import.

First Use same data type which declare in choose data source file. After choosing "destination file" go to edit mapping, and select "edit sql" and take only selected column and declare the same data type. edit mapping select Ignore which column you don't want and declare same data type.

Related

How to import data from the 2nd sheet of an excel file, to SQL database

I have an Excel file that has several sheets, I want to import the data from the 2nd sheet but only the first 2 columns. I use SQL Server Management Studio. How can I do this?
Use the import data tool that comes with SQL Server Management Studio, in the wizard you will get to a section where you can map the sheet to a table. You can select the drop down by the sheet and select the second sheet and then map this to a table. Next you will click edit mappings on the bottom right and select only the two columns you would like.

How to connect an Excel file to an Access Database

I have created a query in an Access database and exported the query result into an Excel file. Now, I want to connect that Excel file to the Access database (or to that query) so that whenever some fields are updated in database, those changes would be automatically updated in the exported Excel file (report). What would be the best way to do it?
Thanks
You can create a link between Excel and an Access table or query by going to the Data tab on Excel and clicking the "From Access" icon.
Alternatively if there is a trigger inside access that you can use to update data (or if you don't need real time updates just hourly or daily ones), you could have access programmatically re-export.
If you want to work with data in Access, but still maintain the data in Excel, you need to link to the data rather than import it. Follow these steps:
Create a blank database or open an existing file in Access.
Select File, Get External Data, Link Tables.
Select Microsoft Excel as the file type.
Select a worksheet or named range to import, and then
click Next. You can import only one worksheet or named range at a
time, and each one will become an Access table.
In the next dialog box, select or deselect the check box First Row
Contains Column Headings, depending on whether your worksheet has
headings. Then click Next.
Enter a name for the table (or accept the default name that Access
suggests), click Finish, and click OK.
Now you have an Access table that looks almost exactly like the imported table. The advantage is that it maintains a live link to the Excel worksheet and can be edited in either application.

Import SQL records from file into sql DB and changing type

I created a .txt file using BULK that contains all the records of a sql DB. Now I need to import these records into a new DB. The problem is that I need to change the type of some fields from DOUBLE to BIGINT or the records won't be added to the new DB.
Please, wich functions and how do I have to use them?
Thanks
Importing into SQL Server, you can right-click on the database and select Tasks > Import Data. This will open up the Import/Export Wizard which is a slimmed down version of the same functionality in SSIS.
Once the Wizard is open, you can navigate the through the prompts and eventually get to a screen, Select Source Data, where you can click on the Edit Mappings button and modify the data types of the data being imported.

Import SQL to SQL DB: How can I populate columns that exist in destination, not source?

I'm using SSIS to import data from one DB to another existing DB. Some columns in the destination tables do not exist in the source tables. Seems the Import & Export Wizard only allows me to select unmapped columns from the source and match them with these new columns in the destination. I'd like to be able to just provide one piece of data to import into all rows of these new columns.
Would like to use the GUI if possible because I'm not skilled at writing scripts. Thanks!
In SSIS, you can add a "derived column" component that will add columns to the buffer rows with the value you want (either a string or an expression).
I don't believe this is possible in the GUI. However, it would be a simple script after the data is loaded with SSIS:
UPDATE table SET newcolumn = new value
If you need to filter the rows, just add
WHERE column = value ...
You could change your source to a select query and list out the columns along with the static value you want to map.
SELECT SOURCECOLUMN_1,SOURCECOLUMN_2,....,SOURCECOLUMN_N,'VALUE' AS DESTINATIONCOLUMN FROM Source_Table
My original thought was that you could use the query right in the Import & Export wizard. you can obviously do alot more if you go in and edit the package, but it sounded like you didn't have much expereince with that. Here is how you would do this in the wizard.
After you have selected your source and destination databases you can Specify Table Copy or Query. Select the Write a query to specify the data to transfer option
On the next screen enter the query listing out all of the columns and add in your static columns.
On the Next screen You will need to select the Destination table or it will default to creating a new table named Query. You should be able to choose from the drop down. As long as you aliased your extra columns with the same names it should map correctly. You can go in and edit mappings here if needed.
You can then save off the SSIS package and it will source form the query.
Alternatively if you already have the SSIS pacakge created without the extra columns you can go in to the Data Flow and change the Data access mode in the OLE DB Source to be a SQL Command instead of a table or view. Add your query here.
You can then go into the properties of the OLE DB Desitination in the Dataflow and map the new column. You could also add in a derived column as #DominicGoulet by adding in a Dervied Column task and putting your static information here and then mapping. If you want to see that solution too let me know.

SSIS 2005: "Append rows to the destination table" is greyed out. Why?

In SQL Server 2005, Import Data (SSIS), my desire is to import a text file and have it append to an existing table. The first time through the wizard on the Column Mappings step I swear the Append rows to the destination table radio button was enabled. But, now, it's disabled (grey) and even re-starting the wizard won't cause it to re-enable.
Is there some secret/magic I don't know about? How do I get that option to re-enable so that I can append (rather than Create destination table)?
I know what you're missing... because I once missed it too!
From the screen, "Select Source Tables and Views",
there is a "destination" drop-down menu on the right side.
Select your table from this drop-down,
then click on the "Edit Mappings" button.
Now the append option will be enabled!
In my experience, if the table you're importing into doesn't exist at the time you're going through the "SSIS Import and Export Wizard", you won't have the option to Append; if the table does exist at the time you're going through the wizard, the option is available.
If you do choose the "Create table" option, the wizard just builds the SSIS package with an Execute SQL task that builds the table prior to the data flow that loads the data. So, you can always just remove the task in the resulting package and you'll get "Append" by default.
The name of the table must be preceded with schema name ("dbo."). Otherwise the wizard didn't find it in my case.