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!