Is there any way of executing Python scripts within SQL Server?
-
1AFAIK with MS SQL server no, with postgreSQL yes.Cetin Basoz– Cetin Basoz2018-02-27 10:22:56 +00:00Commented Feb 27, 2018 at 10:22
-
Useful link - learn.microsoft.com/en-us/sql/advanced-analytics/tutorials/…Abhishek– Abhishek2018-02-27 10:25:04 +00:00Commented Feb 27, 2018 at 10:25
-
some useful links that might help you! [mssqltips.com/sqlservertip/5037/… [mssqltips.com/sqlservertip/5036/…Abdullah Ahmed Ghaznavi– Abdullah Ahmed Ghaznavi2018-02-27 10:35:47 +00:00Commented Feb 27, 2018 at 10:35
-
This question is similar to: Run Python Script from MSSQL. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.TylerH– TylerH2024-11-04 20:59:14 +00:00Commented Nov 4, 2024 at 20:59
Add a comment
|
1 Answer
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)
)