8

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

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96

3 Answers3

24

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".

TheRubberDuck
  • 368
  • 4
  • 12
MaxiWheat
  • 6,133
  • 6
  • 47
  • 76
  • OK, but where do I set the new name of the myView view? Can't find it in the code you posted. – Fabio Milheiro Sep 10 '09 at 14:10
  • A word of warning in case of 'sp_rename' - `Renaming a stored procedure, function, view, or trigger will not change the name of the corresponding object name in the definition column of the sys.sql_modules catalog view. Therefore, we recommend that sp_rename not be used to rename these object types. Instead, drop and re-create the object with its new name.` – Gautam Kumar Samal Apr 22 '17 at 12:12
3

you can use the gui in sms

you can right click view, edit it, then do generate script if you want the code

mson
  • 7,762
  • 6
  • 40
  • 70
0

You can use the ALTER command or CREATE a new one and delete the old one.

-Shaun

Shaun McDonnell
  • 451
  • 4
  • 10