Ich bin dabei, eine komplizierte select-Anweisung zu vereinfachen, und dachte, ich würde allgemeine Tabellenausdrücke verwenden.
Das Deklarieren eines einzelnen Cte funktioniert einwandfrei.
WITH cte1 AS (
SELECT * from cdr.Location
)
select * from cte1
Ist es möglich, mehr als ein Byte im selben SELECT zu deklarieren und zu verwenden?
dh diese SQL gibt einen Fehler
WITH cte1 as (
SELECT * from cdr.Location
)
WITH cte2 as (
SELECT * from cdr.Location
)
select * from cte1
union
select * from cte2
der fehler ist
Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
NB. Ich habe versucht, Semikolons einzufügen und erhalte diesen Fehler
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ';'.
Wahrscheinlich nicht relevant, aber dies ist in SQL 2008.
Ich denke, es sollte so etwas wie sein:
WITH
cte1 as (SELECT * from cdr.Location),
cte2 as (SELECT * from cdr.Location)
select * from cte1 union select * from cte2
Grundsätzlich ist WITH
hier nur eine Klausel, und wie die anderen Klauseln, die Listen enthalten, ist "," das entsprechende Trennzeichen.
Die oben genannte Antwort ist richtig:
WITH
cte1 as (SELECT * from cdr.Location),
cte2 as (SELECT * from cdr.Location)
select * from cte1 union select * from cte2
Zusätzlich können Sie in cte2 auch von cte1 abfragen:
WITH
cte1 as (SELECT * from cdr.Location),
cte2 as (SELECT * from cte1 where val1 = val2)
select * from cte1 union select * from cte2
val1,val2
sind nur Annahmen für Ausdrücke ..
Hoffe, dieser Blog wird auch helfen: http://iamfixed.blogspot.de/2017/11/common-table-expression-in-sql-with.html