;with cte 当第二次循环的时候怎样清楚Cte 之前的数据
begin
declare @JiaGeLaiYuan table( CaiLiaoJieGe varchar(1000),Orders2ID varchar(50))
--声明一个用来循环的表 @whileTable 并插入数据 用Orders2ID循环
declare @whileTable table( whiletableOrders2 varchar(50))
insert into @whileTable(whiletableOrders2)
select ID from Orders2 O2 where O2.OrdersID='20100530101615703644FCD9C5CCDC' and O2.DeleteTag='0'
--声明一个变量来存放循环的Orders2ID
declare @whileOrders2ID varchar(50);
--声明一个临时表存放所用价格来源数据
--declare @t1 table (CaiLiao varchar(100),JiaGe decimal(18,2),Orders2ID varchar(50),ProductNo varchar(50))
--声明一个游标来循环
declare whileCursor cursor for select whiletableOrders2 from @whileTable;
open whileCursor
fetch next from whileCursor into @whileOrders2ID
while @@fetch_status=0
begin
declare @t1 table (CaiLiao varchar(100),JiaGe decimal(18,2),Orders2ID varchar(50),ProductNo varchar(50))
declare @intt int
delete @t1
insert into @t1(CaiLiao,JiaGe,Orders2ID,ProductNo)
select (case P.OtherID when '1' then '钢芯' when '2' then '穿轴'
when '3' then '轴承' when '4' then '胶料' when '5' then '附件' end ) CaiLiao,
MarketPrice,Orders2ID,ProductNo from Orders2MarketPrice P
where Orders2ID=@whileOrders2ID order by Orders2ID,OtherID
--select * from @t1
set @intt=(select count(1) from @t1 );
--select @intt
declare @whileint int
set @whileint=0;
declare @t table ( k int identity, a varchar(500),Orders2ID varchar(50) )
insert into @t(a,Orders2ID)
select (CaiLiao+' :'+Cast(JiaGe as varchar(50))) CaiLiaoJiaGe,Orders2ID from @t1;
while 1=1
begin
;with cte(a,k,Orders2ID)
as
( select cast(t.a as varchar(max)), t.k ,t.Orders2ID from @t t where k = 1
union all
select c.a + cast(t.a as varchar(max)), t.k,t.Orders2ID from @t t
join cte c on t.k = c.k+1 )
select * from Cte
-- insert into @JiaGeLaiYuan(CaiLiaoJieGe,Orders2ID)
-- select top 1 a,Orders2ID from cte order by k desc
break
-- set @whileint=(@whileint+1)
-- print @whileint
-- if @whileint>@intt
-- begin
-- break;
-- end
end
fetch next from whileCursor into @whileOrders2ID
end
close whileCursor;
deallocate whileCursor;
select * from @JiaGeLaiYuan
end
如题
declare @JiaGeLaiYuan table( CaiLiaoJieGe varchar(1000),Orders2ID varchar(50))
--声明一个用来循环的表 @whileTable 并插入数据 用Orders2ID循环
declare @whileTable table( whiletableOrders2 varchar(50))
insert into @whileTable(whiletableOrders2)
select ID from Orders2 O2 where O2.OrdersID='20100530101615703644FCD9C5CCDC' and O2.DeleteTag='0'
--声明一个变量来存放循环的Orders2ID
declare @whileOrders2ID varchar(50);
--声明一个临时表存放所用价格来源数据
--declare @t1 table (CaiLiao varchar(100),JiaGe decimal(18,2),Orders2ID varchar(50),ProductNo varchar(50))
--声明一个游标来循环
declare whileCursor cursor for select whiletableOrders2 from @whileTable;
open whileCursor
fetch next from whileCursor into @whileOrders2ID
while @@fetch_status=0
begin
declare @t1 table (CaiLiao varchar(100),JiaGe decimal(18,2),Orders2ID varchar(50),ProductNo varchar(50))
declare @intt int
delete @t1
insert into @t1(CaiLiao,JiaGe,Orders2ID,ProductNo)
select (case P.OtherID when '1' then '钢芯' when '2' then '穿轴'
when '3' then '轴承' when '4' then '胶料' when '5' then '附件' end ) CaiLiao,
MarketPrice,Orders2ID,ProductNo from Orders2MarketPrice P
where Orders2ID=@whileOrders2ID order by Orders2ID,OtherID
--select * from @t1
set @intt=(select count(1) from @t1 );
--select @intt
declare @whileint int
set @whileint=0;
declare @t table ( k int identity, a varchar(500),Orders2ID varchar(50) )
insert into @t(a,Orders2ID)
select (CaiLiao+' :'+Cast(JiaGe as varchar(50))) CaiLiaoJiaGe,Orders2ID from @t1;
while 1=1
begin
;with cte(a,k,Orders2ID)
as
( select cast(t.a as varchar(max)), t.k ,t.Orders2ID from @t t where k = 1
union all
select c.a + cast(t.a as varchar(max)), t.k,t.Orders2ID from @t t
join cte c on t.k = c.k+1 )
select * from Cte
-- insert into @JiaGeLaiYuan(CaiLiaoJieGe,Orders2ID)
-- select top 1 a,Orders2ID from cte order by k desc
break
-- set @whileint=(@whileint+1)
-- print @whileint
-- if @whileint>@intt
-- begin
-- break;
-- end
end
fetch next from whileCursor into @whileOrders2ID
end
close whileCursor;
deallocate whileCursor;
select * from @JiaGeLaiYuan
end
如题
...全文
相关推荐
SQLserver CTE With
SQLserver CTE With 使用with产出临时表,在根据临时表二次查询
Open SQL 的 WITH:用 CTE 把复杂查询写成一条可维护的 ABAP SQL
文章摘要: SAP ABAP中的WITH关键字为复杂查询提供了可读性解决方案,通过CTE(通用表表达式)将多层嵌套查询拆分为可命名、可复用的阶段性结果集。文章详细解析了WITH语法结构、作用域规则和性能特点,重点解决了三大痛点:复用统计逻辑、避免深层嵌套和减少数据搬运。同时指出CTE会绕过表缓冲直接访问数据库,并触发7.51严格模式检查。通过实际案例展示了CTE在销售订单统计和城市地理信息查询中的应用价值,为S/4HANA项目中的复杂数据处理提供了优化思路。
sql写法_CTE
**数据聚合**:CTE可以用来聚合数据,例如计算总和、平均值等,然后在主查询中使用这些聚合结果。3. **递归查询**:这是CTE最强大的用途之一。
sql里面 with...as 是什么意思?如何使用?
熟练掌握并运用CTE可以帮助我们更好地管理和操作数据库中的数据。
SQL CTE详解[代码]
因此,在选择使用CTE之前,需要了解所使用的数据库是否支持CTE以及支持的具体程度。


