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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Create VIEW with variable

Author  Topic 

Coromandel
Starting Member

1 Post

Posted - 2003-10-02 : 11:08:19
I'm trying to create a view that includes a variable in the select statement, along the following lines ...

CREATE VIEW nicktest AS
DECLARE @CurrYear int
SET @CurrYear=CASE .... GETDATE()...END
SELECT ...

The declaration and select statement works fine until I try to create a view from it. It insists on the create view being the first line and if I list it as above I get the following message
"Server: Msg 156, Level 15, State 1, Procedure trdm_SEEDA_nicktest, Line 2
Incorrect syntax near the keyword 'DECLARE'."

Does this need to be part of something else e.g procedure?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-02 : 12:54:32
You can't do this in a view. What you are trying to do can be done in a stored procedure. A view is just a select statement and that's it. A stored procedure can have much more logic. Take a look at the information about creating stored procedures in SQL Server Books Online.

Tara
Go to Top of Page
   

- Advertisement -