Cannot save query as a View in sql 2008

Posted on 2010-01-08
Last Modified: 2012-05-08
Hi Experts,
I have a piece of code I would like to save as a VIEW but when I try to save the VIEW, I receive an error gui
"Incorrect syntax near the key word 'declare'"
A second gui then appears

User canceled out of save dialog
 (MS Visual Database Tools)

Can I save a VIEW with a declare statement?

The code I used is :

declare @dt1 as datetime, @dt2 as datetime
set @dt1 = '01-' + right('00' + cast(month(DATEADD(m,-6,getdate())) as varchar(2)),2) + '-' + cast(YEAR(DATEADD(m,-6,getdate())) as varchar(4))
set @dt2 = '01-' + right('00' + cast(month(getdate()) as varchar(2)),2) + '-' + cast(YEAR(getdate()) as varchar(4))
SELECT     TOP (100) PERCENT [FC] AS S ,month ([FD]) as D, SUM([FP]) AS F, SUM([CFPC]) AS C, SUM([TOB]) AS T
FROM         dbo.OPS
WHERE     ([CFPC] > 0) AND  CONVERT(VARCHAR(10),([FD]), 101)  >= CONVERT(VARCHAR(10),(@dt1) , 103)

GROUP BY [FC], month ([FD])
Question by:chrislindsay
    LVL 26

    Accepted Solution

    use it like this
    SELECT     TOP (100) PERCENT [FC] AS S ,month ([FD]) as D, SUM([FP]) AS F, SUM([CFPC]) AS C, SUM([TOB]) AS T
    FROM         dbo.OPS
    WHERE     ([CFPC] > 0) AND  CONVERT(VARCHAR(10),([FD]), 101)  >= CONVERT(VARCHAR(10),('01-' + right('00' + cast(month(DATEADD(m,-6,getdate())) as varchar(2)),2) + '-' + cast(YEAR(DATEADD(m,-6,getdate())) as varchar(4))) , 103)
    GROUP BY [FC], month ([FD])

    Open in new window

    LVL 26

    Expert Comment

    by:Chris Luttrell
    so as tigin44 has shown in his example
    the answer to your question of "Can I save a VIEW with a declare statement?" is NO, sorry.  You will have to replace any references to your @dt1 and @dt2 with the expressions you have in your set statements.
    LVL 10

    Expert Comment

    if you need to use variables and more complicated features you should use table-valued function rather than view. Small change in naming and CREATE syntax but you query it almost exactly the same.

    but SETs do not look/perform good so it may be worth changing it to something what tigin44 suggested this time (and whenever possible.
    LVL 75

    Expert Comment

    by:Anthony Perkins
    Also, doing the following kludge:


    May have worked in MS Access and earlier versions of MS SQL Server, but (thankfully) is totally pointless in MS SQL Server 2005/2008

    Author Closing Comment

    Thanks for your help, after the change the View works well.  Thanks to all the other comments which were useful

    Author Comment

    One note of comment administrators.  Accepted solution can not be seen in experts-exchange/mobile on iphone as maybe because of format issues of code snippet box.

    Thanks for all the other comments Experts, much appresiated!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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.
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now