470,596 Members | 1,574 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Incorrect syntax with Declare

Good morning
I hope this will be a simple one, but I am trying to write a view to query my SQL server database, setting a variable using "declare". The code runs fine and returns a number of records, but when I try to save it, it comes up with "Incorrect syntax near the keyword DECLARE", and will not save. I am a bit of a novice when it comes to SQL, but I don't understand why it runs, but won't save. Here is my variable:
DECLARE @ANCHORDATE AS DATETIME
SET @ANCHORDATE = CASE WHEN datepart(mm, getdate()) BETWEEN 4 AND 9 THEN dateadd(mm, 3, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)) ELSE dateadd(mm, - 9,
DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)) END

Thanks for any help.
Jun 28 '07 #1
2 2430
I am not 100% sure but you can try not to use DECLARE and try and save it. Let me know if it helps.
Jun 28 '07 #2
I have replaced all the instances of the variable with the actual definition, and the view runs and saves fine. I was hoping to be able to keep it simple by not having to type in the variable definition each time. I don't understand why it can run, but saving it returns an error.
Jun 28 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Jeff Magouirk | last post: by
13 posts views Thread by Yogesh Khanolkar | last post: by
16 posts views Thread by danu | last post: by
6 posts views Thread by ypjofficial | last post: by
1 post views Thread by Sandesh | last post: by
1 post views Thread by iporter | last post: by
1 post views Thread by griffel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.