日期:2014-05-17 浏览次数:20624 次
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [ID] INT, [标识] varchar(100), [数量] INT, [单号] varchar(100));
insert #temp
select '1','A','1000','CN1' union all
select '2','B','200','CN2' union all
select '3','B','100','CN1' union all
select '4','C','200','CN3'
CREATE TABLE #tempB(id INT, 序列号 VARCHAR(100), 单号 VARCHAR(100))
--SQL:
;WITH
cte1 AS(SELECT num = 1 UNION ALL SELECT 1),
cte2 AS(SELECT num = 1 FROM cte1 a, cte1 b),
cte3 AS(SELECT num = 1 FROM cte2 a, cte2 b),
cte4 AS(SELECT num = 1 FROM cte3 a, cte3 b),
cte5 AS(SELECT num = 1 FROM cte4 a, cte4 b),
cteNum as(SELECT num = ROW_NUMBER() OVER(ORDER BY GETDATE()) FROM cte5),