No BSD License  

Highlights from
BuildKeyWhereClause

image thumbnail
from BuildKeyWhereClause by Michael Robbins
given a matrix of data and indexes to the changed data, it will build a where clause to be used by t

BuildKeyWhereClause(newdata,row,TableName,csFieldnames,csPrimaryKey)
function whereclause = BuildKeyWhereClause(newdata,row,TableName,csFieldnames,csPrimaryKey)
% BUILDKEYWERECLAUSE given a matrix of data and indexes to the changed
% data, BUILDKEYWHERECLAUSE will build a where clause to be used by the
% UPDATE command in the Mathworks Database Toolbox
%
% BUILDKEYWHERECLAUSE(NEWDATA,ROW,TABLENAME,CSFIELDNAMES,CSPRIMARYKEY)
%
% 
% See Also update database primarykey
%
% Key Words sql update database primarykey primary key fieldnames
%    field names table whereclause where clause
%
%
% It's not fancy, but it works

% Michael Robbins
% michaelNOrobbinsSPAMusenet@yahoo.com
% robbins@bloomberg.net


[dummy,idxFieldnames] = intersect(csFieldnames,csPrimaryKey);
for i=1:length(csPrimaryKey)
    if i==1
        whereclause = 'WHERE ';
    else
        whereclause = [whereclause ' AND '];
    end;
    whereclause = [whereclause ...
        sprintf('((%s.%s) = %s)', ...
        TableName, ...
        csFieldnames{idxFieldnames(i)}, ...
        BKWCData2String(newdata{row,idxFieldnames(i)}) ...
        )];
end;

function xstr = BKWCData2String(x)

if isnumeric(x)
    if isinteger(x) || islogical(x) || (x-floor(x))<eps*2
        xstr = sprintf('%d',x);
    else
        xstr = sprintf('%f',x);
    end;
elseif ischar(x)
    xstr = ['''' x '''']; 
end;

Contact us