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
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 3) - Editing and Adding Mining Models
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 2) - Creating a Mining Model
  •  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 1) - Creating a Mining Structure
  •  Microsoft SQL Server 2005 : Report Management
  •  Microsoft SQL Server 2005 : Report Definition and Design (part 5) - Report Builder
  •  Defensive Database Programming with SQL Server : Using TRY...CATCH blocks to Handle Errors
  •  Microsoft SQL Server 2005 : Report Definition and Design (part 4)
  •  Microsoft SQL Server 2005 : Report Definition and Design (part 3)
  •  Microsoft SQL Server 2005 : Report Definition and Design (part 2) - Business Intelligence Development Studio
  •  The SQL Programming Language : Creating Tables and Entering Data
  •  
    Top 10
    A Look At Truecrypt The Open Source Security Tool
    Price Of Piracy
    Acer Aspire 5600U 23" Touchscreen All-in-One PC
    Zalman FX100-Cube Fanless Cooler
    Devolo dLAN LiveCam Starter Kit
    Has Apple Lost It? (Part 2)
    Has Apple Lost It? (Part 1)
    Sony Computer Entertainment (Part 3)
    Sony Computer Entertainment (Part 2)
    Sony Computer Entertainment (Part 1)
    Most View
    The Library In Your Pocket (Part 3) - Epilogue, E-Ink, Color E-Ink
    How To Turn Android Phone Into A Wireless Speaker
    TomTom Via 620 - TomTom At Large, TDK TH-EC200 - The Familiar Acronym
    Cookie D'oh, I Scream
    iPhone 3D Programming : Adding Shaders to ModelViewer (part 1) - New Rendering Engine
    Understanding the Windows 7 Deployment Life Cycle : Placing an MDT Deployment in the MOF Life Cycle
    Bring Your Own Confusion (Part 1)
    Windows Server 2008 : Transport-Level Security - Active Directory Rights Management Services
    iphone Programming : Integrating Your Application - Application Preferences
    Microsoft XNA Game Studio 3.0 : Program Bugs
    Windows System Programming : Exception Handling - Exceptions and Their Handlers
    Windows 7 : Indexing Your Computer for Faster Searches (part 3) - Optimizing File Properties for Indexing
    Asus’ Vulcan Pro Headset: No Noise!
    SteelsSeries Kinzu Pro Edition Mouse - Simplicity
    .NET Micro Framework : Execution Constraints
    Samsung NX1000 Camera Review (Part 2)
    iphone SDK : Using the ABUnknownPersonViewController Class, Using the ABPeoplePickerNavigationController Class
    Buying Guide: Memory Kit (Part 2) - Corsair Dominator GT With DHX Pro Connector, G. Skill RipjawsZ DDR3-2133 & Mushkin Enhanced Redline 16GB DDR3-2133
    Pogo Connect Blue Tooth 4.0 Smart Pen
    Exchange Server 2007 : Administrate Transport Settings - Configure Journaling