Archive

Posts Tagged ‘update’

Update table from select.

November 18th, 2010 No comments

Here’s a small example how you can update a table from a select statement.

UPDATE Contact
SET Contact.Surname = x.Surname
FROM (
	SELECT Id, SUBSTRING(Surname, Len(Insertion) + 2, LEN(Surname) - Len(Insertion)) AS Surname
	FROM Contact
	WHERE Insertion IS NOT NULL
	AND Insertion <> ''
	AND CHARINDEX(Insertion + ' ', Surname) = 1) x
WHERE Contact.Id = x.Id;

Using a join is also possible.

UPDATE c
SET c.Body = x.Body, c.Summary = x.Summary
FROM Content c
JOIN PageContent x ON c.ID = x.ID;
Categories: General Tags: , ,