2

Is there any way of executing Python scripts within SQL Server?

TylerH
21.3k85 gold badges84 silver badges122 bronze badges
asked Feb 27, 2018 at 10:20
4

1 Answer 1

1

I'm far from an expert but was recently looking this, so here's a very simple T-SQL script with embedded Python if it helps anyone (Note: assumes you have Machine Learning Services with Python all set up on SQL Server).

-- Stored proc which runs embedded Machine Learning Services scripts
EXEC sp_execute_external_script
-- The SQL query to be input to the Python script
@input_data_1 = N'SELECT TOP(100) [CustomerID], [JobNumber] 
 FROM [dbo].[Heading] ORDER BY DateDelivery DESC',
-- Variable name for the input SQL data ('InputDataSet' is the default if none)
@input_data_1_name = N'InputDataSet',
-- The language of the script
@language = N'Python',
-- Python script itself - Just imports Pandas and creates a new DataFrame with only one of the two input columns
@script = N'
import pandas as pd
OutputDataSet = pd.DataFrame(InputDataSet["CustomerID"])
'
-- Declaring SQL columns for the output (a Pandas DataFrame)
WITH RESULT SETS (
 ("CustomerID" INT NULL)
)
answered Mar 6, 2018 at 9:33
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.