ALTER EXTERNAL DATA SOURCE

ALTER EXTERNAL DATA SOURCE#

Changes the first location or the credential of an external data source backed by a File connection. When the new location would leave imported tables reading files outside the connection’s locations, the statement moves those tables to the new location.

In Querona, an external data source is a connection: the statement changes the connection of that name for every database on the server.

Syntax#

ALTER EXTERNAL DATA SOURCE data source name SET LOCATION = 'location' CREDENTIAL = credential name , each option at most once

Arguments#

data_source_name

The name of the external data source, which is the name of the connection. A name that matches no connection you can see raises error 15151.

LOCATION = ‘location‘

The new first location of the connection: an absolute path (E:\data), a UNC path (\\files\data) or a URI. It replaces the first location of the connection’s location list; the other locations and every other setting of the connection stay as they are. Empty entries of the list are removed, so the new location is also the first entry. A relative path or a bare file: location is refused. An empty or blank location is refused with error 46504, as CREATE EXTERNAL DATA SOURCE refuses it; SQL Server refuses an empty location the same way, but accepts a location of spaces.

CREDENTIAL = credential_name

The credential the data source authenticates with. The name must resolve to a database scoped credential in the current database or to a server-level credential, or the statement fails with error 46516. See CREATE CREDENTIAL for how the credential is used.

Each option can be given at most once, in either order; the comma between them is optional.

Remarks#

File connections only. The statement alters a connection of the File provider. Any other connection, including an Excel connection, is refused with:

External data source 'MyWorkbooks' is not a file-backed data source.

Imported tables follow the location. A table imported from a File connection stores the absolute path of its file. When SET LOCATION would leave tables under the old first location reading files outside the connection’s locations, the statement moves them: each file below the old first location is moved to the same relative path under the new location, and the connection and the moved tables are saved together. For example, after SET LOCATION = '\\files\data' on a connection whose first location was E:\data, a table reading E:\data\sales\2024.csv reads \\files\data\sales\2024.csv. Tables the new locations still cover are not changed, and the built-in tables of the builtin schema read the connection’s new locations without being moved.

When tables moved, the statement sends an informational message:

Tables moved from 'E:\data' to '\\files\data': 12.

It then checks the new location and sends any problem it finds as further informational messages, for example The location '\\files\data' does not exist or cannot be reached. Tables moved there: 12. These messages are warnings: the change is already saved. A missing file is named only for a table you have access to; the files of the other tables are counted. Under SET NOEXEC ON the statement saves nothing and sends none of these messages.

What is refused. The statement is refused, and nothing is saved, when the change leaves tables outside the connection’s locations that the move cannot carry. That happens, for example, when a local folder is replaced by a URL, because the files of a folder can only move to a local or UNC folder, or when a file is replaced by a folder or by a file with another extension. The refusal names the location and the tables you have access to and counts the others:

The locations of connection 'MyFiles' cannot be changed: tables would read files outside the connection's locations. Keep the locations these tables use, or move the tables to the new locations.
Tables that still use the location 'E:\data': 5. Tables: [Sales].[dbo].[orders_csv], [Sales].[dbo].[customers_csv]. Of these, tables you have no access to: 3.

To move tables under several locations at once, use the Admin Portal; see Moving a File or Excel connection to a new location.

Other effects of the first location. The first location is the one sys.external_data_sources reports in its location column and the one CREATE EXTERNAL TABLE AS SELECT writes under.

Credential checks. The data source’s credential, whether stated in this statement or already stored, is refused when no location of the data source could use it (local paths are read with the Querona service identity) or when it would be sent over plain http to a remote host. A statement that sets only LOCATION can therefore be refused because of the stored credential. See Authenticated reads from remote storage.

Tables already outside the locations. SET LOCATION moves only tables that read under the location it replaces. Tables that already read files outside the connection’s locations are not moved; see Moving a File or Excel connection to a new location for how to repair them.

Auditing. When auditing is enabled, every execution of this statement is recorded in the audit trail, including an attempt refused for a missing permission or rejected by validation. Each moved table, and each built-in table whose own copy of the location list is dropped, is recorded as an ALTER of that table. The recorded text, for example ALTER TABLE [Sales].[dbo].[orders_csv] /* location moved by connection 'MyFiles': 'E:\data\orders.csv' -> '\\files\data\orders.csv' */, describes the change; it is not a statement that runs.

Compared with SQL Server. SQL Server’s external data source belongs to one database, while a Querona connection serves the whole server, so moving its location can move tables in several databases, including databases you cannot see.

Permissions#

Requires the Alter connection access right (AlterConnection) on the connection. No permission on the moved tables is required: moving a connection’s data is a connection-level action.

Examples#

Move the first location of the MyFiles data source to a share; the tables that read under the old location move with it:

ALTER EXTERNAL DATA SOURCE MyFiles SET LOCATION = '\\files\data';

Point the LakeFiles data source at a file in another storage account and change its credential in one statement:

ALTER EXTERNAL DATA SOURCE LakeFiles
    SET LOCATION = 'https://lake2.example.com/exports/sales.csv', CREDENTIAL = Lake2Credential;

See also#