日期:2014-05-18  浏览次数:20511 次

Union All
Union All
create View
as
select col from t1 union all select col from t2

怎么加一个自增列呢?
请注意sql2000 这里是。

------解决方案--------------------
CREATE VIEW 没有加自增列的。
SELECT ID=IDENTITY(INT,1,1),. INTO #T


------解决方案--------------------
union后的东东放入临时表用吧!

SQL code

select *,id = identity(int,1,1) into #tb
from(
    select .. from tb1
    union all
    select .. from tb2
)t

------解决方案--------------------
CREATE VIEW 
AS
SELECT * FROM
(SELECT col ,
(SELECT COUNT(*) FROM 
(SELECT col FROM t1 UNION ALL SELECT col FROM t2) AS A2 
WHERE A2.col<A1.col) AS RANK
FROM (SELECT col FROM t1 UNION ALL SELECT col FROM t2) AS A1) AS B
ORDER BY RANK
------解决方案--------------------
在2000环境下验证通过.
SQL code
CREATE VIEW 
AS
SELECT * FROM
    (SELECT    col ,
            (SELECT COUNT(*) FROM 
                (SELECT col FROM t1 UNION ALL SELECT col FROM t2) AS A2 
                WHERE A2.col<A1.col) AS RANK
     FROM (SELECT col FROM t1 UNION ALL SELECT col FROM t2) AS A1) AS B
ORDER BY RANK