×

Good News Everybody!

The new search engine is LIVE!

Please report any problems to david (at) midrange.com.




Hi all,
I'm not sure if this is allowed so I'm checking with the group.
I'm trying to use a common table expression (CTE) with a MERGE statement.

Glenn,

It looks like the table reference in the MERGE statement does not support common table expressions, although it appears from the documentation it should.

I tested using the following script:

Create Table
QTEMP.NEWTABLE As (
Select
Cast(0 As Integer) As ID,
Cast('' As Char(10)) As NAME
From
(Values 1) As A
) With No Data;

-- This doesn't work
-- Fails with SQL State 42601
Merge Into
QTEMP.NEWTABLE As OLD
Using
(With MYCTE As (
Select
Cast(1 As Integer) As ID,
Cast('JIM' As Char(10)) As NAME
From
(Values 1) As A)
Select
*
From
MYCTE) As NEW
On
(OLD.ID = NEW.ID)
When Not matched Then
Insert Values(NEW.ID, NEW.NAME)
When Matched Then
Update Set OLD.NAME = NEW.NAME;

-- This does work
Merge Into
QTEMP.NEWTABLE As OLD
Using
(
Select
Cast(1 As Integer) As ID,
Cast('JIM' As Char(10)) As NAME
From
(Values 1) As A) As NEW
On
(OLD.ID = NEW.ID)
When Not matched Then
Insert Values(NEW.ID, NEW.NAME)
When Matched Then
Update Set OLD.NAME = NEW.NAME;

Jim

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.