tags:

views:

20

answers:

3

If we want to change the name of MyColumnName to MyAlteredColumnName...

...and we have a SQL Server 2008 table that looks like:

MyTable
  MyColumnName

and a view that references the underlying column:

CREATE VIEW MyDependentView WITH SCHEMABINDING
AS
SELECT ..., MyTable.MyColumnName

We end up following this procedure:

  1. Dropping the View
  2. Altering MyTable.MyColumnName to MyTable.MyAlteredColumnName
  3. Recreating the View with a reference to MyAlteredColumnName

We do this with migrator dot net.

Is there a better way to do this? Is there T-SQL that will alter a view column name? Or any support in SQL Server 2008 for automagically tying the columns together?

+3  A: 

Without the use of a third-party tool, this is one of the only ways to do it. You can obviously also use ALTER VIEW instead of a DROP and CREATE.

It should be noted that Red-Gate makes a tool called SQL Refactor which will automate this sort of change (no I do not work for them). I'm sure there are other similar database refactoring tools out there.

Thomas
+1: @thomas. thanks. is your experience with SQL Refactor positive?
Rob
@Rob - I've used in the past and it worked well. It was very good at finding all places where a column might be used such as stored procs, views etc.
Thomas
+1  A: 

Use sp_refreshview:

EXEC sp_refreshview @viewName

If you want to refresh all your views, you'll have to iterate over a loop of them, which means dynamic SQL.

And if you layered them (a view is dependent on another view - bad), you'll have to refresh the parent first...

OMG Ponies
Won't that simply throw an error (when the view is executed) because the column referenced in the view no longer exists?
Thomas
@Thomas: You update the column name in the table, and then refresh the view. There's a window of opportunity for error, but this update would require that the app be offline from general use...
OMG Ponies
+1: @omg: interesting. so if you create the view without schema binding and do an sp_refreshview it will update the column name?
Rob
@Rob: Yes, that's what the function is for. I missed that it can't be used for views with SCHEMABINDING, but I thought SCHEMABINDING was only really necessary for Indexed Views...
OMG Ponies
@omg: awesome. thank you.
Rob
@OMG Ponies - As Inigo Montoya might say, "I do not think that does what you think it does". If you create view that references a column called "Foo". Rename the column to "Bar" and execute sp_refreshview it will either succeed without doing anything or throw an error because of an invalid column.
Thomas
@Thomas: Testing, BRB
OMG Ponies
Just tested, @Thomas is right. Throws 'Invalid column name 'blarch''. But it was a noble effort. "You killed my father, now prepare to die..."
Rob
@Rob: I just tested - if you hardcode column references from the view - yes, those will fail. Because they are hardcoded - if you use `SELECT *`, the updated column name displays correctly. `sp_refreshview` updates the *view*, **it doesn't fix your code references to columns within the view**
OMG Ponies
@omg: thanks for the extra effort. this isn't the first time you've given me a great answer. we are using explicit column references.
Rob
@Rob: Yeah, I use explicit column references too. It was a reach on my part; once I knew the scope of what you wanted, I voted for Thomas. In the past, I've done that stuff by hand.
OMG Ponies
+2  A: 

If it's a SELECT * view, you can call sp_refreshview, as OMG_Ponies suggested. It will recompile the view and update the column metadata appropriately. This is one area where judicious use of SELECT * could have benefits, if used appropriately within a coherent scheme.

Otherwise, you must redefine the view. Any explicit references to the old column name will now raise an error.

Ah, one more alternative:

EXEC sp_rename 'MyTable.MyColumnName', 'MyAlteredColumnName'
ALTER TABLE MyTable ADD MyColumnName AS MyAlteredColumnName
EXEC sp_rename 'MyView.MyColumnName', 'MyAlteredColumnName'

It's a hack, and it's dangerous, since the stored view definition will now be out of sync with the view metadata. And you have littered the db with superfluous computed columns.

But it will work (until you forget what you did, or someone else has to maintain the system, and things start to break mysteriously).

Peter
+1: @peter: ty for that clarification on sp_refreshview.
Rob