Predicting with T-SQL
The last question to answer is how you use the R and Python models in T-SQL. Of course, you can create the models and train them directly with the sys.sp_execute_external_script
stored procedure. However, it would make no sense to retrain the complete model on the same training set for every new prediction, even if it is prediction on a single case. SQL Server 2017 introduces the PREDICT()
function. Using this function means performing native predictions in SQL Server.
In order to use the PREDICT()
function, you need to serialize the model in a SQL Server table, in a VARBINARY(MAX)
column. You don't even have to have the ML Services (In-Database) installed in the SQL Server where you serialize your models to do the native predictions. The following code creates the table for the models:
CREATE TABLE dbo.dsModels (Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, ModelName NVARCHAR(50) NOT NULL, Model VARBINARY(MAX) NOT NULL); GO
Unfortunately, not all models are supported...