Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

How to execute Python code in Microsoft SQL Server on T-SQL

14 August 2020

How to execute Python code in Microsoft SQL Server on T-SQL

Today we will consider how to run Python code in a Microsoft SQL Server database, you will learn what you need to do to enable this feature, and how to run Python code using a regular T-SQL instruction.

Prerequisites and SQL Server preparation

In order to execute the Python code in an SQL Server database, certain conditions must be met.

Microsoft SQL Server is at least version 2017

Support for Python language appeared only in Microsoft SQL Server 2017, so it is possible to execute code on Python only starting from 2017 version of SQL Server.

The “Machine learning services” must be installed

In Microsoft SQL Server 2016 for the first time it is possible to execute instructions directly in the database in other languages, in particular, the R language.

In 2017, this component was named “Machine Training Services”, which, as noted, added support for the Python language.

Thus, to run the code Python, in a database SQL Server must be installed “Machine Training Services”, which adds to our ability.

If the component “Machine Training Services” is not installed, then run the Python code in a Microsoft SQL Server database will not work.

What is “Machine Training Services” in SQL Server

Machine Training Services is an SQL Server component that allows you to run scripts in Python and R with relational data.

Instructions are executed in the database without moving these data outside of SQL Server, for example, on the network to another server, which greatly extends the standard features of SQL Server and the language T-SQL.

Installing the “Machine Training Services” component

If you do not have “Machine Training Services” installed, you can reinstall them. To do this, run “SQL Server Installation Center“, on the “Installation” tab select “New installation of an isolated instance of SQL Server or adding components to an existing installation“.

New installation of an isolated SQL Server instance or adding components to an existing installation

Then on the “Component Selection” tab in the “DBMS Kernel Services” section mark the “Machine Training Services (in the database)” component as well as which language you want to use, in our case Python.

Machine Training Services

Then click “Accept” on the “Accept installation” page.

Accept installation Python in Microsoft SQL Server 2017

After installation is complete, it is better to restart the computer.

Note! This installation option requires Internet access. If there is no internet access, you can use a standalone installer.

On SQL Server, you must allow the use of external scripts

By default, the execution of external scripts using the Python language in Microsoft SQL Server is prohibited, so you must first enable this feature on SQL Server.

How to allow using external scripts in SQL Server

To allow the use of external scripts in the Python language, you must enable the system parameter “external scripts enabled” in SQL Server, this is done using the system procedure sp_configure.

sp_configure 'external scripts enabled', 1;
RECONFIGURE WITH OVERRIDE

After that you should restart the SQL server service.

To check the value of the parameter, you can execute the following instruction.

EXEC sp_configure 'external scripts enabled'

To check the value of the parameter, you can execute the following instruction

If the run_value parameter value is 1, then the use of external scripts in SQL Server is allowed.

Executing Python code in Microsoft SQL Server in T-SQL

Python code is executed, as well as code of other languages, such as R, in Microsoft SQL Server using the system stored procedure sp_execute_external_script.

sp_execute_external_script is a system stored procedure that executes the script in supported languages (R, Python), passed as an input parameter.

Syntax sp_execute_external_script

sp_execute_external_script
@language = N'language',
@script = N'script'
[ , @input_data_1 = N'input_data_1' ]
[ , @input_data_1_name = N'input_data_1_name' ].
[ , @output_data_1_name = N'output_data_1_name' ].
[ , @parallel = 0 | 1 ]
[ , @params = N'@parameter_name data_type [ OUT | OUTPUT ] [ , ...n ]' ]
[ , @parameter1 = 'value1' [ OUT | OUTPUT ] [ , ...n ] ]
[ WITH RESULT SETS ( <result_sets_definition> )] ;

Where,

  • @language – the parameter in which the script language is specified;
  • @script – script text;
  • @input_data_1 – the parameter specifies the input data used by external script in the form of T-SQL query;
  • @input_data_1_name – the parameter specifies the name of the variable used to represent the query defined in @input_data_1;
  • @output_data_1_name – the parameter specifies the variable name in the external script that contains data returned to SQL Server after the stored procedure call;
  • @parallel – the parameter includes parallel execution of R scripts (value 1). Default value for this parameter is 0 (without parallelism);
  • @params – declaration of input parameters used in the external script;
  • @parameter1 – list of values of input parameters, used in the external script;
  • WITH RESULT SETS – by default the resulting set returned by the sp_execute_external_script procedure is displayed with unnamed columns. To name the columns of the resulting set, use the WITH RESULT SETS sentence for the EXECUTE command.

Examples of Python code execution in SQL Server

Let’s run some examples.

Example 1 – Output the result into a standard data output

In this case, we simply perform some calculations and get the result into a standard data output.

EXECUTE sp_execute_external_script
@language = N'Python',
@script = N'
a = 1
b = 2
c = a+b
print ("Example instruction on Python")
print("Result =", c)';

As you can see, in the language parameter we specified that we would run the code on Python, then in the script parameter we wrote the code itself, where we create several variables, add their values and generate messages with the print command.

The result is that we get these messages in the standard output of the messages, for example, as if we had executed the eponymous print command in T-SQL.

Example 2 – Output the result in tabular form

Let’s make our Python code a bit more complicated and form data on it in such a way as to get it in our usual form, i.e. tabular.

For this we will use the Pandas library and the Series object, which is a one-dimensional array of indexed data.

We will form the resulting dataset using the WITH RESULT SETS instruction, which will help us specify the column name and its data type.

EXECUTE sp_execute_external_script @language = N'Python',
@script = N'
import pandas as pd
TestVar = pandas.Series([100, 200, 300]);
OutputDataSet = pd.DataFrame(TestVar);
'
@input_data_1 = N''.
WITH RESULT SETS((Column1 INT NOT NULL));

Example 3 – Transferring data to Python code

Here we will consider an example of transmitting data, e.g. tabular data, received with a simple SELECT query to a procedure.

For this purpose, we will first form the text of the query and save it in a variable, for convenience and clarity of the code.

Then we use the parameter @input_data_1 to pass this query text into the procedure using a variable (in principle, we can specify the query text itself).

Using parameter @input_data_1_name we speak as the result set of query data which we specified in parameter @input_data_1 will be named in Python code, i.e. here we specify the name of the variable which will contain our incoming data. For example, I named this variable Input_Data, and in code we can work with this variable to use the incoming tabular data.

In this case we just pass the data and we do not do anything with it, we just return the data back, for this OutputDataSet parameter we assigned the data from our variable.

We have formed the final dataset, i.e. we have specified column names and data types, also using the WITH RESULT SETS instruction.

SELECT ProductId, ProductName, Price
FROM Goods;

GO

DECLARE @Input_Query NVARCHAR(MAX) = N'SELECT ProductId,
CAST(ProductName AS NVARCHAR(100)),
CAST(Price AS FLOAT)
FROM Goods';

EXEC sp_execute_external_script @language = N'Python',
@script = N'
# Here we can process the incoming data
#...
OutputDataSet = Input_Data
'
, @input_data_1 = @Input_Query
@input_data_1_name = N'Input_Data'.
WITH RESULT SETS ((ProductId INT, ProductName VARCHAR(100), Price MONEY);

Here we are with you and have seen the basic principles of working with Python in Microsoft SQL Server, we learned how to enable the ability to work with Python, how to run code, how to use the incoming data and how to get the result in tabular form.

 

 
Tags: , , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...