execute multiple SQL statements using 'exec'
12 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to declare and set a geography data type within a database using the Matlab database toolbox. Specifically I am trying to run the following SQL command(s),
DECLARE @g geography; SET @g = geography::Point(47.65100, -122.34900, 4326) SELECT @g.ToString();
Where eventually the defined point will be a lat/long location from some Matlab code and some retreival of data within the database will be performed.
I have tried running the above code by breaking the 'SET', 'DECLARE' and 'SELECT statements and running sequential 'exec' commands ie.,
conn = databaseConnect;
run = ['DECLARE @g geography'] ;
log = exec(conn, run);
run = ['SET @g = geography::Parse(''POINT(55.8659449685365
4.25072511658072)'')'];
log = exec(conn, run);
etc.
but this does not work, this error is returned,
No ResultSet was produced
[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@g".
[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@g".
Is there another way this can be done (without populating a table in the database with the lat/long and using this)? I can't seem to combine these statements into one and execute this with a single 'exec' command.
I am eventually looking at implementing the algorithms detailed here http://blogs.msdn.com/b/isaac/archive/2008/10/23/nearest-neighbors.aspx?PageIndex=1#comments where my search point is defined within Matlab.
Any help is much appreciated,
Thanks Chris
0 个评论
回答(1 个)
Leepakshi
2025-3-5
Hey Chris,
When executing SQL commands that involve variable declarations and multiple statements within MATLAB using the Database Toolbox, the commands should be combined into a single batch execution. This can be achieved using a single exec command with the entire SQL script. But Here's how the SQL command can be structured and executed:
% Establish the database connection
conn = databaseConnect;
% Combine the SQL statements into a single batch
sqlQuery = [
'DECLARE @g geography; ' ...
'SET @g = geography::Point(47.65100, -122.34900, 4326); ' ...
'SELECT @g.ToString();'
];
% Execute the combined SQL command
log = exec(conn, sqlQuery);
% Fetch and display the results
results = fetch(log);
disp(results.Data);
% Close the connection
close(conn);
Executing SQL commands that involve variable declarations across separate exec calls in MATLAB will not work as expected. This is because each exec call is treated as a separate transaction, and any variables declared in one transaction are not recognized in subsequent transactions.
Note that, The exec function is not recommended now. For SQL statements that return data, use the fetch function or the select function instead. For other SQL statements, use the execute function instead.
Refer to below documentation for more clarity:
Hope this helps!
Thanks
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!