Quote:
|
Originally Posted by db2Aishani
Hi I am having problem while using with clause in following syntax:
MERGE INTO tablename tr
USING ( with WTab as (
select distinct xxx as ID1, yyy as ID2 from table2
)
select ID1 as key from WTab
Union all
select ID2 as key from WTab
) KeyList
ON tr.Key=KeyList.Key
When NOT MATCHED THEN
INSERT ......
We get the syntax error saying "as following with Wtab is not valid"
where as when I remove the "with clause" and use the same table outside then it works fine.
So Is there any other systax I should be following or whether this MERGE statement can not work with "With" Clause?
Any help is appreciated.
|
I do not know if it will fix the entire problem, but the Common Table Expression (CTE) should look like this:
with WTab (ID1, ID2) as (
select distinct xxx, yyy from table2
)
select ID1 as key from WTab
Union all
select ID2 as key from WTab
Andy