日期:2014-05-17 浏览次数:20652 次
-->数据库版本: -->Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 --> 测试数据:t1 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N't1') AND type in (N'U')) DROP TABLE t1 GO ---->建表 create table t1([a] int,[b] varchar(2),[c] varchar(2),[d] varchar(1)) insert t1 select 1,'b','c','d' union all select 2,'b','c1','d' union all select 3,'b1','c','d' union all select 4,'b2','c','d' GO -->数据库版本: -->Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 --> 测试数据:t2 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N't2') AND type in (N'U')) DROP TABLE t2 GO ---->建表 create table t2([a] int,[e] varchar(2)) insert t2 select 1,'b' union all select 2,'b' union all select 3,'b1' union all select 4,'b2' GO --> 查询结果 SELECT * FROM t1 --> 删除表格 --DROP TABLE t1 --> 查询结果 SELECT * FROM t2 --> 删除表格 --DROP TABLE t2 create view v_view as SELECT ROW_NUMBER() over (order by getdate()) as id ,t1.a,t1.b,t1.c,t1.d,t2.e FROM t1 join t2 on t1.a=t2.a go select * from v_view drop view v_view
------解决方案--------------------
----- if OBJECT_ID('Temp') is not null truncate table Temp drop table Temp go -- SELECT IDENTITY(int,1,1) as id ,t1.a,t1.b,t1.c,t1.d,t2.e into Temp FROM t1 join t2 on t1.a=t2.a go -- create view v_view as select * from Temp go -- select * from v_view
------解决方案--------------------
3楼答案是对的。