HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Is this valid SQL Server Update syntax?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thisupdatesqlsyntaxvalidserver

Problem

Today I was reading some of my database notes that were taken long time ago. And I saw this query:

UPDATE Sales.Orders
SET (shipcountry, shipregion, shipcity)=
(SELECT country, region, city
FROM Sales.Customers AS C
WHERE C.custid =Sales.Orders.custid)
WHERE custid IN
(SELECT custid FROM Sales.Customers WHERE country = 'USA');


I'm really unfamiliar with this syntax. But my note says it's "valid SQL Server update syntax". Is this true? SSMS can't parse it. When I tried to run it, I got this:

Also SQL Server's UPDATE documentation seems suggest that ( is not allowed after the SET keyword. Anyway, I just want to confirm.

Solution

The syntax:

...
SET (shipcountry, shipregion, shipcity) =
    ( SELECT country, region, city 
      ...
    )
...


is valid SQL syntax but not valid SQL Server syntax. It has not yet been implemented in any version of it.

The (expression1, expression2, ...) is called Row constructor and it can be used in various places, e.g. in WHERE / WHEN / HAVING conditions, in UPDATE .. SET (as your question), in VALUES (...) syntax (often used in INSERT statements). In SQL Server it is allowed only in the third case (VALUES), implemented in version 2008.

Other DBMSs (Postgres, Oracle, DB2) have implemented this syntax, fully or partially.

Code Snippets

...
SET (shipcountry, shipregion, shipcity) =
    ( SELECT country, region, city 
      ...
    )
...

Context

StackExchange Database Administrators Q#317686, answer score: 8

Revisions (0)

No revisions yet.