Friday, May 14, 2010

SQL SERVER – How To Update a Record of a Table

Update is the one of the data manipulation Language statement which can be used to change the existing data in a table or a view.

We will see the syntax of the Update statement :

Syntax :

UPDATE                                                                        [ TOP ( expression ) [PERCENT ] ]{<object>}                                                                   SET                                                                             {column_name = { expression | DEFAULT |NULL}               | @variable = expression                                                                    | @variable = column = expression                                                                   | column_name { += | -= | *= | /= | %= | &= | ^= | |= } expression                                                                   | @variable { += | -= | *= | /= | %= | &= | ^= | |= } expression                                                                    | @variable = column { += | -= | *= | /= | %= | &= | ^= | |= } expression} [ ,...n ]   

<object> ::=                                                                  {                                                                                [ server_name .database_name .schema_name .                  |database_name.[schema_name ].                                            |schema_name .                                                            ]  table_or_view_name}

TOP ( expression ) [ PERCENT ]

Specifies the number or percent of rows that will be updated. expression can be either a number or a percent of the rows.

server_name

Is the name of the linked server on which the table or view is located.

database_name

Is the name of the database.

schema_name

Is the name of the schema to which the table or view belongs.

table_or view_name

Is the name of the table or view from which the rows are to be updated.

SET

Specifies the list of column or variable names to be updated.

column_name

Is a column that contains the data to be changed.

expression

Is a variable, literal value, expression, or a sub select statement (enclosed with parentheses) that returns a single value.

Example :

Let us see the example for updating the manager Id and salary of Employee ‘Emp005’  in the Employee Table

Update Employee                                                         
Set                                                                             
MgrId= 'MGR002',                                                         
Salary = Salary + 20000
Where EmpId = 'Emp005'

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.