sql : SELECT *,1 AS Config,Row_Number() over ( order by getdate() ) as ID FROM mtlevel1
UNION ALL
SELECT *,2 AS Config,Row_Number() over ( order by getdate() ) as ID FROM mtlevel2
这样写好像不行
结果:
ID name
1 xxx
2 xxxx
3 x xxxx
1 dsad
2 ffff
3 klkl
完全分开了, 不是我想要的结果。
我想要的结果:
1 xxx
2 xxxx
3 x xxxx
4 dsad
5 ffff
6 klkl
有什么方法吗?求帮助。 这个合并的两个视图。
楼主是想用Config来标记究竟是哪个表?按照楼主思路:
WITH tbl AS (
SELECT 1 AS Config,* FROM mtlevel1
UNION ALL
SELECT 2 AS Config,* FROM mtlevel2
)
SELECT ROW_NUMBER() OVER(ORDER BY GETDATE()),* FROM tbl
对的。谢谢你的回答
USE YTGS;
GO
IF OBJECT_ID ( 'YTGS.YTGS.getSequence', 'P' ) IS NOT NULL
DROP PROCEDURE YTGS.getSequence;
GO
CREATE PROCEDURE YTGS.getSequence(@maxCount int)
AS
declare @maxValue int;
SELECT @maxValue=MAX(sequence)
FROM YTGS.YTGS.sequencetable;
if @maxValue is null
set @maxValue=0;
else if @maxValue>@maxCount
begin
delete YTGS.YTGS.sequencetable;
set @maxValue=0;
end;
set @maxValue=@maxValue+1;
insert into YTGS.YTGS.sequencetable (sequence) values(@maxValue)
GO
declare @v int;
exec YTGS.getSequence 1000;
SELECT MAX(sequence)
FROM YTGS.YTGS.sequencetable;
实现后跟ORACLE里面的序列的概念是差不多的。
参考:http://download.csdn.net/detail/mzg29/4544724#comment
如果只是视图中区别两个表,ID前不同表加不同前缀就行,比如表1 s+ID 表2 d+ID
物理存储上要ID是一个自增序列的话,用种子表吧