Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Table expression error

Author  Topic 

mavershang
Posting Yak Master

111 Posts

Posted - 2009-01-07 : 13:52:04
Hello all. I have a question about table expression.

With A As
(
......
......
),
B As
(
......
......
)

Then it pump error up when I want to select from B.

If I do it like
------------------
With A As
(
.....
)

With B As
(
....
)
--------------------

The error is "If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon."

What should I do to fix it?

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-07 : 13:53:41
Add a semicolon like the error message says.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-01-07 : 14:26:01
but where should I add it to?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-07 : 14:28:04
I see people just do this for each "With"

;With A As
(
...)

;With B As
(
...
)

That way you ensure you always have a semicolon before the table expression.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-01-07 : 14:38:26
It is terminated by error: Incorrect syntax near ';'.
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-01-07 : 15:44:12
Finally, I only use one such CTE. But it seems it could only be select once.
---------------------------------
With A as
(....)

select .....
from A
(It works)

select .....
from A
(It fail)
---------------------------------


anyone could tell me the reason?

Thanks a lot.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 15:56:48
It's because of SCOPE!

CTE's can only be used by following statement, due to scope limitation. However you can reference to the CTE up to 256 times in same following statement.
Yes, it is documented in Books Online.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 15:57:29
And you are corrent in using comma to declare multiple CTE's

;With A As
(
......
......
),
B As
(
......
......
)

will work.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-01-07 : 16:12:23
Thanks a lot.

quote:
Originally posted by Peso

And you are corrent in using comma to declare multiple CTE's

;With A As
(
......
......
),
B As
(
......
......
)

will work.



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page
   

- Advertisement -