Home

Records Maintenance: Updating Records

 

Description

Updating a record consists of changing its value for a particular column.

To support record maintenance operations, the SQL provides the UPDATE keyword that is used to specify the table on which you want to maintain the record(s). The basic formula to use is:

UPDATE TableName
SET ColumnName = Expression

With this formula, you must specify the name of the involved table as the TableName factor of our formula. The SET statement allows you to specify a new value, Expression, for the field under the ColumnName column.

Updating all Records

In SQL, the primary formula of the UPDATE statement as introduced on our formula can be used to update all records. Here is an example:

void btnUpdateAllRecords_Click(object sender, EventArgs e)
{
    SqlConnection connection =
        new SqlConnection("Data Source=(local);" +
                  "Database='Exercise';" +
                  "Integrated Security=yes;");
    SqlCommand command = new SqlCommand("UPDATE Employees " +
                                        "SET HourlySalary = 20.00;", connection);

    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
}

When this code executes, the hourly salary of all employees on the table will be set to 20.00

Editing a Record

To edit a record, you must provide a way to locate the record. To do this, you use the WHERE operator in an UPDATE statement using the following formula:

UPDATE TableName
SET ColumnName = Expression
WHERE Condition(s)

The WHERE operator allows you to specify how the particular record involved would be identified. Here is an example:

void btnUpdateRecord_Click(object sender, EventArgs e)
{
    SqlConnection connection =
        new SqlConnection("Data Source=(local);" +
                  "Database='Exercise';" +
                  "Integrated Security=yes;");
    SqlCommand command = new SqlCommand("UPDATE Employees " +
                                        "SET EmployeeName = 'Henry Helmonds' " +
                                        "WHERE EmployeeNumber = '72-644';",
					connection);

    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
} 
 
 
 
   
 

Home Copyright © 2009-2015, FunctionX, Inc.