zoukankan      html  css  js  c++  java
  • 【SQLSerrver】游标的使用(游标循环遍历一个表中的数据)

    drop table aa
    go
    create table aa
    (
    [name] nvarchar(20),
    [language] nvarchar(20)
    )
    go

    insert into aa([name],[language]) values ('张学友','zh');
    insert into aa([name],[language]) values ('张学友','en');
    insert into aa([name],[language]) values ('刘德华','zh');
    insert into aa([name],[language]) values ('黎明','jp');
    insert into aa([name],[language]) values ('刘德华','jp');
    go



    declare curaa cursor
    for select [name],[language] from aa order by name,[language]
    begin
    declare @name nvarchar(20);
    declare @language nvarchar(20);
    declare @num decimal(18);
    create table #aa([name] nvarchar(20),[language] nvarchar(20),cc decimal(18));
    OPEN curaa;
    fetch next from curaa into @name,@language;
    if ((@name is not null) and (@language is not null))
    insert into #aa(cc,[Name],[language])values(1,@name,@language);
    WHILE (@@FETCH_STATUS <> -1)
    begin
    set @name = null;
    set @language = null;
    fetch next from curaa into @name,@language;
    if ((@name is not null) and (@language is not null))
    begin
    select @num=Count(Name)+1 from #aa where Name=@name
    insert into #aa(cc,[Name],[language])values(@num,@name,@language);
    end
    end
    select * from #aa order by Name,[language];
    drop table #aa;
    CLOSE curaa;
    DEALLOCATE curaa;
    end
  • 相关阅读:
    为什么转速环经过pi调节后输出的是电流?
    如何将mysql、php、nginx设置为开机自启动
    rm-rf /*
    2020/4/23-linux中的定时任务详解
    2020/4/20 一键部署服务
    2020/4/20-ansible一键部署nfs
    2020/4/20-backup之rsync
    2020/4/19-linux中/etc/hosts
    2020/4/18-linux中的selinux
    2020/4/18-linux中的iptables
  • 原文地址:https://www.cnblogs.com/aptdo2008/p/2242347.html
Copyright © 2011-2022 走看看