Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: How to change name in a view?

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

like image 312
Fabio Milheiro Avatar asked Sep 10 '09 00:09

Fabio Milheiro


People also ask

How do I edit an existing SQL view?

To modify a view In Object Explorer, click the plus sign next to the database where your view is located and then click the plus sign next to the Views folder. Right-click on the view you wish to modify and select Design.

Can I update a view in SQL Server?

If you remember the CREATE VIEW SQL syntax, a view can be modified by simply using the ALTER VIEW keyword instead, and then changing the structure of the SELECT statement. Therefore, let's change the previously created view with the CREATE VIEW SQL statement by using the ALTER VIEW statement.

Can you edit data in a view?

Yes underlying table data can be updated by Updating a view. The point to note here is, as long as the View is created based on one single table then direct "Update View" statement would work. But if the view is created based on multiple tables then a direct Update statement won't work.


1 Answers

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

like image 75
MaxiWheat Avatar answered Oct 03 '22 18:10

MaxiWheat