SQL server 将游标查询结果显示在一个表中,现在是每一条显示一个表头

SQL server 将游标查询结果显示在一个表中,现在是每一条显示一个表头,语句该如何修改呢?

图片说明

declare @addr varchar(128)
declare myCursor CURSOR FOR select distinct cCardCode from dbo.ZLKCardFlow
open myCursor

fetch next from myCursor into @addr
while(@@fetch_status=0)
begin

 select distinct  c.cCardCode,c.cName, c.cstatus,  a0 as '充值金额',b1 as '消费金额',(a.a0-b.b1) as '余额' from 

(select SUM(nMoney) a0 from dbo.ZLKCardFlow where cCardCode=@addr and cOPType='充值')a,

(select SUM(nMoney) b1 from dbo.ZLKCardFlow where cCardCode=@addr and cOPType='消费')b,
(select top 1 * from dbo.ZLKCardFlow where cCardCode=@addr order by cstatus desc )c

  fetch next from myCursor into @addr 

end
CLOSE myCursor

DEALLOCATE myCursor

sql
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问