Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers 3

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.

share|improve this answer
add comment
up vote 2 down vote accepted

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:

CREATE PROCEDURE [dbo].[MAT_EXEC_SCRIPT]
    @scriptName [varchar](1000)
WITH EXECUTE AS CALLER AS

BEGIN

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

END

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

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

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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