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
 alter view issue

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-05-26 : 12:15:21
Im trying to check if a view exists and if it does alter it

im using:

IF OBJECT_ID('view1', 'V') IS NOT NULL

BEGIN

ALTER VIEW view1

AS

select top 10 *
from tabletest

END

im getting Incorrect syntax near the keyword 'VIEW'.

the error seems to be with the begin/end. what am i doing wrong?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-05-26 : 12:37:17
You can't put anything before the ALTER statement. You could try drop and recreate the view


IF OBJECT_ID('view1', 'V') IS NOT NULL
DROP VIEW view1
GO

CREATE VIEW view1
AS

select top 10 *
from tabletest


Jim


Everyday I learn something that somebody else already knew
Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-05-26 : 12:43:50
ok thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-27 : 03:08:37
or use dynamic sql

IF OBJECT_ID('view1', 'V') IS NOT NULL


EXEC('ALTER VIEW view1

AS

select top 10 *
from tabletest
')


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -