DATABASE

SQL Server 2008 : Working with DML Queries - Using the UPDATE Statement (part 2)

1/16/2011 9:08:52 AM

Understanding the Column.WRITE Expression

SQL Server 2008 provides a capability to append or insert text into existing text columns as part of the UPDATE statement. This is achieved using the column. WRITE expression. Let’s examine the column.WRITE expression in more detail. The elements of the column.WRITE statement are shown in Example 5.

Example 5. UPDATE...SET Column.WRITE(...) Statement—Core Elements
UPDATE table_or_view
column_to_update { .WRITE (new_value, @Offset, @Length) }
[FROM ...
WHERE ... ]

The column.WRITE statement tells SQL Server to append, remove, or insert text into the existing text value in column_name (see Example 6). The @Length characters starting from @Offset of column_name are replaced with the new_value. If the new_value is set to NULL, @Length is ignored, and the value in column_name is truncated at the specified @Offset. Only columns of varchar(max), nvarchar(max), or varbinary(max) can be specified with this clause. When using the .WRITE method, you cannot qualify the column_name with a table name or table alias.

@Offset is the number of characters after which the new_value replaces the existing characters. If @Offset is NULL, the new_value will be appended to the end of the column, regardless of the @Length parameter. If @Offset is larger than the length of the column value, the operation will fail, and an error will be reported.

@Length is the number of characters to be replaced by the new_value. If @Length is NULL, the update operation removes all data from @Offset to the end of the column_name value.

Example 6. Using the Column.WRITE Statement
USE AdventureWorks
GO

-- Create a new table to hold advertising copy
CREATE TABLE AdvertisingCopy (AdvertisementName nvarchar(100),
AdvertisementText varchar(max))
GO
INSERT AdvertisingCopy VALUES
('Reflector', 'Be seen at night.')
GO
SELECT * FROM AdvertisingCopy
GO

-- AdvertisementText is 'Be seen at night.'
UPDATE AdvertisingCopy
SET AdvertisementText.WRITE(' by traffic coming behind you.', 16, 30)
WHERE AdvertisementName = 'Reflector'
SELECT * FROM AdvertisingCopy
GO

-- AdvertisementText is 'Be seen at night by traffic coming behind you.'
UPDATE AdvertisingCopy
SET AdvertisementText.WRITE('The most advanced reflector ever. ', 0, 0)
WHERE AdvertisementName = 'Reflector'
SELECT * FROM AdvertisingCopy
GO

-- AdvertisementText is 'The most advanced reflector ever. Be seen at night
by traffic coming behind you.'
UPDATE AdvertisingCopy
SET AdvertisementText.WRITE(Null, 34, NULL)

WHERE AdvertisementName = 'Reflector'
SELECT * FROM AdvertisingCopy
GO

-- AdvertisementText is 'The most advanced reflector ever.'
UPDATE AdvertisingCopy
SET AdvertisementText.WRITE('(Well, sort of...).', 28, NULL)
WHERE AdvertisementName = 'Reflector'
SELECT * FROM AdvertisingCopy
GO

-- AdvertisementText is 'The most advanced reflector (Well, sort of...).'


Warning

Remember that only columns of varchar(max), nvarchar(max), or varbinary(max) can be specified with this clause. When using the .WRITE method, you cannot qualify the column_name with a table name or table alias.


Exercise . Using the UPDATE Statement

In this exercise, we will practice using the UPDATE statement.

Before you begin, you must have the following software installed on your computer:

  • SQL Server 2008 (a free trial is available for download)

  • AdventureWorks sample database

This exercise does not depend on you completing any previous exercises. We will be modifying rows in the Production.Product table in the AdventureWorks database.

  1. Open SQL Server Management Studio. Click Start | All Programs | Microsoft SQL Server 2008 | SQL Server Management Studio.

  2. Create a new query against the AdventureWorks database.

  3. Examine and execute the following SELECT statement. This statement shows all rows with Road-350-W as the product model.

    SELECT Product.Name, DiscontinuedDate, ProductModel.Name as Model
    FROM Production.Product JOIN Production.ProductModel
    ON Product.ProductModelID = ProductModel.ProductModelID
    WHERE ProductModel.Name = 'Road-350-W'
    ORDER BY DiscontinuedDate DESC
    GO
  4. Create and execute an UPDATE statement that will set DiscontinuedDate to today’s date for all products with Road-350-W as the product model.

  5. Use the UPDATE statement to discontinue all products of model Road-350-W:

    UPDATE Production.Product
    SET DiscontinuedDate = GETDATE()
    FROM Production.Product JOIN Production.ProductModel
    ON Product.ProductModelID = ProductModel.ProductModelID
    WHERE ProductModel.Name = 'Road-350-W'
    GO
  6. Execute the SELECT statement again. Notice that the DiscontinuedDate has changed from NULL to today’s date value.

Tip

To quickly memorize the syntax of the UPDATE statement, think “UPDATE table SET column = value FROM table WHERE condition.” The verbs are UPDATE...SET...FROM...WHERE.


Other  
  •  Azure Programming Considerations
  •  Programming with SQL Azure : Record Navigation in WCF Data Services
  •  Defensive Database Programming with SQL Server : TRY...CATCH Gotchas (part 2) - TRY...CATCH blocks cannot catch all errors
  •  Defensive Database Programming with SQL Server : TRY...CATCH Gotchas (part 1) - Re-throwing errors
  •  SQL Server 2008 : Working with DML Queries - Using the INSERT Statement (part 2)
  •  SQL Server 2008 : Working with DML Queries - Using the INSERT Statement (part 1) - Using the INSERT Statement with the VALUES Clause
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 7) - Nested Tables
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 6) - Validating and Comparing Mining Models
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 5) - Viewing Mining Models
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 4) - Deploying and Processing Data Mining Objects
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone