游标内的迭代循环未按预期工作

发布于 2024-12-02 23:17:54 字数 2073 浏览 1 评论 0原文

请帮忙。下面的查询未按预期工作。当 @count=1 时预计会停止插入,但它不会停止。我不知道问题出在哪里,请帮我解决。

create procedure mypro
as

declare @count int
declare aa cursor

for select SerialNumb from FORMTABLE

  open aa

      declare @SerialNumb varchar(11)
      fetch next from aa into @SerialNumb

      while(@@FETCH_STATUS<>-1)
       begin
          if(@@FETCH_STATUS <>-2)
          select @count= COUNT(SerialNumb) from CARDTABLE where SerialNumb=@SerialNumb
          while (@count<>1)
           begin

     ---- variables declaration

          declare @name
          declare @location
          declare @status


         select  @name = name, @location = location, @status=status

         from FORMTABLE  where SerialNumb = @SerialNumb


        insert into FORMTABLE values(@name, @location, @status)


        set @count = @count-1
      end
        fetch next from aa into @SerialNumb
          end

close aa

deallocate aa

请参阅下面的示例数据:

DECLARE @FormTable TABLE
(
Serialnumb INT,
[GROUP] CHAR(1),
CLASS VARCHAR(50) 
)

INSERT INTO @FormTable
SELECT 400,'A','Science' UNION ALL 
SELECT 401,'B','Social science' UNION ALL 
SELECT 402,'C','philosophy' UNION ALL 
SELECT 403,'D','Engineering' 

CardTable

 DECLARE @CardTable TABLE
(
Id INT PRIMARY KEY,
Serialnumb INT,
Name VARCHAR(50) 
)

INSERT INTO @CardTable
SELECT 1,400,'Charey' UNION ALL 
SELECT 2,400,'Mike' UNION ALL 
SELECT 3,400,'James' UNION ALL 
SELECT 4,401,'Vina' UNION ALL 
SELECT 5,400,'Gloria' UNION ALL 
SELECT 6,401,'Faith' UNION ALL 
SELECT 7,401,'Sarah' UNION ALL 
SELECT 8,402,'Joy' UNION ALL 
SELECT 9,402,'Rita' 

所需的结果应该是:

@FormTable

Serialno    Group       Class
400     A       Science
401     B       Social science
402     C       philosophy
403     D       Engineering
400     A       Science
400     A       Science
400     A       Science
401     B       Social science
401     B       Social science
402     C       philosophy

please help. The query below is not working as expected. It is expected to stop inserting when @count=1 but it doesn't stop. i can't figure out what the problem is , please help me out.

create procedure mypro
as

declare @count int
declare aa cursor

for select SerialNumb from FORMTABLE

  open aa

      declare @SerialNumb varchar(11)
      fetch next from aa into @SerialNumb

      while(@@FETCH_STATUS<>-1)
       begin
          if(@@FETCH_STATUS <>-2)
          select @count= COUNT(SerialNumb) from CARDTABLE where SerialNumb=@SerialNumb
          while (@count<>1)
           begin

     ---- variables declaration

          declare @name
          declare @location
          declare @status


         select  @name = name, @location = location, @status=status

         from FORMTABLE  where SerialNumb = @SerialNumb


        insert into FORMTABLE values(@name, @location, @status)


        set @count = @count-1
      end
        fetch next from aa into @SerialNumb
          end

close aa

deallocate aa

See sample data below:

DECLARE @FormTable TABLE
(
Serialnumb INT,
[GROUP] CHAR(1),
CLASS VARCHAR(50) 
)

INSERT INTO @FormTable
SELECT 400,'A','Science' UNION ALL 
SELECT 401,'B','Social science' UNION ALL 
SELECT 402,'C','philosophy' UNION ALL 
SELECT 403,'D','Engineering' 

CardTable

 DECLARE @CardTable TABLE
(
Id INT PRIMARY KEY,
Serialnumb INT,
Name VARCHAR(50) 
)

INSERT INTO @CardTable
SELECT 1,400,'Charey' UNION ALL 
SELECT 2,400,'Mike' UNION ALL 
SELECT 3,400,'James' UNION ALL 
SELECT 4,401,'Vina' UNION ALL 
SELECT 5,400,'Gloria' UNION ALL 
SELECT 6,401,'Faith' UNION ALL 
SELECT 7,401,'Sarah' UNION ALL 
SELECT 8,402,'Joy' UNION ALL 
SELECT 9,402,'Rita' 

The desired result should be:

@FormTable

Serialno    Group       Class
400     A       Science
401     B       Social science
402     C       philosophy
403     D       Engineering
400     A       Science
400     A       Science
400     A       Science
401     B       Social science
401     B       Social science
402     C       philosophy

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

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

发布评论

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

评论(1

心房敞 2024-12-09 23:17:54

我确实同意马丁的观点,看来你根本不需要光标。一个简单的连接就足够了。

为什么要再次在表单中存储数据?

I do agree with Martin, it seems you don't need cursor at all. A simple join would be suffice.

Why do you want to store data in your Form Table again?

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