
## SQL Server package

URL: https://docs.atlan.com/apps/connectors/database/microsoft-sql-server/sdk/references/package-reference

> Programmatically crawl and manage Microsoft SQL Server assets in Atlan using the Python SDK (pyatlan). Reference for the SQL Server assets package.

:::warning[Deprecated—use the app reference]
This package is deprecated. Use the [app reference](app-reference) for the new `client.app` builder, or see [Manage apps](https://docs.atlan.com/llms/platform/python/manage-apps/llms.txt).
:::

# SQL Server assets package

The [SQL Server assets package](https://ask.atlan.com/hc/en-us/articles/6167939436945-How-to-crawl-Microsoft-SQL-Server)
crawls Microsoft SQL Server assets and publishes them to Atlan for discovery.

:::warning[Will create a new connection]
This should only be used to create the workflow the first time. Each time you run this
method it will create a new connection and new assets within that connection — which could
lead to duplicate assets if you run the workflow this way multiple times with the same settings.

Instead, when you want to re-crawl assets, re-run the existing workflow (see [Re-run existing workflow](#re-run-existing-workflow) below).
:::
To crawl assets directly from Microsoft SQL Server:

### Java

```java showLineNumbers title="Direct extraction from SQL Server"
Workflow crawler = SQLServerCrawler.creator( // (1)
 client, // (2)
 "production", // (3)
 List.of(client.getRoleCache().getIdForName("$admin")), // (4)
 null,
 null
 )
 .direct( // (5)
 "11.22.33.44",
 "WideWorldImporters"
 )
 .basicAuth( // (6)
 "username",
 "password"
 )
 .include( // (7)
 Map.of(
 "WideWorldImporters", 
 List.of(
 "Application",
 "Purchasing",
 "Sales",
 "Warehouse",
 "Website"
 )
 )
 )
 .exclude(Map.of()) // (8)
 .build() // (9)
 .toWorkflow(); // (10)

WorkflowResponse response = crawler.run(client); // (11)
```

1. The `SQLServerCrawler` package will create a workflow to crawl assets from SQL Server.
2. You must provide Atlan client.
3. You must provide a name for the connection that the SQL Server assets will exist within.
4. You must specify at **least one connection admin**, either:

 - everyone in a role (in this example, all `$admin` users).
 - a list of groups (names) that will be connection admins.
 - a list of users (names) that will be connection admins.

5. When crawling assets directly from SQL Server, you are required to provide the following information:

 - hostname of the SQL Server host
 - name of the database to extract

6. When using basic authentication, you are required to provide the following information:

 - username for accessing SQL Server.
 - password for accessing SQL Server.

7. You can also optionally specify the set of assets to include in crawling. For SQL Server assets, this should be specified as a map keyed by database name with values as a list of schemas within that database to crawl. (If set to null, all databases and schemas will be crawled.)
8. You can also optionally specify the set of assets to exclude from crawling. For SQL Server assets, this should be specified as a map keyed by database name with values as a list of schemas within the database to exclude. (If set to null, no assets will be excluded.)
9. Build the minimal package object.
10. Now, you can convert the package into a `Workflow` object.
11. You can then run the workflow using the `run()` method on the object you've created. Because this operation will execute work in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

 :::warning[Workflows run asynchronously]
Remember that workflows run asynchronously. See the [packages and workflows introduction](https://docs.atlan.com/llms/platform/python/packages/llms.txt) for details on how you can check the status and wait until the workflow has been completed.
 :::

### Python

```python showLineNumbers title="Direct extraction from SQL Server"
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.packages import SQLServerCrawler

client = AtlanClient()

crawler = (
 SQLServerCrawler( # (1)
 client=client, # (2)
 connection_name="production", # (3)
 admin_roles=[client.role_cache.get_id_for_name("$admin")], # (4)
 admin_groups=None,
 admin_users=None,
 )
 .direct(hostname="11.22.33.44", database="WideWorldImporters") # (5)
 .basic_auth( # (6)
 username="username",
 password="password"
 )
 .include( # (7)
 assets={
 "WideWorldImporters": [
 "Application",
 "Purchasing",
 "Sales",
 "Warehouse",
 "Website",
 ]
 }
 )
 .exclude(assets={}) # (8)
 .to_workflow() # (9)
)

response = client.workflow.run(crawler) # (10)
```

1. Base configuration for a new SQL Server crawler.
2. You must provide a client instance.
3. You must provide a name for the connection that the SQL Server assets will exist within.
4. You must specify at **least one connection admin**, either:

 - everyone in a role (in this example, all `$admin` users).
 - a list of groups (names) that will be connection admins.
 - a list of users (names) that will be connection admins.
5. When crawling assets directly from SQL Server,
you are required to provide the following information:

 - hostname of the SQL Server host
 - name of the database to extract
6. When using basic authentication,
you are required to provide the following information:

 - username for accessing SQL Server.
 - password for accessing SQL Server.
7. You can also optionally specify the set of assets to include in crawling. For SQL Server assets,
this should be specified as a dict keyed by database name with values as a list of schemas within that
database to crawl. (If set to None, all databases and schemas will be crawled.)
8. You can also optionally specify the set of assets to exclude from crawling.
For SQL Server assets, this should be specified as a dict keyed by database name with values
as a list of schemas within the database to exclude. (If set to None, no assets will be excluded.)
9. Now, you can convert the package into a `Workflow` object.
10. Run the workflow by invoking the `run()` method on the workflow client, passing the created object.

 :::warning[Workflows run asynchronously]
Remember that workflows run asynchronously. See the [packages and workflows introduction](https://docs.atlan.com/llms/platform/python/packages/llms.txt)
for details on how you can check the status and wait until the workflow has been completed.
 :::

### Kotlin

```kotlin showLineNumbers title="Direct extraction from SQL Server"
val crawler = SQLServerCrawler.creator( // (1)
 client, // (2)
 "production", // (3)
 listOf(client.roleCache.getIdForName("\$admin")), // (4)
 null,
 null
 )
 .direct( // (5)
 "11.22.33.44",
 "WideWorldImporters"
 )
 .basicAuth( // (6)
 "username",
 "password"
 )
 .include( // (7)
 mapOf(
 "WideWorldImporters" to listOf(
 "Application",
 "Purchasing",
 "Sales",
 "Warehouse",
 "Website"
 )
 )
 )
 .exclude(mapOf()) // (8)
 .build() // (9)
 .toWorkflow() // (10)

WorkflowResponse response = crawler.run(client) // (11)
```

1. The `SQLServerCrawler` package will create a workflow to crawl assets from SQL Server.
2. You must provide Atlan client.
3. You must provide a name for the connection that the SQL Server assets will exist within.
4. You must specify at **least one connection admin**, either:

 - everyone in a role (in this example, all `$admin` users).
 - a list of groups (names) that will be connection admins.
 - a list of users (names) that will be connection admins.

5. When crawling assets directly from SQL Server, you are required to provide the following information:

 - hostname of the SQL Server host
 - name of the database to extract

6. When using basic authentication, you are required to provide the following information:

 - username for accessing SQL Server.
 - password for accessing SQL Server.

7. You can also optionally specify the set of assets to include in crawling. For SQL Server assets, this should be specified as a map keyed by database name with values as a list of schemas within that database to crawl. (If set to null, all databases and schemas will be crawled.)
8. You can also optionally specify the set of assets to exclude from crawling. For SQL Server assets, this should be specified as a map keyed by database name with values as a list of schemas within the database to exclude. (If set to null, no assets will be excluded.)
9. Build the minimal package object.
10. Now, you can convert the package into a `Workflow` object.
11. You can then run the workflow using the `run()` method on the object you've created. Because this operation will execute work in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

 :::warning[Workflows run asynchronously]
Remember that workflows run asynchronously. See the [packages and workflows introduction](https://docs.atlan.com/llms/platform/python/packages/llms.txt) for details on how you can check the status and wait until the workflow has been completed.
 :::

### Raw REST API

:::tip[Create the workflow via UI only]
We recommend creating the workflow only via the UI. To rerun an existing workflow, see the steps below.
:::

## Re-run existing workflow

To re-run an existing workflow for SQL Server assets:

### Java

```java showLineNumbers title="Re-run existing SQL Server workflow"
List existing = WorkflowSearchRequest // (1)
 .findByType(client, SQLServerCrawler.PREFIX, 5); // (2)

// Determine which of the results is the
// SQL Server workflow you want to re-run...
WorkflowRunResponse response = existing.get(n).rerun(client); // (3)
```

1. You can search for existing workflows through the `WorkflowSearchRequest` class.
2. You can find workflows by their type using the `findByType()` helper method and providing the prefix for one of the packages. In this example, we do so for the `SQLServerCrawler`. (You can also specify the maximum number of resulting workflows you want to retrieve as results.)
3. Once you've found the workflow you want to re-run, you can simply call the `rerun()` helper method on the workflow search result. The `WorkflowRunResponse` is just a subtype of `WorkflowResponse` so has the same helper method to monitor progress of the workflow run. Because this operation will execute work in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

 - Optionally, you can use the `rerun(client, true)` method with idempotency to avoid re-running a workflow that is already in running or in a pending state. This will return details of the already running workflow if found, and by default, it is set to `false`

 :::warning[Workflows run asynchronously]
Remember that workflows run asynchronously. See the [packages and workflows introduction](https://docs.atlan.com/llms/platform/python/packages/llms.txt) for details on how you can check the status and wait until the workflow has been completed.
 :::

### Python

```python showLineNumbers title="Re-run existing SQL Server workflow"
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.enums import WorkflowPackage

client = AtlanClient()

existing = client.workflow.find_by_type( # (1)
 prefix=WorkflowPackage.MSSQL, max_results=5
)

# Determine which SQL Server workflow (n)

# from the list of results you want to re-run.

response = client.workflow.rerun(existing[n]) # (2)
```

1. You can find workflows by their type using the workflow client `find_by_type()`
method and providing the **prefix** for one of the packages.
In this example, we do so for the `SQLServerCrawler`. (You can also specify
the **maximum number of resulting workflows** you want to retrieve as results.)
2. Once you've found the workflow you want to re-run,
you can simply call the workflow client `rerun()` method.

 - Optionally, you can use `rerun(idempotent=True)` to avoid re-running a workflow that is already in running or in a pending state.
 This will return details of the already running workflow if found, and by default, it is set to `False`.

 :::warning[Workflows run asynchronously]
Remember that workflows run asynchronously. See the [packages and workflows introduction](https://docs.atlan.com/llms/platform/python/packages/llms.txt)
for details on how you can check the status and wait until the workflow has been completed.
 :::

### Kotlin

```kotlin showLineNumbers title="Re-run existing SQL Server workflow"
val existing = WorkflowSearchRequest // (1)
 .findByType(client, SQLServerCrawler.PREFIX, 5) // (2)

// Determine which of the results is the
// SQL Server workflow you want to re-run...
val response = existing.get(n).rerun(client) // (3)
```

1. You can search for existing workflows through the `WorkflowSearchRequest` class.
2. You can find workflows by their type using the `findByType()` helper method and providing the prefix for one of the packages. In this example, we do so for the `SQLServerCrawler`. (You can also specify the maximum number of resulting workflows you want to retrieve as results.)
3. Once you've found the workflow you want to re-run, you can simply call the `rerun()` helper method on the workflow search result. The `WorkflowRunResponse` is just a subtype of `WorkflowResponse` so has the same helper method to monitor progress of the workflow run. Because this operation will execute work in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

 - Optionally, you can use the `rerun(client, true)` method with idempotency to avoid re-running a workflow that is already in running or in a pending state. This will return details of the already running workflow if found, and by default, it is set to `false`

 :::warning[Workflows run asynchronously]
Remember that workflows run asynchronously. See the [packages and workflows introduction](https://docs.atlan.com/llms/platform/python/packages/llms.txt) for details on how you can check the status and wait until the workflow has been completed.
 :::

### Raw REST API

:::warning[Requires multiple steps through the raw REST API]
1. Find the existing workflow.
2. Send through the resulting re-run request.
:::
```json showLineNumbers title="POST /api/service/workflows/indexsearch"
{
 "from": 0,
 "size": 5,
 "query": {
 "bool": {
 "filter": [
 {
 "nested": {
 "path": "metadata",
 "query": {
 "prefix": {
 "metadata.name.keyword": {
 "value": "atlan-mssql" // (1)
 }
 }
 }
 }
 }
 ]
 }
 },
 "sort": [
 {
 "metadata.creationTimestamp": {
 "nested": {
 "path": "metadata"
 },
 "order": "desc"
 }
 }
 ],
 "track_total_hits": true
}
```

1. Searching by the `atlan-mssql` prefix will ensure you only find existing SQL Server assets workflows.

 :::tip[Name of the workflow]
The name of the workflow will be nested within the `_source.metadata.name` property of the response object.
(Remember since this is a search, there could be multiple results, so you may want to use the other
details in each result to determine which workflow you really want.)
 :::
```json title="POST /api/service/workflows/submit"
{
 "namespace": "default",
 "resourceKind": "WorkflowTemplate",
 "resourceName": "atlan-mssql-1684500411" // (1)
}
```

1. Send the name of the workflow as the `resourceName` to rerun it.

---
