/Stock Calculator/SharedFiles/AddField.m

http://stock-shock-source.googlecode.com/ · MATLAB · 35 lines · 8 code · 2 blank · 25 comment · 1 complexity · c9d571999d06b2770e76c86bafcb977f MD5 · raw file

  1. function [] = AddField(conn,TableName,fieldname,DataType)
  2. %Add a new field to a table
  3. %
  4. %INPUTS:
  5. % TableName: Name of the table you would like to add a field.
  6. % Fieldname: Fieldname you want to add.
  7. % DataType: Type of data associated with the filed name
  8. % 'Type(FieldSize)
  9. % 'TEXT(40)' 40 character max length.
  10. % 'TEXT(20)'
  11. % 'TEXT(10)'
  12. % 'NUMBER' defaults to double (not sure how to do interger
  13. % at the moment.
  14. %
  15. % Example:
  16. % conn = database('SaxoTrader','','');
  17. % AddField(conn,TableName,fieldname,DataType);
  18. %
  19. %Written by: Bryan Taylor
  20. %Date Created: 9th July 2008
  21. %Date Modified: 9th July 2008
  22. % CREATE TABLE MarketingSurvey (ID SHORT, Name TEXT (40), Response MEMO, Class TEXT (10));
  23. % ALTER TABLE MyTable ADD COLUMN NewColumn TEXT (20);
  24. sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s',TableName,fieldname,DataType);
  25. curs = exec(conn, sql);
  26. %ERROR CHECKING
  27. %Check to see if there was any errors
  28. %Flag any errors reported in the curs object
  29. status = curs.Message;
  30. location = findstr(status,'No ResultSet was produced');
  31. if isempty(location)
  32. error(status)
  33. end