dynamic_sql_question

by

 

How to create dynamic SQL questions.

dynamic_sql_question(choice,startdate,enddate)
function [out,rawColumnNames] = dynamic_sql_question(choice,startdate,enddate)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%A function to create dynamic SQL-question towards databases
%Input:     choice - char  [database]
%           startdate - char, ie 20090909
%           enddate -   char, ie 20091224
%
%Author:Peter Lindberg
%
%Date:091222
%
%The main structure of this code was generated with querybuilder
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





disp('*****dynamic_sql_question*****')
% Set preferences with setdbprefs.
s.DataReturnFormat = 'cellarray';
s.ErrorHandling = 'store';
s.NullNumberRead = 'NaN';
s.NullNumberWrite = 'NaN';
s.NullStringRead = 'null';
s.NullStringWrite = 'null';
s.JDBCDataSourceFile = '';
s.UseRegistryForSources = 'yes';
s.TempDirForRegistryOutput = 'C:\Temp';
setdbprefs(s)

% Make connection to database.  Note that the password has been omitted.
% Using ODBC driver.
conn = database('database','username','password');  % database = the database you wanna connect to as string
                                                    % username as string
                                                    % password as string



if strcmp(choice,'one')
    disp('Choice one selected')
    kalle=['SELECT * FROM ' '''' choice '''' ' WHERE ??? BETWEEN ' '''' startdate '''' ' AND ' '''' enddate '''' '  AND ??? LIKE ''???%''  AND ??? NOT LIKE ''????''  ORDER BY ????'];
elseif strcmp(choice,'two')
    disp('Choice two selected')
    kalle=['SELECT * FROM ' '''' choice '''' ' WHERE ??? BETWEEN ' '''' startdate '''' ' AND ' '''' enddate '''' '  AND ??? LIKE ''???%''  AND ??? NOT LIKE ''????''  ORDER BY ????'];
else
    disp('wrong name of db!')
end
    
% Read data from database.
e = exec(conn,kalle);
e = fetch(e);


out=e.Data;                     %get data from e
rawColumnNames=columnnames(e);  %get columnnames from e
close(e)                        %close e
close(conn)                     % Close database connection.

Contact us