我们老师留了一道用sqlserver游标语句打印报表,用什么语句啊?
答案:2 悬赏:20 手机版
解决时间 2021-01-03 17:37
- 提问者网友:疯子也有疯子的情调
- 2021-01-02 21:53
本人现在还没积分,各位大虾,帮忙想想吧,等有积分了,一定追加!谢谢了!!本人不胜感激!!
最佳答案
- 五星知识达人网友:等灯
- 2021-01-06 22:27
给你个例子 多看sql server 的帮助
DECLARE authors_cursor CURSOR FOR
SELECt au_id, au_fname, au_lname
FROM authors
WHERe state = "UT"
ORDER BY au_id
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT " "
SELECt @message = "----- Books by Author: " +
@au_fname + " " + @au_lname
PRINT @message
-- Declare an inner cursor based
-- on au_id from the outer cursor.
DECLARE titles_cursor CURSOR FOR
SELECt t.title
FROM titleauthor ta, titles t
WHERe ta.title_id = t.title_id AND
ta.au_id = @au_id -- Variable value from the outer cursor
OPEN titles_cursor
FETCH NEXT FROM titles_cursor INTO @title
IF @@FETCH_STATUS 0
PRINT " >"
WHILE @@FETCH_STATUS = 0
BEGIN
SELECt @message = " " + @title
PRINT @message
FETCH NEXT FROM titles_cursor INTO @title
END
CLOSE titles_cursor
DEALLOCATE titles_cursor
-- Get the next author.
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
DECLARE authors_cursor CURSOR FOR
SELECt au_id, au_fname, au_lname
FROM authors
WHERe state = "UT"
ORDER BY au_id
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT " "
SELECt @message = "----- Books by Author: " +
@au_fname + " " + @au_lname
PRINT @message
-- Declare an inner cursor based
-- on au_id from the outer cursor.
DECLARE titles_cursor CURSOR FOR
SELECt t.title
FROM titleauthor ta, titles t
WHERe ta.title_id = t.title_id AND
ta.au_id = @au_id -- Variable value from the outer cursor
OPEN titles_cursor
FETCH NEXT FROM titles_cursor INTO @title
IF @@FETCH_STATUS 0
PRINT " >"
WHILE @@FETCH_STATUS = 0
BEGIN
SELECt @message = " " + @title
PRINT @message
FETCH NEXT FROM titles_cursor INTO @title
END
CLOSE titles_cursor
DEALLOCATE titles_cursor
-- Get the next author.
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
全部回答
- 1楼网友:第四晚心情
- 2021-01-06 22:34
是可以嵌套的,原理如下:
declare 外层游标 open 外层游标 fetch next ...提取外层游标行 while @@fetch_status = 0 begin declare 内层游标 open 内层游标 ftech next ...提取内层游标行 while @@fetch_status = 0 begin .....处理内层游标 ftech next ....内层游标向下移动一行 end close 内层游标 deallocate 内层游标 fetch next ....内层游标处理结束后,外层游标才继续向下移动一行 end close 外层游标 deallocate 外层游标 也就是说,外层游标每移动一行,就要重复进行内层游标定义,打开,循环,关闭,释放等操作,然后才能再向下移动行
实例看我的博客: http://blog.csdn.net/gxiangzi/article/details/6774786
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯