I have a SQL script that works fine in Microsoft SQL Sever Management Studio. It looks something like this:
DECLARE @CONST_STARTDATE DATETIME
DECLARE @CONST_ENDDATE DATETIME
SET @CONST_STARTDATE = '2017-01-01';
SET @CONST_ENDDATE = '2050-05-05';
WITH abc AS ( SELECT * FROM tablename WHERE StartDate BETWEEN @CONST_STARTDATE AND @CONST_ENDDATE )
However, it does not seem to work when I run this in MATLAB as follows:
conn = database('db','user','pw');
qfile = 'path\to\file.sql'
results = runsqlscript(conn,qfile);
It returns the following errors:
Query Batch 1: No ResultSet was produced Query Batch 2: [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@CONST_ENDDATE". Query Batch 3: [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@CONST_STARTDATE".
What is the reason for this error in MATLAB, whereas it works fine on the Sever Studio?