Common Table Experssion (CTE) Format

Posted on 2011-10-12
Medium Priority
Last Modified: 2012-05-12
In SQL 2008 Management Studio I developed a CTE in a view. My question or issue is that when I reopen the view I lose the begining syntax of the CTE. In other words my CTE starts as  With XYZ as (Select A, B, C etc),  I then save the view and when I reopen it the With XYZ as ( is gone, so the view now only shows Select A, B, C etc.....

I also developed a stored procedure using this same view and it works ok. So overall my application is OK, however why would I lose that syntax when reopening the CTE?
Question by:SeTech

Accepted Solution

DavidMorrison earned 2000 total points
ID: 36955248
Hi SeTech,

Are you creating & subsequently "opening" the view in the built in editor in SSMS? (rmb on view -> design)

If so this may be why, the editor does some odd things to SQL, I'd suggest writing the code direct in a query and to subsequently edit it do script view as Alter


LVL 75

Expert Comment

by:Anthony Perkins
ID: 36955533
As alluded to in the previous comment, you have come across one of the many "quirks" of the Designer tool and why most SQL Developers that have used SSMS more than a week avoid it like the plague.
LVL 13

Expert Comment

ID: 36955600
Hi, If you are creating something like the following, it should work.
create view v1 as 
with t(a,b) as
(select 1 as a,2 as b union select 3 as a,4 as b)
select * from t

Open in new window


Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question