Does anyone have experience in calling Mathematica from inside SQL Server to do calculations? For example, how can I make a Procedure that uses Mathematica? Or a job that runs some Mathematica script?

I saw this post on MathGroup, but it's a little bit confusing to me. Is there an easy way?

Being more specific, I would like to execute (from inside SQL Server) a Mathematica script. The script works using MathKernel -script myFile.m but I don't know how to make SQL call it. The Mathematica is installed in another server, in the same local web.


If you just need to run a block of Mathematica code then this questions seems to be equivalent to, How do I run an operating system command/script from SQL?

Using the operating system command line invocation MathKernel -script myFile.m you can call your Mathematica script by whichever mechanism your version of SQL supports for accessing OS commands.

Your script can return data using SQLExecute or by writing to an external CSV file and have SQL import it.


I found out how to solve this problem in SQL Sever. I used PsExec together with xp_Cmdshell to do the job. You have to install the PsExec in Windows in the system32 directory of the server machine. You can find the installer here. After that, I created this procedure, that can execute a file in a remote server:

    @scriptName [varchar](1000)


    Declare @command varchar(1000)  
    set @command='PsExec \\ -u domain\login -p password -d  C:\Rotinas\' + @scriptName 
    Exec Master..xp_Cmdshell @command


and this test bat file in the Rotina folder, called exec.bat:

::Test bat file to call Mathematica Scripts
@echo off
PATH = C:\Program Files\Wolfram Research\Mathematica\8.0;%PATH%
start MathKernel -script "C:\Scripts\mathScript00_Test.m"

Now I can execute the procedure as: EXEC MAT_EXEC_SCRIPT 'exec.bat' This will call the bat file, which then calls the Mathematica script. You can avoid this bat file if you want, but I prefer to keep it separated, with the intermediate bat file, so as to be better organized.


On MySQL, this is done with system 'MathKernel -script myFile.m', but it only works in Unix.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.