July 13, 2005 at 2:59 am
There is a possibility to drop a table which is being refered by a view. i think its logically wrong.
Is there any other suggestions
July 13, 2005 at 3:52 am
Create the view WITH SCHEMABINDING
That way the tables (or other views) referenced by the view cannot be modified in any way unless the view is dropped first.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 13, 2005 at 11:15 am
It is possible, and does not warn.
use pubs
Select *
into Authors2
from authors
Create view AuthorBooks
as
select au_lname, au_fname, b.title_id, title, type
from authors2 a
join titleAuthor b on a.au_id = b.au_id
join titles c on c.title_id = b.title_id
select top 10 *
from AuthorBooks
Drop Table authors2
select top 10 *
from AuthorBooks
-- Results
-- Server: Msg 208, Level 16, State 1, Procedure AuthorBooks, Line 3
-- Invalid object name 'authors2'.
-- Server: Msg 4413, Level 16, State 1, Line 1
-- Could not use view or function 'AuthorBooks' because of binding errors.
Drop view AuthorBooks
As mentioned create view with schemabinding to prevent this.
July 13, 2005 at 11:52 am
>> Create the view WITH SCHEMABINDING
That way the tables (or other views) referenced by the view cannot be modified in any way unless the view is dropped first. <<
That is PARTIALLY correct. You CAN modify the table as long as you don't touch the columns included in the view. The other columns are changeable...
been there done that
* Noel
July 13, 2005 at 6:33 pm
Ray is right. Table can be dropped. The view that referenced this table still exists but unviewable.
July 13, 2005 at 11:49 pm
> You CAN modify the table as long as you don't touch the columns included
> in the view. The other columns are changeable...
Ah. *reads fine print in BoL* So noted.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 14, 2005 at 12:10 am
Gila, you're totally correct! but the table still can be dropped before the view
July 14, 2005 at 12:26 am
???
Create Table Testing (
Test int
)
GO
Create View vwTest WITH SCHEMABINDING AS
Select Test FROM dbo.Testing
GO
DROP TABLE Testing
GO
The drop table returns
Server: Msg 3729, Level 16, State 1, Line 1
Cannot DROP TABLE 'Testing' because it is being referenced by object 'vwTest'.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 14, 2005 at 5:36 pm
Gila, you got me there!!!
Setting schemabinding will not allow you to drop the table first. Thanks for showing me that!!
I learn new things very day
But if you "REALLY" want to drop your table first (which you really don't want to do) there is still a way.
ALTER View vwTest AS
Select Test FROM dbo.Testing
GO
drop table dbo.Testing
GO
July 14, 2005 at 11:52 pm
Sure. By altering the view like that you've removed the schemabinding, hence you can now do anything with the base table
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply