Must declare the scalar variable SQL with Matlab - sql

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?

Related

Getting a syntax error when declaring a table in SQL

New to SQL and trying to run this code and I get the error "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.

DB implicitly converts from INT to NVARCHAR(128) warning

On my Azure SQL database I get a warning in 'Actual Execution Plan' in SQL Server Management Studio whenever I select.
It is due to the following 'where' in my row level security.
WHERE (#GROUP_ID = USER_NAME()) OR (IS_MEMBER('ReadOrWriteAll') = 1)
GROUP_ID is an int that's being compared to USER_NAME() which returns a NVARCHAR(128).
I've tried converting GROUP_ID into a NVARCHAR, but the warning remains.
I've tried using str(#GROUP_ID) and convert(#GROUP_ID).

SAS Pass Through OpenQuery into iHistorian Error

I am doing a pass through query using SAS into iHistorian Interactive SQL. The code is listed below. I know the pass through code works; I ran the quoted code through iHistorian Interactive SQL itself and it returned the required data. I get the following error when I attempt the provided code.
ERROR: CLI describe error: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot initialize the data source object of OLE DB
provider "IhOLEDB.iHistorian" for linked server "fsp1".
Has anyone tried this with iHistorian? Does anyone know what's going on here? Thanks in advance.
proc sql;
connect to odbc as conn(dsn="FSPPDW" uid=FontaneF);
create table work.Stuff as
select * from connection to conn (
select * from OPENQUERY(fsp1,
'SELECT
tagname,
timestamp,
value as Signal
FROM ihrawdata
WHERE timestamp > 10/23/2015 AND timestamp < 10/25/2015
AND (tagname = Fsdfeee:000)
AND intervalmilliseconds = 60000
AND samplingmode=interpolated'
)
);
disconnect from conn;
;
quit;

Why am I getting this error? Must Declare Scalar Variable Error SQL Server 2008

I think I am trying to do something that is relatively straight forward, but keep getting the 'Must Declare Scalar Variable Error'. I have two variables:
DECLARE #CountOfThisCampaignSegment int
DECLARE #CountOfLastCampaignSegment int
select #CountOfThisCampaignSegment = COUNT(seg1) from #thisCampaignFinal
select #CountOfLastCampaignSegment = COUNT(seg1) from #lastCampaignFinal
I read that somewhere else that it seems I need to turn them into nvarchar, but I need to compare these two variables for an IF statement down the track in my procedure - so I am not sure if that would cause problems?
If anyone could give me some advice and background on why SQL Server throws this error, it would be greatly appreciated!
Many Thanks!
I suspect you actually have more than one batch in your script. Batches are separated by GO statements (by default) in SQL Server Management Studio.
Variables do not persist across batches. So this, for example, gives an error:
DECLARE #CountOfThisCampaignSegment int
DECLARE #CountOfLastCampaignSegment int
select #CountOfThisCampaignSegment = COUNT(seg1) from #thisCampaignFinal
select #CountOfLastCampaignSegment = COUNT(seg1) from #lastCampaignFinal
-- other statements
GO
-- this line fails with "must declare scalar variable":
select #CountOfThisCampaignSegment, #CountOfLastCampaignSegment

How to create a table type in Sql Server 2005

I'm trying to create a table type in sql server 2005.
Here is what my code looks like:
CREATE TYPE NameResourceType AS TABLE
(
ID int,
[Value] Varchar(256)
)
GO
I receive the following error:
Incorrect syntax near the keyword 'AS'.
Table alias data types & Table-Valued parameters were introduced in SQL Server 2008 so are not available in prior versions.
New in SQL Server 2008... "Table-Valued Parameters"
CREATE TYPE in SQL Server 2005 refers to "simple" user defined data types only... notice the difference in the SQL Server 2008 CREATE TYPE