Keep value of autonumber column when importing into Microsoft Access database - sql

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).

Related

Unable to import/link table from SQL Server into access. Too many indexes error

I am trying to import a table from SQL into access but I am getting too many indexes in the table error. The table in SQL is indexed to several tables and unfortunately I don't have the rights to modify or change the table anyway. just have read access in the DB. I am trying to import/link the table but I am unable to do so due to too many indexes error being thrown.
Is it possible to only import/link the data and not indexes? I know access has a limit of 32 indexes. I have cleared auto indexes in options too, but I still get the error.
Is there a solution as to how I can import/link this table in access?
Thanks in advance
I did some digging on how to avoid this problem. So the problem is we can't directly link to the SQL database if the table has a lot of indexes and Access will throw an error "too many indexes on the table you are trying to import".
one way to beat this is by putting together a "Pass-through query". Lot of people suggested VBA code. I am not a coder and could not get it to work effectively. However, Access gives you the capability to build it with out VBA and the solution I found was in Microsoft website.
The steps are as follows: you might have to work around it for 2010 due to different naming convention when compared to 2007.
On the Create tab, click Query Design in the Other group.
Click Close in the Show Table dialog box without adding any tables or queries.
Save the query. Open the Query in design mode
On the Design tab, click Pass-Through in the Query Type workgroup.
Click Property Sheet in the Show/Hide workgroup to display the property sheet for the query.
In the query property sheet, place the mouse pointer in the ODBC Connect Str property, and then, click the Build (...) button.
With the ODBC Connect Str property, you specify information about the database to which you want to connect. You can type the connection information, or click Build, and then enter information about the server to which you are connecting.
When you are prompted to save the password in the connection string, click Yes if you want the password and logon name to be stored in the connection string information.
If the query is not the type that returns records, set the ReturnsRecords property to No.
In the SQL Pass-Through Query window, type your pass-through query. For example, the following pass-through query uses the Microsoft SQL Server TOP operator in the SELECT statement to return only the first 25 orders in the Orders table from the sample Northwind database:
SELECT TOP 25 orderid from orders
To run the query, click Run in the Results group on the Design tab. For a SQL pass-through query that returns records, click Datasheet view on the status bar.
If necessary, Microsoft Access prompts you for information about your server database.
This worked for me. If some one is having the same problem you can use these steps.

Import Excel Spreadsheet Data to an EXISTING sql table?

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

Modify data in MSAccess before passing on to SQL Table

I have set up an Access database with a linked table to an SQL table. I've created a query in Access off the linked table. I have shipping software that i've set up to export data to the Access query and it successfully gets passed on to the SQL Table. So far, so good.
Now to the issue. The "weight" column in the SQL table has an "implied" 4 decimal places. So when I export a weight of "1", it shows up in the SQL table as ".0001". I need to multiply the weight by 10000 in order to get it to show up as "1.0000" in the SQL table.
I've tried modifying the Query's SELECT statement to do the math on that column but I get a "Cannot Update Field" error out of the ODBC driver.
Is there a way to make this happen? I'm not using MS Access at all (other than to create the .mdb file). Just using the .mdb file as a bridge between the shipping application and the SQL table.
Thanks for any guidance!
Look at this answer which looks very similar to what you are looking for.
https://stackoverflow.com/a/11296097/886591

Generate Scripts of Certain Columns from a table in SQL Server 2008 management studio

I have a SQL Server 2008 database. This database has a table with 6 columns in it. I want to generate the insert scripts associated with the data only. I know how to do this part with SQL Server management studio via the "Advanced" dialog in the "Generate Scripts..." wizard. My challenge is, I only want to script 5 of the 6 columns. How do I do this?
Check out the FREE SSMS Tools Pack v2.0 - it has among a lot of other things a feature to generate INSERT statements for data in your tables, and yes - you can pick for which columns that INSERT statement should be:
SELECT Column1,Column2,Column3,Column4,Column5
INTO new table
FROM your Existing table;
This will create a new table with the columns that you specified, then the data will be inserted in the new created table. Then script the new table that have only the five columns that you want.
I don't think there's a way to do this. After you generate the full table script, you can do one of a few things.
If you want to preserve the actual field (keep it there), then just do something like:
update yourTable
set yourColumnThatYouDidntWantDataFor = null
Or, if you didn't want that field to be in the table at all, you can just drop it like so:
alter table yourTable
drop column yourColumnThatYouNoLongerWant

SQL Server 2008 : Cannot Insert new column in the middle position and change data type

My OS is Windows server 2008.
I've already installed SQL Server Express 2008.
I have several problems:
I can't insert a new column in the middle position. If I insert in the last one, I can save the table design.
I can change the column name but I can't change the data type.
I got error message :
Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be recreated or enabled the option Prevent saving changes that require the table to be re-created.
Example:
I have ID, Name, Phone, and Status columns. I am unable to add Address between Name and Phone.
But, I can add Address if i place it after Status.
Is there any way to solve this problem?
Thanks before.
In SSMS Tools -> Options -> Designers you would need to uncheck the option "Prevent Saving Changes that require table re-creation" to allow you to do this in SSMS.
This will rebuild the table and so generally isn't worth the hassle if the table is at all large and will make deployment to production trickier.
If there are columns which logically you would prefer to have next to each other to make writing queries easier you can create a View with the desired column order.
Column order doesn't matter either in the designer or in sys.columns.
The on disk storage will be the same regardless: Inside the Storage Engine - Anatomy of a record.
There is no performance benefit either.
I think using query it's not possible, but you do using UI of SSMS. right click on selected table and Insert Column whenever you want.
Think it does not matter columns order.
If you want a script to do this, all you need to do is select the data out into a temporary table, drop the table, recreate it with the columns in your preferred order and then reinsert the data from the temporary table in the right order.