SQL Server/T-SQL/Insert Delete Update/Update

Материал из SQL эксперт
Перейти к: навигация, поиск

An UPDATE statement can only modify data of a single table

   <source lang="sql">

1> CREATE TABLE works_on (emp_no INTEGER NOT NULL, 2> project_no CHAR(4) NOT NULL, 3> job CHAR (15) NULL, 4> enter_date DATETIME NULL) 5> 6> insert into works_on values (1, "p1", "analyst", "1997.10.1") 7> insert into works_on values (1, "p3", "manager", "1999.1.1") 8> insert into works_on values (2, "p2", "clerk", "1998.2.15") 9> insert into works_on values (2, "p2", NULL, "1998.6.1") 10> insert into works_on values (3, "p2", NULL, "1997.12.15") 11> insert into works_on values (4, "p3", "analyst", "1998.10.15") 12> insert into works_on values (5, "p1", "manager", "1998.4.15") 13> insert into works_on values (6, "p1", NULL, "1998.8.1") 14> insert into works_on values (7, "p2", "clerk", "1999.2.1") 15> insert into works_on values (8, "p3", "clerk", "1997.11.15") 16> insert into works_on values (7, "p1", "clerk", "1998.1.4") 17> 18> select * from works_on 19> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) emp_no project_no job enter_date


---------- --------------- -----------------------
         1 p1         analyst         1997-10-01 00:00:00.000
         1 p3         manager         1999-01-01 00:00:00.000
         2 p2         clerk           1998-02-15 00:00:00.000
         2 p2         NULL            1998-06-01 00:00:00.000
         3 p2         NULL            1997-12-15 00:00:00.000
         4 p3         analyst         1998-10-15 00:00:00.000
         5 p1         manager         1998-04-15 00:00:00.000
         6 p1         NULL            1998-08-01 00:00:00.000
         7 p2         clerk           1999-02-01 00:00:00.000
         8 p3         clerk           1997-11-15 00:00:00.000
         7 p1         clerk           1998-01-04 00:00:00.000

(11 rows affected) 1> 2> -- An UPDATE statement can only modify data of a single table. 3> 4> 5> UPDATE works_on 6> SET job = "Manager" 7> WHERE emp_no = 2 8> AND project_no = "p2" 9> GO (2 rows affected) 1> select * from works_on 2> GO emp_no project_no job enter_date


---------- --------------- -----------------------
         1 p1         analyst         1997-10-01 00:00:00.000
         1 p3         manager         1999-01-01 00:00:00.000
         2 p2         Manager         1998-02-15 00:00:00.000
         2 p2         Manager         1998-06-01 00:00:00.000
         3 p2         NULL            1997-12-15 00:00:00.000
         4 p3         analyst         1998-10-15 00:00:00.000
         5 p1         manager         1998-04-15 00:00:00.000
         6 p1         NULL            1998-08-01 00:00:00.000
         7 p2         clerk           1999-02-01 00:00:00.000
         8 p3         clerk           1997-11-15 00:00:00.000
         7 p1         clerk           1998-01-04 00:00:00.000

(11 rows affected) 1> drop table works_on 2> GO 1>

      </source>
   
  


Change more than one column by adding a comma and the additional column expression

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> --Change more than one column by adding a comma and the additional column expression 4> 5> 6> UPDATE Employee 7> SET city = "There", name = "CA" 8> WHERE id = 1 9> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 CA               40420 1994-02-01 00:00:00.000 There      W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> drop table employee 4> GO 1>

      </source>
   
  


Math operator in update statement

   <source lang="sql">

2> 3> 4> CREATE TABLE titles( 5> title_id varchar(20), 6> title varchar(80) NOT NULL, 7> type char(12) NOT NULL, 8> pub_id char(4) NULL, 9> price money NULL, 10> advance money NULL, 11> royalty int NULL, 12> ytd_sales int NULL, 13> notes varchar(200) NULL, 14> pubdate datetime NOT NULL 15> ) 16> GO 1> 2> insert titles values ("1", "Secrets", "popular_comp", "1389", $20.00, $8000.00, 10, 4095,"Note 1","06/12/94") 3> insert titles values ("2", "The", "business", "1389", $19.99, $5000.00, 10, 4095,"Note 2","06/12/91") 4> insert titles values ("3", "Emotional", "psychology", "0736", $7.99, $4000.00, 10, 3336,"Note 3","06/12/91") 5> insert titles values ("4", "Prolonged", "psychology", "0736", $19.99, $2000.00, 10, 4072,"Note 4","06/12/91") 6> insert titles values ("5", "With", "business", "1389", $11.95, $5000.00, 10, 3876,"Note 5","06/09/91") 7> insert titles values ("6", "Valley", "mod_cook", "0877", $19.99, $0.00, 12, 2032,"Note 6","06/09/91") 8> insert titles values ("7", "Any?", "trad_cook", "0877", $14.99, $8000.00, 10, 4095,"Note 7","06/12/91") 9> insert titles values ("8", "Fifty", "trad_cook", "0877", $11.95, $4000.00, 14, 1509,"Note 8","06/12/91") 10> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> 2> UPDATE titles 3> SET price = price * 1.1 4> WHERE title_id LIKE "BU%" 5> GO (0 rows affected) 1> 2> drop table titles; 3> GO 1>

</source>
   
  


Modify rows of a table with an expression

   <source lang="sql">

1> CREATE TABLE project (project_no CHAR(4) NOT NULL, 2> project_name CHAR(15) NOT NULL, 3> budget FLOAT NULL) 4> 5> insert into project values ("p1", "Search Engine", 120000.00) 6> insert into project values ("p2", "Programming", 95000.00) 7> insert into project values ("p3", "SQL", 186500.00) 8> 9> select * from project 10> GO (1 rows affected) (1 rows affected) (1 rows affected) project_no project_name budget


--------------- ------------------------

p1 Search Engine 120000 p2 Programming 95000 p3 SQL 186500 (3 rows affected) 1> 2> -- Modify rows of a table with an expression. 3> 4> UPDATE project SET budget = budget*0.56 5> GO (3 rows affected) 1> select * from project 2> GO project_no project_name budget


--------------- ------------------------

p1 Search Engine 67200 p2 Programming 53200.000000000007 p3 SQL 104440.00000000001 (3 rows affected) 1> drop table project 2> GO 1>

      </source>
   
  


Reference a declared variable in an update statement

   <source lang="sql">

2> CREATE TABLE employee( 3> id INTEGER NOT NULL PRIMARY KEY, 4> first_name VARCHAR(10), 5> last_name VARCHAR(10), 6> salary DECIMAL(10,2), 7> start_Date DATETIME, 8> region VARCHAR(10), 9> city VARCHAR(20), 10> managerid INTEGER 11> ); 12> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> SET QUOTED_IDENTIFIER OFF 4> GO 1> 2> DECLARE @ValueToUpdate VARCHAR(30) 3> SET @ValueToUpdate = "Mick" 4> 5> UPDATE Employee 6> SET First_Name = @ValueToUpdate 7> GO (9 rows affected) 1> select * from Employee; 2> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Mick       Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Mick       Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 Mick       Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Mick       Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Mick       Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Mick       Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 Mick       Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 Mick       Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Mick       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> drop table employee; 4> GO

</source>
   
  


Update a single column

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> -- Update the value in the city column: 3> 4> UPDATE Employee SET city = "Vancouver" WHERE id = 1 5> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 Vancouver  W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> drop table employee 3> GO 1>

      </source>
   
  


Update a single row

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> UPDATE Employee SET Salary = 1575 WHERE ID = 5 3> GO (1 rows affected) 1> 2> SELECT * FROM Employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David             1575 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> drop table employee 3> GO 1>

      </source>
   
  


Update command with where condition

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> UPDATE Employee SET Name = "Lee Smith" 4> WHERE City = "Toronto" 5> GO (3 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Lee Smith        24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 Lee Smith        70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Lee Smith        60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> drop table employee 3> GO 1>

      </source>
   
  


Update more than one columns in update clause

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> -- Update more than one columns in update clause 3> BEGIN TRAN Restore_Value 4> 5> DECLARE @ValueToUpdate VARCHAR(30) 6> 7> SET @ValueToUpdate = "S B" 8> 9> UPDATE Employee 10> SET Name = @ValueToUpdate, 11> City = "New City", 12> Salary = 0 13> WHERE ID = 5 14> 15> COMMIT TRAN 16> 17> SELECT * FROM Employee 18> 19> drop table employee 20> GO (1 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 S B                  0 1998-10-05 00:00:00.000 New City   W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1>

      </source>
   
  


Update statement and IN operator

   <source lang="sql">

26> 27> CREATE TABLE employee (emp_no INTEGER NOT NULL, 28> emp_fname CHAR(20) NOT NULL, 29> emp_lname CHAR(20) NOT NULL, 30> dept_no CHAR(4) NULL) 31> 32> insert into employee values(1, "Matthew", "Smith", "d3") 33> insert into employee values(2, "Ann", "Jones", "d3") 34> insert into employee values(3, "John", "Barrimore","d1") 35> insert into employee values(4, "James", "James", "d2") 36> insert into employee values(5, "Elsa", "Bertoni", "d2") 37> insert into employee values(6, "Elke", "Hansel", "d2") 38> insert into employee values(7, "Sybill", "Moser", "d1") 39> 40> select * from employee 41> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) emp_no emp_fname emp_lname dept_no


-------------------- -------------------- -------
         1 Matthew              Smith                d3
         2 Ann                  Jones                d3
         3 John                 Barrimore            d1
         4 James                James                d2
         5 Elsa                 Bertoni              d2
         6 Elke                 Hansel               d2
         7 Sybill               Moser                d1

(7 rows affected) 1> 2> 3> CREATE TABLE works_on (emp_no INTEGER NOT NULL, 4> project_no CHAR(4) NOT NULL, 5> job CHAR (15) NULL, 6> enter_date DATETIME NULL) 7> 8> insert into works_on values (1, "p1", "analyst", "1997.10.1") 9> insert into works_on values (1, "p3", "manager", "1999.1.1") 10> insert into works_on values (2, "p2", "clerk", "1998.2.15") 11> insert into works_on values (2, "p2", NULL, "1998.6.1") 12> insert into works_on values (3, "p2", NULL, "1997.12.15") 13> insert into works_on values (4, "p3", "analyst", "1998.10.15") 14> insert into works_on values (5, "p1", "manager", "1998.4.15") 15> insert into works_on values (6, "p1", NULL, "1998.8.1") 16> insert into works_on values (7, "p2", "clerk", "1999.2.1") 17> insert into works_on values (8, "p3", "clerk", "1997.11.15") 18> insert into works_on values (7, "p1", "clerk", "1998.1.4") 19> 20> select * from works_on 21> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) emp_no project_no job enter_date


---------- --------------- -----------------------
         1 p1         analyst         1997-10-01 00:00:00.000
         1 p3         manager         1999-01-01 00:00:00.000
         2 p2         clerk           1998-02-15 00:00:00.000
         2 p2         NULL            1998-06-01 00:00:00.000
         3 p2         NULL            1997-12-15 00:00:00.000
         4 p3         analyst         1998-10-15 00:00:00.000
         5 p1         manager         1998-04-15 00:00:00.000
         6 p1         NULL            1998-08-01 00:00:00.000
         7 p2         clerk           1999-02-01 00:00:00.000
         8 p3         clerk           1997-11-15 00:00:00.000
         7 p1         clerk           1998-01-04 00:00:00.000

(11 rows affected) 1> 2> 3> UPDATE works_on SET job = NULL WHERE emp_no IN 4> (SELECT emp_no FROM employee WHERE emp_lname = "Jones") 5> GO (2 rows affected) 1> select * from works_on 2> GO emp_no project_no job enter_date


---------- --------------- -----------------------
         1 p1         analyst         1997-10-01 00:00:00.000
         1 p3         manager         1999-01-01 00:00:00.000
         2 p2         NULL            1998-02-15 00:00:00.000
         2 p2         NULL            1998-06-01 00:00:00.000
         3 p2         NULL            1997-12-15 00:00:00.000
         4 p3         analyst         1998-10-15 00:00:00.000
         5 p1         manager         1998-04-15 00:00:00.000
         6 p1         NULL            1998-08-01 00:00:00.000
         7 p2         clerk           1999-02-01 00:00:00.000
         8 p3         clerk           1997-11-15 00:00:00.000
         7 p1         clerk           1998-01-04 00:00:00.000

(11 rows affected) 1> drop table works_on 2> drop table employee 3> GO 1>

      </source>
   
  


UPDATE with a Join

   <source lang="sql">

15> 16> CREATE TABLE employee( 17> id INTEGER NOT NULL PRIMARY KEY, 18> first_name VARCHAR(10), 19> last_name VARCHAR(10), 20> salary DECIMAL(10,2), 21> start_Date DATETIME, 22> region VARCHAR(10), 23> city VARCHAR(20) 24> ); 25> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver"); 2> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown"); 3> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris"); 4> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London"); 5> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton"); 6> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary"); 7> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York"); 8> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina"); 9> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto"); 10> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> select * from employee; 2> GO id first_name last_name salary start_Date region city


---------- ---------- ------------ ----------------------- ---------- --------------------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto

(9 rows affected) 1> 2> CREATE TABLE title( 3> id INTEGER, 4> job_title VARCHAR(20) 5> ); 6> GO 1> INSERT INTO title VALUES (1, "developer"); 2> INSERT INTO title VALUES (1, "manager"); 3> INSERT INTO title VALUES (2, "tester"); 4> INSERT INTO title VALUES (2, "programmer"); 5> INSERT INTO title VALUES (3, "boss"); 6> INSERT INTO title VALUES (4, "sales"); 7> INSERT INTO title VALUES (5, "market"); 8> INSERT INTO title VALUES (6, "coder"); 9> INSERT INTO title VALUES (7, "tester"); 10> INSERT INTO title VALUES (8, "developer"); 11> INSERT INTO title VALUES (9, "manager"); 12> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> 2> 3> UPDATE E 4> SET Salary = Salary + 0.05 5> FROM 6> Employee AS E 7> JOIN 8> Title AS T ON E.id = T.id 9> WHERE 10> T.ID = 1 11> 12> select * from employee 13> GO (1 rows affected) id first_name last_name salary start_Date region city


---------- ---------- ------------ ----------------------- ---------- --------------------
         1 Jason      Martin          5890.05 2005-03-22 00:00:00.000 North      Vancouver
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto

(9 rows affected) 1> 2> drop table employee; 3> drop table title; 4> GO

</source>
   
  


Updating a Column Value

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> --Updating a Column Value 3> 4> 5> CREATE PROC usp_UpdateName 6> @ID int, 7> @Name nvarchar(50) 8> AS 9> UPDATE dbo.Employee 10> SET Name = @Name 11> WHERE ID = @ID 12> GO 1> 2> EXEC usp_UpdateName 2,"DBA" 3> GO (1 rows affected) 1> 2> SELECT * 3> FROM dbo.Employee 4> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 DBA              14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> drop table employee 3> GO 1>

      </source>
   
  


Updating Rows Based on Multiple Tables

   <source lang="sql">

1> 2> create table employee( 3> ID int, 4> name nvarchar (10), 5> salary int ) 6> GO 1> 2> create table job( 3> ID int, 4> title nvarchar (10), 5> averageSalary int) 6> GO 1> 2> 3> insert into employee (ID, name, salary) values (1, "Jason", 1234) 4> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (2, "Robert", 4321) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (3, "Celia", 5432) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (4, "Linda", 3456) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (5, "David", 7654) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (6, "James", 4567) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (7, "Alison", 8744) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (8, "Chris", 9875) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (9, "Mary", 2345) 2> GO (1 rows affected) 1> 2> insert into job(ID, title, averageSalary) values(1,"Developer",3000) 3> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000) 2> GO (1 rows affected) 1> 2> 3> 4> select * from employee; 5> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> select * from job; 2> GO ID title averageSalary


---------- -------------
         1 Developer           3000
         2 Tester              4000
         3 Designer            5000
         4 Programmer          6000

(4 rows affected) 1> 2> -- Updating Rows Based on Multiple Tables 3> 4> UPDATE Employee 5> SET Salary = 4000 6> FROM Employee INNER JOIN Job 7> ON Employee.ID = Job.ID 8> GO (4 rows affected) 1> 2> select * from employee 3> GO ID name salary


---------- -----------
         1 Jason             4000
         2 Robert            4000
         3 Celia             4000
         4 Linda             4000
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> 2> drop table employee; 3> drop table job; 4> GO 1> 2>

      </source>
   
  


Use CASE in the UPDATE statement

   <source lang="sql">

27> 28> CREATE TABLE project (project_no CHAR(4) NOT NULL, 29> project_name CHAR(15) NOT NULL, 30> budget FLOAT NULL) 31> 32> insert into project values ("p1", "Search Engine", 120000.00) 33> insert into project values ("p2", "Programming", 95000.00) 34> insert into project values ("p3", "SQL", 186500.00) 35> 36> select * from project 37> GO (1 rows affected) (1 rows affected) (1 rows affected) project_no project_name budget


--------------- ------------------------

p1 Search Engine 120000 p2 Programming 95000 p3 SQL 186500 (3 rows affected) 1> 2> -- Use CASE in the UPDATE statement. 3> 4> UPDATE project SET budget = CASE 5> WHEN budget > 0 and budget < 100000 THEN budget* 1.2 6> WHEN budget > = 100000 and budget < 200000 THEN budget* 1.1 7> ELSE budget* 1.05 8> END 9> GO (3 rows affected) 1> select * from project 2> GO project_no project_name budget


--------------- ------------------------

p1 Search Engine 132000 p2 Programming 114000 p3 SQL 205150.00000000003 (3 rows affected) 1> drop table project 2> GO 1> 2>

      </source>
   
  


Using an expression in our UPDATE statement

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> -- Using an expression in our UPDATE statement: 4> 5> UPDATE Employee 6> SET salary = salary / 1.1 7> WHERE city LIKE "N%" 8> GO (3 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            36745 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            36927 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           82381 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> 4> drop table employee 5> GO 1>

      </source>