问一个循环的存储过程怎么写,急,在线等各位大侠指教

ou630j 2009-08-19 08:26:51
1现在有如下四个表
reporttable(reptid为唯一)
字段 reptid reptname UIcode UIname title memo
AOO1 KK1 006 报表1 报表标题1
BOO1 KK2 006 报表1 报表标题2
AOO3 KK3 006 报表1 报表标题3
BOO4 KK4 006 报表1 报表标题4
reporttableDT
字段 reptid id file filename memo
A001 1 usercode 用户编号


A001 N USERNAME 用户名称
BOO1 1 usercode 用户编号


BOO1 N USERNAME 用户名称
A003 1 usercode 用户编号


A003 N USERNAME 用户名称
BOO4 1 usercode 用户编号


BOO4 N USERNAME 用户名称

现在我想把reporttable表里面的数据加起来放在另一个表reportresult(字段和reporttable的字段一样)
既是生成多种组合格式

AOO1 KK1 006 报表1 报表标题1
BOO1 KK2 006 报表1 报表标题2
AOO3 KK3 006 报表1 报表标题3
BOO4 KK4 006 报表1 报表标题4
AOO1-BOO1 KK1-KK2 006 报表1 报表标题1
BOO1-AOO1 KK2-kk1 006 报表1 报表标题2
AOO3-b004 KK3-kk4 006 报表1 报表标题3
BOO4-a003 KK4-kk3 006 报表1 报表标题4
既是表reporttable第一条记录和下一条记录生成三种格式(A001,A001-B001,B001-A001)
然后插入到表reportresult中
当然明细表的数据也要插入,那就是把 reptid字段改变为上面的,然后令两个数据相加
请问如何写,急,在线等各位大侠指教
...全文
69 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2009-08-19
  • 打赏
  • 举报
回复
DECLARE @a TABLE(reptid varCHAR(50),reptname varCHAR(50), UIcode varCHAR(50), UIname NVARCHAR(20),  title  NVARCHAR(20))
INSERT @a SELECT 'AOO1','KK1','006','报表1','报表标题1'
UNION ALL SELECT 'BOO1','KK2','006','报表1','报表标题2'
UNION ALL SELECT 'AOO3','KK3','006','报表1','报表标题3'
UNION ALL SELECT 'BOO4','KK4','006','报表1','报表标题4'

DECLARE @b TABLE(Id INT IDENTITY(0,1),reptid CHAR(4),reptname CHAR(3), UIcode CHAR(3), UIname NVARCHAR(20), title NVARCHAR(20))
INSERT @b SELECT * FROM @a

SELECT CASE WHEN aa.reptid=bb.reptid THEN aa.reptid ELSE aa.reptid+'-'+bb.reptid END reptid,
CASE WHEN aa.reptname=bb.reptname THEN aa.reptname ELSE aa.reptname+'-'+bb.reptname END reptname,
aa.UIcode,
aa.UIname,
aa.title
FROM @b aa ,@b bb WHERE aa.id /2=bb.id/2

--result
/*reptid reptname UIcode UIname title
--------- -------- ------ -------------------- --------------------
AOO1 KK1 006 报表1 报表标题1
AOO1-BOO1 KK1-KK2 006 报表1 报表标题1
BOO1-AOO1 KK2-KK1 006 报表1 报表标题2
BOO1 KK2 006 报表1 报表标题2
AOO3 KK3 006 报表1 报表标题3
AOO3-BOO4 KK3-KK4 006 报表1 报表标题3
BOO4-AOO3 KK4-KK3 006 报表1 报表标题4
BOO4 KK4 006 报表1 报表标题4

(所影响的行数为 8 行)
*/
SQL77 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
等楼下写
[/Quote]
观望楼下
  • 打赏
  • 举报
回复
---------------------------------
-- Author: HEROWANG(让你望见影子的墙)
-- Date : 2009-08-19 09:23:41
---------------------------------

IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
go
CREATE TABLE [tb] (reptid VARCHAR(10),reptname VARCHAR(10),UIcode VARCHAR(3),UIname VARCHAR(5),title VARCHAR(9))
INSERT INTO [tb]
SELECT 'AOO1','KK1','006','报表1','报表标题1' UNION ALL
SELECT 'BOO1','KK2','006','报表1','报表标题2' UNION ALL
SELECT 'AOO3','KK3','006','报表1','报表标题3' UNION ALL
SELECT 'BOO4','KK4','006','报表1','报表标题4'

select * from [tb]

;

with
wang as (select row=row_number() over (order by getdate()),* from tb)
insert into tb
select s.reptid+'-'+t.reptid,s.reptname+'-'+t.reptname,s.uicode,s.uiname,s.title from wang s join wang t on s.row=t.row-1
where s.row%2=1
union all
select s.reptid+'-'+t.reptid,s.reptname+'-'+t.reptname,s.uicode,s.uiname,s.title from wang s join wang t on s.row=t.row+1
where s.row%2=0

select * from tb

reptid reptname UIcode UIname title
AOO1 KK1 006 报表1 报表标题1
BOO1 KK2 006 报表1 报表标题2
AOO3 KK3 006 报表1 报表标题3
BOO4 KK4 006 报表1 报表标题4
AOO1-BOO1 KK1-KK2 006 报表1 报表标题1
AOO3-BOO4 KK3-KK4 006 报表1 报表标题3
BOO1-AOO1 KK2-KK1 006 报表1 报表标题2
BOO4-AOO3 KK4-KK3 006 报表1 报表标题4
华夏小卒 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
等楼下写
[/Quote].
--小F-- 2009-08-19
  • 打赏
  • 举报
回复
等楼下写
chuifengde 2009-08-19
  • 打赏
  • 举报
回复
明细表变化后得到什么结果?

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧