看看MS SQL2000的BUG
首先,请先建两个表,脚本如下:
CREATE TABLE [dbo].[table1] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[description] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[type] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[table2] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[table1ID] [int] NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[table1] WITH NOCHECK ADD
CONSTRAINT [PK_table1_1] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[table2] WITH NOCHECK ADD
CONSTRAINT [PK_table2_1] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[table1] WITH NOCHECK ADD
CONSTRAINT [DF_table1_type] DEFAULT (0) FOR [type]
GO
然后,输入数据:
这是table1的数据:
这是table2的数据:
然后,做一个视图,脚本如下:
CREATE VIEW dbo.view1
AS
SELECT a.*, b.name AS table2name
FROM dbo.table1 a INNER JOIN
dbo.table2 b ON a.id = b.table1ID
好了,你现在在表table1中新加一个字段 type,脚本如下:
ALTER TABLE dbo.table1 ADD
type int NOT NULL CONSTRAINT DF_table1_type DEFAULT 0
GO
OK,请你分别执行两个查询语句:
SELECT a.*, b.name AS table2name
FROM dbo.table1 a INNER JOIN
dbo.table2 b ON a.id = b.table1ID
(注意:这条语句就是生成视图view1的语句)
这是执行结果:
SELECT * FROM view1
这是执行结果:
你看到什么不同了吗?
正确的写法是,在视图中避免使用*,应将字段完整列出。