Import Excel Spreadsheet Data to an EXISTING sql table? - sql

I have a table called tblAccounts whose contents will come from an excel spreadsheet.
I am using MS SQL Server 2008 (x64) on a Windows 8.1 (x64)
I tried using the SQL Server Import/Export Wizard but there is no option to choose an existing table but only an option to create a new one.
I tried using other methods such as OPENROWSETS
INSERT INTO tblAccount SELECT * FROM OPENROWSET( 'Microsoft.Jet.OLEDB.4.0',
'Excel 12.0;Database=D:\exceloutp.xls','SELECT * FROM [Sheet1$]')
but gave me an error:
Msg 7308, Level 16, State 1, Line 1
OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.
Some research told me that it occurred because of a 64-bit instance of SQL server.
The problem is that this Excel data transfer to a SQL table must be accomplished using the SQL Import/Export Wizard only.
How can I import an Excel spreadsheet to an existing SQL table without creating a new one?
Some links I visited but was not able to help me resolve my problem:
How do I import an excel spreadsheet into SQL Server?
Fix OLE DB error

You can copy-paste data from en excel-sheet to an SQL-table by doing so:
Select the data in Excel and press Ctrl + C
In SQL Server Management Studio right click the table and choose Edit Top 200 Rows
Scroll to the bottom and select the entire empty row by clicking on the row header
Paste the data by pressing Ctrl + V
Note: Often tables have a first column which is an ID-column with an auto generated/incremented ID. When you paste your data it will start inserting the leftmost selected column in Excel into the leftmost column in SSMS thus inserting data into the ID-column. To avoid that keep an empty column at the leftmost part of your selection in order to skip that column in SSMS. That will result in SSMS inserting the default data which is the auto generated ID.
Furthermore you can skip other columns by having empty columns at the same ordinal positions in the Excel sheet selection as those columns to be skipped. That will make SSMS insert the default value (or NULL where no default value is specified).

Saudate, I ran across this looking for a different problem. You most definitely can use the Sql Server Import wizard to import data into a new table. Of course, you do not wish to leave that table in the database, so my suggesting is that you import into a new table, then script the data in query manager to insert into the existing table. You can add a line to drop the temp table created by the import wizard as the last step upon successful completion of the script.
I believe your original issue is in fact related to Sql Server 64 bit and is due to your having a 32 bit Excel and these drivers don't play well together. I did run into a very similar issue when first using 64 bit excel.

You can use import data with wizard and there you can choose destination table.
Run the wizard. In selecting source tables and views window you see two parts. Source and Destination.
Click on the field under Destination part to open the drop down and select you destination table and edit its mappings if needed.
EDIT
Merely typing the name of the table does not work. It appears that the name of the table must include the schema (dbo) and possibly brackets. Note the dropdown on the right hand side of the text field.

If you would like a software tool to do this, you might like to check out this step-by-step guide:
"How to Validate and Import Excel spreadsheet to SQL Server database"
http://leansoftware.net/forum/en-us/help/excel-database-tasks/worked-examples/how-to-import-excel-spreadsheet-to-sql-server-data.aspx

Related

SQL Server 2012 to Excel 2010 Copy and Paste

I am trying to copy and paste from SQL Server 2012 to Excel 2010.
My query has the 13th column that is delimiting that is ruining the results.
It has a long description field that gets cut off after a space or a certain amount of characters.
It doesn't matter how big or small my order might be or how dumb my questions are, I get great service quickly. My account manager constantly comes through for me, and is the reason all of my purchasing inquiries start with blah
Please help.
I have tried the following methods
Tried saving without any delimiting to the Excel FIle
Changing the Query Results to add Quote Strings contianing list separators when saving .csv or copying result.
Your problem is that you have already used TextToColumn in this instance of Excel. Close Excel. Restart Excel. Execute the query in (I assume you mean) Sql Server Management Studio (AKA SSMS). Left-Click the square in the top left of the results to select all the rows in the query results windows. Then Right-click and select Copy With Headers. Then go back to Excel, select cell A1 and paste. The headers should take care of the column splitting.
Hth,
Ojf
Check if you are able to Export data using DTS Import Export Wizard of SQL SERVER 2012.
Try saving in both Excel(.xls) and Comma Separted file(.csv).

SQL Server 2008 R2 Data Export Problems

I am trying to export data from my production DB to my development DB but I am getting this error:
Messages Error 0xc0202049: Data Flow Task 1: Failure inserting into the
read-only column "id". (SQL Server Import and Export Wizard)
Is there a way to check which column is that because I have 20 tables that all of them use the column name id or at least get a better error report?
Just click on Edit mappings when you choose tables to import from and to. and check on the Enable identity insert checkbox. And you should do that for each choosen table. Then you can complete your import. It was helped me in the same situation.
If the column ID is an identity column, it's likely set up to generate automatically when a row is inserted, and is not able to be directly written to.
To preserve linking from the production environment to the test, use:
SET Identity_Insert <TableName> ON
To allow you to write identity values.
All I had to do was:
Right-click on the column (in this case 'ID')
Select Modify
Inside the Column Properties scroll down until you see Identity Specification
Expand the view and select NO from the drop down menu.
If anyone knows a different (faster) way of doing this please share
Sounds to me as though you might be trying to insert values into a column that has been declared as an IDENTITY field.
Well, the answer is a bit late but I'm posting it anyway for the sake of providing to community.
I assume that you are trying to copy tables (not partial data). My answer is based on this assumption.
Connect to the source db via Object Explorer in SSMS.
Right click the db that you want its tables to be copied.
Go to 'Tasks' --> 'Export'.
Choose your source and destination server and if you don't want additional script to be run before hands just press next ('Copy data from one or more tables or views'). Now you may select tables you want to be copied to the destination.
Select tables that you want to be completely copied to the destination. And click on 'edit mappings'. Here you may 'enable identity insert'. You will also have other options so check the screen before pressing OK.
If you do not use an express edition of SSMS here you may create an SSIS package to create jobs and to automatize this process in the future. You may check 'run immediately' and follow the screen about how it goes with all the warnings and errors. Hopefully you will get none. Good luck!
Tested with:
(
Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
Jun 17 2011 00:57:23
Copyright (c) Microsoft Corporation
Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
)

Add Description From Excel File to Columns description in SQL

I have a DB (in SQL Server 2008 SP3) include many tables and each tables have a lot of columns, in other hand I have an Excel file include description for any of my DB's columns (In SQL Server Management Studio when you want create a table, in design mode, for each column in property window there is a description field), So I am interesting in add this(Excel) description to description of columns in DB (of course not by hand and one by one), does any one have any idea about this?
Knock up some VBA to iterate through the Excel data, calling the database for each table/column.
The SQL Server stored procedure you want (if I understand correctly) is sp_addextendedproperty to add comments about objects in SQL Server
You can get help from some references given below: please have a look over there, thanks
a. Reading and Writing Excel
b. Reading Excel and then save to your DB

How do I export a table's data into INSERT statements?

How can I export a table from a SQL Server 2000 database to a .sql file as a bunch of INSERT INTO statements?
One of the fields in the table is a Text datatype and holds HTML so doing this by hand would be rather time-consuming.
I have access to SQL Server Management Studio 2008 to access the SQL Server 2000 database.
Updating since this Q&A was at the top of the search results when I was looking for the answer.
In MSSQL 2008 R2:
Right Click on database: Tasks -> Generate Scripts...
The Generate and Publish Scripts dialog will pop up. The Intro page is worthless. Click "Next"
Choose "Select Specific database objects" and then select the Table(s) you want to get Inserts for. Click Next and the dialog will advance to the "Set Scripting Options".
Click on Advanced and you should see:
Scroll down the list of Options until you find "Types of data to script". Click on that row and choose "Data Only" from the pull-down. Click "OK". Choose your Save options and click "Next" a few times.
Note - The output also includes the following after every 100 inserts.
GO
print 'Processed 200 total records'
Check out the SSMS Tool Pack - it's a great, FREE add-on for SQL Server Management Studio which does a lot of things - among other it can generate INSERT statements from a given table.
I have been using this stored procedure for a long time: sp_generate_inserts: the 2000 version and the 2005 (and up) version.
You use it like this:
sp_generate_inserts 'thetablename'
or if you want to filter:
sp_generate_inserts 'thetablename', #from='from ... where ... order by ...'
The sp will return inserts statements as query results. Don't forget to modify setting: increase the maximum number of characters displayed in each column (tools - options - query results).
If you can use other DB management apps the quickest way would be using a tool like SqlDbx which has a built-in "Export as inserts (SQL)" function (just execute a query like SELECT * FROM Table and then use the contextual menu from the result grid).
If you need to stick to SQL Management Studio then you could use a stored procedure like this one:
http://vyaskn.tripod.com/code/generate_inserts.txt
It generates a set of results with the SQL INSERT statement for each row of the target table. Then you can exports the results to a file, or just copy them to the clipboard and paste in the query window (it works fine even with several megabytes of data).

Keep value of autonumber column when importing into Microsoft Access database

What I try is to import several tables programmatically from Microsoft SQL Server to Microsoft Access.
Each SQL Server table has an identity column, and the corresponding Access tables, an autonumber column, too.
Now I want to generate SQL scripts to copy the data from SQL Server to Access and have the autonumber colum the same value as in SQL server.
Is this possible?
When doing it the other way from Access to SQL Server, it is rather easy by using SET IDENTITY_INSERT [MyTable] ON and later SET IDENTITY_INSERT [MyTable] OFF.
I found out that there is no such statement for Microsoft Access.
In addition I tried to create the Access tables to import into first with the identity field as type LONG and later use the ALTER TABLE ... ALTER COLUMN statement to switch to autonumber. I failed in doing so.
So my question: Is there any way to achieve my goal?
If you use Insert Into and specify all column names in MS Access, it should work.
I just created a table with the following structure
Id (autonumber)
Firstname (text)
Secondname (text)
Lastname (text)
I ran this statement
docmd.RunSQL "insert into table2 (id, firstname, secondname, lastname) values (27, 'a', 'b', 'c')"
It worked and inserted 27 into the autonumber column
Assuming you can see both the SQL Server tables (linked tables) and the MS Access tables while in the MS Access database, here is the procedure to do it without code. These instructions are for Access 2013, so while interface elements have moved, this should work for 2003, 2007, etc.
Your Access table into which you are importing should be free of any data.
Close all Access objects (tables, queries, reports, etc.)
Click Database Tools on the ribbon.
Optional: Click Compact and Repair (this sets the AutoNumber counters back to 0 on empty tables)
Click Create on the menu.
Click Query Design on the ribbon. (the Show Tables dialog is open)
Add the SQL Server source table to the query.
Close the Show Tables dialog.
Click the Append Query button on the ribbon.
Select the Access table into which you want to import data and click OK.
In the SQL Server table, double-click any fields that you want to import, or double-click the * field if all column names match and you want to import all fields.
For each field that is added to the append query below, check that all fields map to a field in the Access table. If not, in the Append To attribute of each field, select the field in Access that should receive the data from SQL Server.
Click the Run button on the ribbon.
Access will tell you how many records you are about to append - you can use this information to verify that you are getting all your data.
You can save this query if you think you'll need to use it again to sync the tables.
If you want to get fancy, you could create a delete query which deletes all the records in the access table, then create a macro which runs everything in this order:
Delete records
Compact and Repair Database
Run import from SQL Server
The secret is to temporarily un-assign the autonumber as the primary key. This allows Appending AutoNumber Field to your records without any trouble (just make sure you do not have any duplicates when appending, otherwise when you re-assign the primary key you will get an error).