Blog Post

Azure Synapse Analytics Blog
4 MIN READ

Generate create/drop external table scripts on Synapse SQL using PowerShell DbaTools

JovanPop's avatar
JovanPop
Icon for Microsoft rankMicrosoft
May 06, 2020

Azure Synapse SQL Analytics is Azure Data analytic solution that contains all components and services that you would need to implement data analytic solutions. One important part of Azure Synapse is Synapse SQL serverless query service that enables you to query Azure storage files using pure T-SQL language and external table.

Synapse SQL supports rich T-SQL language that enables most of the tools (even open-source non-Microsoft tools like DbaTools) to work with this new service. In this article you will see how you can use PowerShell DbaTools to script Synapse SQL objects the same way you are doing this in standard SQL Server and Azure SQL Database.

DbaTools is well known community PowerShell tool that simplifies database administration tasks. It contains many useful scripts that can help you automate administration/devops tasks, configure database, and script objects in the database.

One interesting PowerShell script that might be handy is Export-DbaScript that enables you to generate scripts of some selected objects from your database. In Synapse SQL you can list set of external tables from your database and use Export-DbaScript to generate CREATE or DROP scripts. This might be useful if you need to move external tables to another database, or keep scripts under source control.

Installing DbaTools

DBAtools is an open-source PowerShell library that must be installed on the computer that you will use to connect to your Synapse SQL query service. You can review requirements on dbatools download/ page, but if you are already using PowerShell, you probably need to run to following command to install this library:

 

 

Install-Module dbatools

 

 

Once you install you are ready to try the samples in this article.

Generating a script that creates external tables

If you have created a set of external tables in Synapse SQL and you want to move their definitions to another instance or keep creation script locally or in some source-control system, you can easily create script that creates all external tables. 

If you have installed the latest version of DbaTools you can use the following script to generate CREATE EXTERNAL TABLE scripts for a set of your tables:

 

$loginName = "<login>"
$synapseSqlName = "<sql endpoint name>"
$databaseName = "<database name>"
$login = Get-Credential -Message "Enter your SQL on-demand password" -UserName $loginName
$script = Get-DbaDbTable -SqlInstance "$synapseSqlName.sql.azuresynapse.net" -Database $databaseName -SqlCredential $login | Export-DbaScript -Passthru | Out-String
$script -replace ' NULL', ''

 

 

You need to enter name of Synapse SQL serverless endpoint instead of <sql endpoint name> (for example mysynapsesqlendpoint-ondemand), login name that you will use to connect to your Synapse SQL database instead of <login>, and name of the database where your external tables are placed instead of <database name>.

 

NOTE: if you have problem connecting to .sql.azuresynapse.net domain, try to switch to .database.windows.net.

 

Once you run this script, you will see the script that creates external tables in output window.

Export-DbaScript enables you to customize the script and export CREATE TABLE statements in some file, define encoding, omit schema, etc. You have many options that you can use to customize script generation.

In the example above I have used Get-DbaDbTable command to fetch all external tables from my database. You can also customize this command to get the tables from multiple databases or specify set of tables that you want to export.

Generating a script that drops all external tables

If you already have external tables in your target database you might want to generate drop script that will drop. Export-DbaScript also simplifies this task and you can use the following script:

 

$loginName = "<login>"
$synapseSqlName = "<sql endpoint name>"
$databaseName = "<database name>"
$login = Get-Credential -Message "Enter your SQL on-demand password" -UserName $loginName
$options = New-DbaScriptingOption
$options.ScriptDrops = $true
Get-DbaDbTable -SqlInstance "$synapseSqlName.sql.azuresynapse.net" -Database $databaseName -SqlCredential $login | Export-DbaScript -ScriptingOptionsObject $options -Passthru

 

 

Again, you need to enter name of Synapse SQL serverless endpoint instead of <sql endpoint name> (for example mysynapsesqlendpoint-ondemand), login name that you will use to connect to your SynapseSQL database instead of <login>, and name of the database where your external tables are placed instead of <database name>.

Note the DbaScriptingOption parameter where you can instruct Export-DbaScript to generate only DROP statements. Using these options you can easily customize the generated scripts.

Conclusion

DbaTools is very useful set of PowerShell commands that can enable you to automate administration tasks in your Synapse SQL databases. In this example you have seen hot to generate scripts that create and drop tables, but you can easily modify this script to create/drop users, databases, etc.

Updated Oct 26, 2021
Version 5.0

7 Comments

  • make sure that you put -ondemand in mysynapsesqlserverlessendpoint-ondemand.database.windows.net.

    If this doesn't work, try to connect to synapse using the SSMS from the same computer where you are running the script because you are getting the networks error.

  • NP123765's avatar
    NP123765
    Copper Contributor

    I also received the error:

    WARNING: [14:08:36][Get-DbaDatabase] Failure | The network path was not found

    I use Active Directory to login.

    So I changed the OP script ever so slightly:

     

    $loginName = "email address removed for privacy reasons"
    $synapseSqlName = "mysynapsesqlserverlessendpoint.database.windows.net" #had to use database.windows.net
    $databaseName = "mySynDB"
    $server = Connect-DbaInstance -SqlInstance $synapseSqlName -Database $databaseName -SqlCredential $loginName -DisableException
    #Run a test query to confirm OK
    Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
    #Then run the script just removing the login part from OP original script since I have already connected at this point
    $script = Get-DbaDbTable -SqlInstance $synapseSqlName -Database $databaseName | Export-DbaScript -Passthru | Out-String
    $script -replace ' NULL', '

     

    This worked for me and the idea to use this from OP was great as it saved me a chunk of time, thanks OP!

  • kboos421's avatar
    kboos421
    Copper Contributor

    JovanPop.  I use the same credentials with other utilities like "sqlcmd" from cloud shell and it works. When I use them with Get-DbaDbTbl it fails to login.
    the credentials that I am using are of  "Sql authentication" type. Is there any pre-setup or configuration that needs to be done for the command to work and does it support Sql authentication for Synapse?  I have tried this with Server admin ID and a non-admin id but both game me the same error. Any thoughts or advise?

  • Hi kboos421 this looks like a generic error where you cannot even login. Please double-check credentials or try with some other sql credential. DbaTools uses the same auth technique as any .Net app/tool, so try also with other tools. Unfortunately, I don't have any better answer.

  • kboos421's avatar
    kboos421
    Copper Contributor

    the Get-DbaDbTable sounds promising and we have a use case for the DDL generation. When I use the example listed above (with appropriate values plugged in) I keep getting the below message even though the credentials and endpoint details are accurate. Please let me know if there could be any other reason I get the below message

     

    WARNING: [20:29:50][Get-DbaDatabase] Failure | Login failed for user 'xxxxxxxxx'.

     

  • sm7__'s avatar
    sm7__
    Copper Contributor

    I'm getting the following error:

     

    WARNING: [10:53:45][Get-DbaDatabase] Failure | The network path was not found

     

    trying to connecting to a Serverless SQL endpoint

     

    .sql.azuresynapse.net

     

    Any ideas why I am getting this error?

    JovanPop 

  • marsxiang's avatar
    marsxiang
    Former Employee

    Nice guideline. Had some test based on this documentation and found when we use .sql.azuresynapse.net, it returns following error message during the authentication:

    WARNING: [20:34:26][Invoke-QueryRawDatabases] Failure | Error connecting to [mxwssea01-ondemand.sql.azuresynapse.net]: You cannot call a method on a null-valued expression.

     

    To mitigate this we need to change the endpoint to .database.windows.net.