SQL query in a M-file. Not working
1 次查看(过去 30 天)
显示 更早的评论
I tried to execute an sql query from an M- file. But I am getting the error message "undefined function fetch ". when i executed the same query from workspace it is working.
I tried the following code in a M-file:
function [ Pocc ] = Multiprodinv( x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
conn = database('MultiProductInventory','','');
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x5=x(5);
x6=x(6);
x7=x(7);
x8=x(8);
x9=x(9);
x10=x(10);
curs = exec(conn,['select all ID from Stocks where PI=',num2str(x1),'and F1=',num2str(x2),...
'and F2=',num2str(x3),'and F3=',num2str(x4),'and F4=',num2str(x5),'and F5=',num2str(x6),...
'and F6=',num2str(x7),'and F7=',num2str(x8),'and F8=',num2str(x9),'and F9=',num2str(x10)]);
curs=fetch(curs);
y= curs.Data;
Pocc=length(y);
x is a record of integer values with 10 fields representing the inventory at various levels (plants, warehouses, distributors, agents etc).The first field of x is the ProductID. The program has to find the number of occurrences of the record x in the database(MultiProductInventory) table Stocks.
please guide me.
2 个评论
Guillaume
2016-8-3
You've got a computer language that allows you to automate things, and instead you go and make it more manual by typing every step of your query.
conn = database('MultiProductInventory','','');
query = ['select all ID from Stocks where PI=', num2str(x(1))];
for fidx = 1:9
query = [query, sprintf(' and F%d = ', fidx), num2str(x(fidx + 1))];
end
curs = exec(conn, query);
Isn't that less work?
No idea about your problem, though. Can you give the entire error message? Are you sure that curs is a cursor object when you call fetch from the command line?
回答(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!