当前位置: 代码迷 >> Sql Server >> Union All,该如何解决
  详细解决方案

Union All,该如何解决

热度:549   发布时间:2016-04-27 12:41:23.0
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 #tbfrom(    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 ASSELECT * 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 BORDER BY RANK