SQL Server: How to change name in a view?

Fabio Milheiro picture Fabio Milheiro · Sep 10, 2009 · Viewed 32.3k times · Source

I am using Visual Studio 2008 and SQL Server 2008 Express.

How can I change the name of the view? I can change tables' names, but I can't change the view name.

Any suggestion?

Thank you, Fabio Milheiro

Answer

MaxiWheat picture MaxiWheat · Sep 10, 2009

You can use the ALTER VIEW statement something like this :

ALTER VIEW dbo.myView
AS
SELECT foo
FROM dbo.bar
WHERE widget = 'foo'
GO

Reference on MSDN

To rename a view, use sp_rename System Stored Procedure :

EXEC sp_rename 'dbo.myView', 'myNewViewName'

Note: don't include the schema name in the second string, or else you'll get a name like "dbo.dbo.myNewViewName".