Download Using SQL Server data

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Import or link Access to SQL Server data
Applies to: Microsoft Access 2010
If your department or workgroup uses Microsoft SQL Server to store data, you might have to work with
some SQL Server data in Access.
You can bring data from SQL Server objects (tables or views) into Access in either of two ways — by
importing, or by linking. The difference between the two processes is as follows:


When you import the data, Access creates a copy of the SQL Server data and any later changes that are
made to the data in your Access database are not reflected in the SQL Server database. Likewise, any later
changes made in the SQL Server table or view are not reflected in Access.
When you link to the SQL Server data, you are connecting directly to the source data so any later changes
that are made to data in Access are reflected in the SQL Server, and vice versa.
This article describes how to either import or link to SQL Server data.
Decide whether to import or to link
SITUATIONS WHEN IMPORTING IS SUITABLE
Typically, you import SQL Server data to an Access database for these reasons:


To permanently move SQL Server data to an Access database because you no longer need the data in the
SQL Server database. After you import the data into Access, you can delete the data from the SQL Server
database.
Your department or workgroup uses Access, but you are occasionally pointed to a SQL Server database
for additional data that must be merged into one of your Access databases.
Since importing SQL Server data creates a copy of the data in your Access database, during the import
process, you specify the tables or views that you want copied.
SITUATIONS WHEN LINKING IS SUITABLE
Typically, you link to SQL Server data for these reasons:



To connect directly to the source data to be able to view and edit the latest information both in the SQL
Server database and in your Access database.
The SQL Server database contains many large tables, and you are not be able to import them all into a
single .accdb file. The maximum size of an Access database is 2 gigabytes, minus the space needed for
system objects.
You want to run queries and generate reports based on data from the SQL Server without making a copy
of the data, consider linking to the SQL Server.



Your department or workgroup uses Access for reporting and querying and uses SQL Server for data
storage. Individual teams can create SQL Server tables and views for centralized storage, but often this
data must be brought into desktop programs for aggregation and reporting. Linking is the appropriate
choice, because it allows users of both the SQL Server database and the Access database to add and
update data, and to always view and work with the latest data.
You are an Access user who recently started using SQL Server. You migrated several of your databases to
SQL Server, and most of the tables in these databases are linked tables. From now on, instead of creating
Access tables, you will create tables and views in SQL Server and then link to them from your Access
databases.
You want to continue storing your data in SQL Server, but you also want to work with the most recent
data inside Access in order to run queries and print reports that you designed in Access.
Import data from SQL Server
PREPARE TO IMPORT
During the import operation, Access creates a table and then copies the data from the SQL Server
database into that table. At the end of the import operation, you can choose to save the details of the
import operation as a specification.
NOTE
An import specification helps you to repeat the import operation in the future without having to
step through the Import Wizard each time.
1.
Locate the SQL Server database that contains the data that you want import. Contact the administrator of
the database for connection information.
2.
Identify the tables or views that you want to import. You can import multiple objects in a single import
operation.
3.
Review the source data and keep the following considerations in mind:

Access does not support more than 255 fields in a table, so Access imports only the first 255 columns.

The maximum size of an Access database is 2 gigabytes, minus the space needed for system objects. If the
SQL Server database contains many large tables, you might not be able to import them all into a single
.accdb file. In this case, you might want to consider linking the data to your Access database instead.

Access does not automatically create relationships between related tables at the end of an import
operation. You must manually create the relationships between the various new and existing tables by
using the options in the Relationships window. To display the Relationships window:

Click the File tab, and then on the Info tab, click Relationships.
4.
Identify the Access database into which you want to import the SQL Server data.
Ensure that you have the necessary permissions to add data to the Access database. If you don't
want to store the data in any of your existing databases, create a blank database by clicking
the Filetab, and then on theNew tab, click Blank Database.
©2009 Microsoft Corporation.
5.
Review the tables, if any exist, in the Access database.
The import operation creates a table with the same name as the SQL Server object. If that name is
already in use, Access appends "1" to the new table name — for example, Contacts1. (If Contacts1
is also already in use, Access will create Contacts2, and so on.)
NOTE
Access never overwrites a table in the database as part of an import operation, and you
cannot append SQL Server data to an existing table.
IMPORT THE DATA
1.
Open the destination database.
On the External Data tab, in the Import & Link group, click ODBC Database.
2.
Click Import the source data into a new table in the current database, and then click OK.
3.
In the Select Data Source dialog box, if the .dsn file that you want to use already exists, click the file in
the list.
I need to create a new .dsn file
NOTE
1.
The steps in this procedure might vary slightly for you, depending on the software that is
installed on your computer.
Click New to create a new data source name (DSN).
The Create New Data Source Wizard starts.
2.
In the wizard, select SQL Server in the list of drivers, and then click Next.
3.
Type a name for the .dsn file, or click Browse to save the file to a different location.
NOTE
4.
You must have write permissions to the folder to save the .dsn file.
Click Next, review the summary information, and then click Finish to complete the wizard.
Create a New Data Source to SQL Server dialog box appears.
5.
Type a description of the data source in the Description box. This step is optional.
6.
Under Which SQL Server do you want to connect to, in the Server box, type or select the name
of the SQL Server to which you want to connect, and then click Next to continue.
7.
You might require information from the SQL Server database administrator, such as whether to use
Microsoft Windows NT authentication or SQL Server authentication. Click Next to continue.
8.
If you want to connect to a specific database, ensure that the Change the default database
to check box is selected. Then select the database that you want to work with, and then click Next.
9.
Click Finish.
10. Review the summary information and click Test Data Source.
©2009 Microsoft Corporation.
11. Review the test results, and then click OK to close the dialog box.
If the test was successful, click OK again, or click Cancel to change your settings.
4.
Click OK to close the Select Data Source dialog box.
Access displays the Import Objects dialog box.
5.
Under Tables, click each table or view that you want to import, and then click OK.
6.
If the Select Unique Record Identifier dialog box appears, Access was unable to determine which field or
fields uniquely identify each row of a particular object. In this case, select the field or combination of fields
that is unique for each row, and then click OK. If you are not sure, check with the SQL Server database
administrator.
Access imports the data. If you plan to repeat the import operation later, you can save the import
steps as an import specification and easily rerun the same import steps later. You require Microsoft
Office Outlook installed on your computer to create a task.
7.
Click Close under Save Import Steps in the Get External Data - ODBC Database dialog box. Access
finishes the import operation and displays the new table or tables in the Navigation Pane.
If you want to save the import as a task for reuse, continue to the next section.
SAVE AND USE THE IMPORT SETTINGS
NOTE
You must have Microsoft Office Outlook installed to create a task.
1.
Under Save Import Steps in the Get External Data - ODBC Database dialog box, select the Save
import steps check box. A set of additional controls appears.
2.
In the Save as box, type a name for the import specification.
3.
Type a description in the Description box. This step is optional.
4.
To perform the operation at fixed intervals (such as weekly or monthly), select the Create Outlook
Task check box. This creates a task in Microsoft Outlook 2010 that lets you run the specification.
5.
Click Save Import.
If Outlook is not installed, Access displays an error message when you click Save Import.
NOTE
If Outlook 2010 is not configured properly, the Microsoft Outlook 2010 Startup wizard starts.
Follow the instructions in the wizard to configure Outlook.
Optionally, you can create an Outlook task. It can be useful to create a task in Outlook if you want to run
the import operation at regular or recurring intervals. However, if you do not create a task, Access still
saves the specification.
©2009 Microsoft Corporation.
CREATE AN OUTLOOK TASK
If you selected the Create Outlook Task check box, Access starts Office Outlook 2010 and displays a new
task. Follow these steps to configure the task:
NOTE
If Outlook is not installed, Access displays an error message. If Outlook is not configured
properly, the Outlook Configuration Wizard starts. Follow the instructions in the wizard to configure
1.
Outlook.
Review and modify the task settings, such as the Start date,Due date and Reminder.
To make the import task a recurring event, click Recurrence and fill in the appropriate information.
2.
Click Save and Close.
RUN A SAVED TASK
1.
In the Outlook Navigation Pane, click Tasks, and then double-click the task that you want to run.
2.
On the Task tab, in the Microsoft Access group, click Run Import.
3.
Switch back to the Access window, and then press F5 to refresh the Navigation Pane.
4.
Double-click the imported table to open it in Datasheet view.
5.
Ensure that all of the fields and records were imported, and that there are no errors.
6.
Right-click the imported table in the Navigation Pane, and then click Design View on the shortcut menu.
Review the field data types and other field properties.
Link to SQL Server data
Since data is stored in tables, when you link to a table or view in a SQL Server database, Access creates a
new table (often known as a linked table) that reflects the structure and contents of the source object. You
can change data either in SQL Server, or in Datasheet view or Form view from Access. The changes that
you make to data in one location are reflected in the other. However, if you want to make structural
changes, such as removing or changing a column, you must do so from the SQL Server database, or from
an Access project that is connected to that database. You cannot add, delete, or change the fields in a
linked table while you are working in Access.
PREPARE TO LINK
1.
Locate the SQL Server database that has the data to which you want to link. Contact the database
administrator for connection information.
2.
Identify the tables and views to which you want to link. You can link to multiple objects in a single linking
operation.
3.
Review the source data and keep the following considerations in mind:
©2009 Microsoft Corporation.

Access does not support more than 255 fields in a table, so the linked table will include only the first 255
fields of the object you link to.

The columns that are read-only in a SQL Server object will continue to be read-only in Access.

You will not be able to add, delete, or modify columns in the linked table in Access.
4.
Identify the Access database in which you want to create the linked tables. Ensure that you have the
necessary permissions to add data to the database. If you don't want to store the data in any of your
existing databases, create a new, blank database by clicking the Filetab, and then on the New tab,
clicking Blank Database.
5.
Review the tables in the Access database. When you link to a SQL Server table or view, Access creates a
linked table with the same name as the source object. If that name is already in use, Access will append
"1" to the new linked table name — for example, Contacts1. (If Contacts1 is also already in use, Access will
create Contacts2, and so on.)
6.
To link to the data, open the destination database.
7.
On the External Data tab, in the Import & Link group, click ODBC Database.
8.
Click Link to the data source by creating a linked table, and then click OK.
9.
In the Select Data Source dialog box, click the .dsn file that you want to use, or click New to create a new
data source name (DSN).
10. In the Select Data Source dialog box, if the .dsn file you want to use already exists, click the file in the list.
I need to create a new .dsn file
NOTE
1.
The steps in this procedure might vary slightly for you, depending on the software that is
installed on your computer.
Click New to create a new data source name (DSN).
The Create New Data Source Wizard starts.
2.
In the wizard, select SQL Server in the list of drivers, and then click Next.
3.
Type a name for the .dsn file, or click Browse to save the file to a different location.
NOTE
4.
You must have write permissions to the folder to save the .dsn file.
Click Next, review the summary information, and then click Finish to complete the Create New
Data Source Wizard.
The Create a New Data Source to SQL Server Wizard starts.
5.
In the wizard, type a description of the data source in the Description box. This step is optional.
6.
Under Which SQL Server do you want to connect to, in the Server box, type or select the name
of the SQL Server computer to which you want to connect, and then click Next to continue.
©2009 Microsoft Corporation.
7.
On this page of the wizard, you might need to get information from the SQL Server database
administrator, such as whether to use Windows NT authentication or SQL Server authentication.
Click Next to continue.
8.
On the next page of the wizard, you might need to get more information from the SQL Server
database administrator. If you want to connect to a specific database, ensure that the Change the
default database to check box is selected, select the SQL Server database that you want to work
with, and then click Next.
9.
Click Finish. Review the summary information, and then click Test Data Source.
10. Review the test results, and then click OK to close the SQL Server ODBC Data Source Test dialog
box.
If the test was successful, click OK again to complete the wizard, or click Cancel to return to the wizard
and change your settings.
1.
Click OK.
Access displays the Link Tables dialog box.
2.
Under Tables, click each table or view that you want to link to, and then click OK.
1.
If the Select Unique Record Identifier dialog box appears, Access was unable to determine which
field or fields uniquely identify each row of the source data. In this case, select the field or
combination of fields that is unique for each row, and then click OK. If you are not sure, check with
the SQL Server database administrator.
Access finishes the linking operation and displays the new linked table or tables in the Navigation Pane.
IMPORTANT Each time you open either a linked table or the source object, you see the latest data
displayed in it. However, structural changes made to a SQL Server object are not automatically reflected in
a linked table.
UPDATE A LINKED TABLE BY APPLYING THE LATEST SQL SERVER OBJECT STRUCTURE
1.
Right-click the table in the Navigation Pane, and then click Linked Table Manager on the shortcut menu.
2.
Select the check box next to each linked table that you want to update, or click Select All to select all of
the linked tables.
3.
Click OK.
If the update is successful, Access displays a message to that effect. Otherwise, Access displays an
message.
4.
Click Close to close the Linked Table Manager.
©2009 Microsoft Corporation.
How Access sees SQL Server data types
Because Access data types differ from SQL Server data types, Access must determine the most
appropriate Access data type to use for each column of each SQL Server table or view that you import or
link to. For example, a SQL Server column of the data type bit is imported or linked into Access with the
data type Yes/No. Another example, a SQL Server column of the data type nvarchar(255) (or smaller) is
imported or linked with the data type Text, but a column of the data type nvarchar(256) (or larger) is
imported as an Access Memo field. After completing an import or linking operation, you can open the
table in Design view and confirm which data types Access assigned to its fields. You can change data
types of fields in imported tables. However, you cannot change data types of fields in linked tables, except
by changing them in the SQL Server database itself or in an Access project that is connected to that
database.
The following table lists the main SQL Server data types. The second and third columns show how Access
interprets each type.
SQL SERVER DATA TYPE
ACCESS
ACCESS FIELD SIZE
DATA TYPE
bigint
Text
255
binary(field size)
Binary
Same as SQL Server field size
bit
Yes/No
char(field size), where is less
Text
Same as SQL Server field size
than or equal to 255
char(field size), where is greater
Memo
than 255
datetime
Date/Time
decimal(precision,scale)
Number
Decimal (Access Precision and Scaleproperties
match SQL Server precision and scale.)
float
Number
image
OLE Object
int
Number
©2009 Microsoft Corporation.
Double
Long Integer
money
Currency
nchar(field size), where is less
Text
Same as SQL Server field size
than or equal to 255
nchar(field size), where is
Memo
greater than 255
ntext
Memo
numeric(precision,scale)
Number
Decimal (Access Precision and Scaleproperties
match SQL Server precision and scale.)
nvarchar(field size), where is
Text
Same as SQL Server field size
less than or equal to 255
nvarchar(field size), where is
Memo
greater than 255
nvarchar(MAX)
Memo
real
Number
smalldatetime
Date/Time
smallint
Number
smallmoney
Currency
sql_variant
Text
text
Memo
timestamp
Binary
8
tinyint
Number
Byte
uniqueidentifier
Number
Replication ID
varbinary
Binary
Same as SQL Server field size
©2009 Microsoft Corporation.
Single
Integer
255
varbinary(MAX)
OLE Object
varchar(field size), where is less
Text
than or equal to 255
varchar(field size), where is
Memo
greater than 255
varchar(MAX)
Memo
xml
Memo
©2009 Microsoft Corporation.
Same as SQL Server field size