Quickstart: "Hello world" Python script in SQL Server

APPLIES TO: yesSQL Server (Windows only) noAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

In this quickstart, you learn key concepts by running a "Hello World" Python script inT-SQL, with an introduction to the sp_execute_external_script system stored procedure.

Prerequisites

A previous quickstart, Verify Python exists in SQL Server, provides information and links for setting up the Python environment required for this quickstart.

Basic Python interaction

There are two ways to run Python code in SQL Server:

The following exercise is focused on the first interaction model: how to pass Python code to a stored procedure.

  1. Run some simple code to see how data is passed back and forth between SQL Server and Python.

    execute sp_execute_external_script 
    @language = N'Python', 
    @script = N'
    a = 1
    b = 2
    c = a/b
    d = a*b
    print(c, d)
    '
    
  2. Assuming that you have everything set up correctly, and Python and SQL Server are talking to each other, the correct result is calculated, and the Python print function returns the result to the Messages windows.

    Results

    STDOUT message(s) from external script: 
    0.5 2
    

    While getting stdout messages is handy when testing your code, more often you need to return the results in tabular format, so that you can use it in an application or write it to a table.

For now, remember these rules:

  • Everything inside the @script argument must be valid Python code.
  • The code must follow all Python rules regarding indentation, variable names, and so forth. When you get an error, check your white space and casing.
  • Among programming languages, Python is one of the most flexible with regard to single quotes vs. double quotation marks; they're pretty much interchangeable. However, T-SQL uses single quotes for only certain things, and the @script argument uses single quotes to enclose the Python code as a Unicode string. Therefore, you might need to review your Python code and change some single quotes to double quotes.
  • If you are using any libraries that are not loaded by default, you must use an import statement at the beginning of your script to load them. SQL Server adds several product-specific libraries. For more information, see Python libraries.
  • If the library is not already installed, stop and install the Python package outside of SQL Server, as described here: Install new Python packages on SQL Server

Run a Hello World script

The following exercise runs another simple Python scripts.

EXEC sp_execute_external_script
@language =N'Python',
@script=N'OutputDataSet = InputDataSet',
@input_data_1 =N'SELECT 1 AS hello'
WITH RESULT SETS (([Hello World] int));
GO

Inputs to this stored procedure include:

  • *@language* parameter defines the language extension to call, in this case, Python.
  • *@script* parameter defines the commands passed to the Python runtime. Your entire Python script must be enclosed in this argument, as Unicode text. You could also add the text to a variable of type nvarchar and then call the variable.
  • *@input_data_1* is data returned by the query, passed to the Python runtime, which returns the data to SQL Server as a data frame.
  • WITH RESULT SETS clause defines the schema of the returned data table for SQL Server, adding "Hello World" as the column name, int for the data type.

Results

Hello World
1

Next steps

Now that you have run a couple of simple Python scripts, take a closer look at structuring inputs and outputs.