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.
Author |
Topic |
bogey
Posting Yak Master
166 Posts |
Posted - 2008-06-25 : 08:32:12
|
A colleague of mine ran into a problem today. He had a view on a Table called employee that was this "select * from employee". He then altered the table and inserted a column in the 2nd position with default values. Now when he runs the view its not showing the new column but it is showing the data in the orginal 2nd column. I'm assuming that its not best practice to insert columns into existing tables but they should be added to the end of the table. Anyone know why this works like it does?thanks. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 08:36:52
|
Its not a good practise to use select * in views. If you use it you need to ensure whenever the table structure changes, you drop and recreate view to reflect the new structure. |
|
|
bogey
Posting Yak Master
166 Posts |
Posted - 2008-06-25 : 08:41:07
|
quote: Originally posted by visakh16 Its not a good practise to use select * in views. If you use it you need to ensure whenever the table structure changes, you drop and recreate view to reflect the new structure.
Should not an alter force recompilation of all objects that depend on the altered object? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 08:56:26
|
quote: Originally posted by bogey
quote: Originally posted by visakh16 Its not a good practise to use select * in views. If you use it you need to ensure whenever the table structure changes, you drop and recreate view to reflect the new structure.
Should not an alter force recompilation of all objects that depend on the altered object?
Alter will only alter the structure of table alone not dependent views. You need to recreate the view once tables have changed. thats what bol also suggests:-If a view depends on a table (or view) that has been dropped, SQL Server produces an error message if anyone tries to use the view. If a new table (or view) is created, even if the table structure does not change from the previous base table, to replace the one dropped, the view again becomes usable. If the new table (or view) structure changes, then the view must be dropped and re-created. |
|
|
mdubey
Posting Yak Master
133 Posts |
Posted - 2008-06-25 : 09:40:37
|
View is dependant on the Table(Source). Whenever any changes happen in the Table(Source) you need to recreate a view if required.(If you wanted to modify view). It will not take automatically effect on the view.ManojMCP, MCTS |
|
|
rmiao
Master Smack Fu Yak Hacker
7266 Posts |
Posted - 2008-06-28 : 22:14:56
|
You can refresh view with sp_refreshview. |
|
|
maninder
Posting Yak Master
100 Posts |
Posted - 2008-06-29 : 18:54:30
|
Inserting COLUMNS Does not change Their ORDINAL Positions, they will be added as the last Column(s), Unless, you goto DESIGN View and INSERT a Column at a relevant position, this will show you the column at that Position.This will however not refresh the view. To Refresh teh View, you have to alter the View to Refresh the list.ALTER VIEW ViewNameASselect * from employeeManinder |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-06-29 : 21:41:32
|
quote: Originally posted by rmiao You can refresh view with sp_refreshview.
Rmaio is correct too. |
|
|
|
|
|
|
|