How do I use the UPDATE command to update a column for all the rows in my table?
    1 view (last 30 days)
  
       Show older comments
    
I am using the Database Toolbox 3.4.1 (R2008a). I want to update a column in my table with a given value using the UPDATE command without the WHERE clause.
Accepted Answer
  MathWorks Support Team
    
 on 3 Mar 2011
        A column in a table can be updated for all the rows using the UPDATE command with the ORDER BY clause instead of the WHERE clause.
For example, refer to the code below:
% Create the table
query = 'CREATE TABLE test2 (col1 varchar(10), col2 varchar(5))';
results = exec(conn,query);
results = fetch(results);
results.Data
query = 'SHOW tables';
results = exec(conn,query);
results = fetch(results);
results.Data
%Insert Rows
exdata = {'San Diego', 'CA'}
colnames = {'col1', 'col2'}
fastinsert(conn, 'test2', colnames, exdata)
exdata = {'Hartford', 'CT'}
fastinsert(conn, 'test2', colnames, exdata)
query = 'SELECT * from test2';
results = exec(conn,query);
results = fetch(results);
results.Data
% UPDATE the rows
update(conn, 'test2', {'col1'}, {'NewYork'}, '');
query = 'SELECT * from test';
results = exec(conn,query);
results = fetch(results);
results.Data
0 Comments
More Answers (0)
See Also
Categories
				Find more on Database Toolbox in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!