首页 / 数据库 / SQLServer / SQL Server 2000的视图中必须小心使用*符号
SQL Server 2000的视图中必须小心使用*符号2010-06-16林梦有些朋友看到这个标题可能会有疑问,难道在视图中使用*符号还有何要注意的地方吗?对于这个问题,我们先不必回答,先看一下例子吧。我这里,使用的是SqlServer2000自带的Northwind,这样方便大家自己私下里测试。首先,创建两个视图,视图的脚本如下:--视图 vCustomersAcreate view vCustomersA
as
select CustomerID ,CompanyName,ContactName,ContactTitle,
Address,City,Region,PostalCode,Country,Phone,Fax
from dbo.Customers
go
--视图 vCustomersBcreate view vCustomersB
as
select * from vCustomersA
go
然后,使用这两个视图查询客户ID为ALFKI的资料,查询语句如下:select * from vCustomersA where CustomerID = "ALFKI"select * from vCustomersB where CustomerID = "ALFKI"查询的结果如下:一切正常,这个时候,需求发生了变化,我们需要改动vCustomersA,改动后的脚本如下:(为了说明问题,我们只是把CompanyName和ContactName互换一下位置)--改动后的视图vCustomersAalter view vCustomersA
as
select CustomerID ,ContactName,CompanyName,ContactTitle,
Address,City,Region,PostalCode,Country,Phone,Fax
from dbo.Customers
go
这个时候,当我们再次使用视图vCustomersB查询客户ID为ALFKI的资料的时候,错误已经悄然来临,你注意到了吗?让我们来看一下这两个视图的查询结果吧,查询语句如下:select * from vCustomersA where CustomerID = "ALFKI"select * from vCustomersB where CustomerID = "ALFKI"