从存储过程返回游标

发布于 2024-10-10 20:34:31 字数 412 浏览 1 评论 0原文

您好,我需要从 STORED PROCEDURE 返回一个 CURSOR 我是这样处理的

create proc pps @return_cursor cursor VARYING OUTPUT As
DECLARE cursor_name CURSOR FOR
SELECT id FROM table_name

现在我的问题是捕获返回的 Cursor

DECLARE cur_ret  CURSOR 

cur_ret = 执行pps

但执行方法给出错误

我的问题是如何从存储过程返回游标并捕获它

Hi i need to return a CURSOR from a STORED PROCEDURE i approached like this

create proc pps @return_cursor cursor VARYING OUTPUT As
DECLARE cursor_name CURSOR FOR
SELECT id FROM table_name

Now my problem is to capture the return Cursor

DECLARE cur_ret  CURSOR 

cur_ret = exec pps

but execute approach give an error

My problem is How to Return a CURSOR from a Stored procedure and capture it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-10-17 20:34:31

我发现可以这样做

创建 proc pps @ret_cur 游标
改变输出作为 DECLARE @CURx CURSOR
SET @CURx= 光标用于选择 id FROM
用户1

声明@CrsrVar游标;

执行 PPS @ret_cur=@CrsrVar 输出

I have figured out it can be do like this

create proc pps @ret_cur cursor
VARYING OUTPUT As DECLARE @CURx CURSOR
SET @CURx= CURSOR FOR SELECT id FROM
users1

DECLARE @CrsrVar CURSOR;

EXEC PPS @ret_cur=@CrsrVar OUTPUT

多彩岁月 2024-10-17 20:34:31

请参阅链接中的“SQL Server 游标作为存储过程的输出”部分 https://www.mssqltips.com/sqlservertip/6308/ Different-ways-to-write-a-cursor-in-sql-server/

USE tempdb
GO

-- Cursor as an output of a stored procedure
CREATE PROCEDURE dbo.usp_cursor_db
@cursor_db CURSOR VARYING OUTPUT
AS
BEGIN
   DECLARE
      @database_id INT, 
      @database_name   VARCHAR(255);

   SET @cursor_db = CURSOR
   FOR SELECT 
         database_id, name
      FROM sys.master_files;

   OPEN @cursor_db;

END
GO

--从存储过程检索游标结果集输出的代码

-- Code to retrieve the cursor resultset output from the stored procedure
DECLARE 
    @cursor_db CURSOR

DECLARE
    @database_id INT, 
    @database_name   VARCHAR(255);

EXEC dbo.usp_cursor_db @cursor_db = @cursor_db OUTPUT

FETCH NEXT FROM @cursor_db INTO 
   @database_id, @database_name;

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT @database_name + ' id:' + CAST(@database_id AS VARCHAR(10));

   FETCH NEXT FROM @cursor_db INTO 
      @database_id, 
      @database_name;
END;

CLOSE @cursor_db;

DEALLOCATE @cursor_db;
GO

Refer the section "SQL Server Cursor as Output of a Stored Procedure" in the link https://www.mssqltips.com/sqlservertip/6308/different-ways-to-write-a-cursor-in-sql-server/

USE tempdb
GO

-- Cursor as an output of a stored procedure
CREATE PROCEDURE dbo.usp_cursor_db
@cursor_db CURSOR VARYING OUTPUT
AS
BEGIN
   DECLARE
      @database_id INT, 
      @database_name   VARCHAR(255);

   SET @cursor_db = CURSOR
   FOR SELECT 
         database_id, name
      FROM sys.master_files;

   OPEN @cursor_db;

END
GO

-- Code to retrieve the cursor resultset output from the stored procedure

-- Code to retrieve the cursor resultset output from the stored procedure
DECLARE 
    @cursor_db CURSOR

DECLARE
    @database_id INT, 
    @database_name   VARCHAR(255);

EXEC dbo.usp_cursor_db @cursor_db = @cursor_db OUTPUT

FETCH NEXT FROM @cursor_db INTO 
   @database_id, @database_name;

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT @database_name + ' id:' + CAST(@database_id AS VARCHAR(10));

   FETCH NEXT FROM @cursor_db INTO 
      @database_id, 
      @database_name;
END;

CLOSE @cursor_db;

DEALLOCATE @cursor_db;
GO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文