• 统计testa表中的各个下级分类的数量(利用游标)


    创建表:

    USE [testa]
    GO

    /****** Object:  Table [dbo].[AjaxCity]    Script Date: 06/26/2011 14:05:36 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[AjaxCity](
     [id] [int] NOT NULL,
     [cityName] [nvarchar](50) NOT NULL,
     [short] [nvarchar](10) NOT NULL
    ) ON [PRIMARY]

    GO

    USE [testa]
    GO

    /****** Object:  Table [dbo].[AjaxCounty]    Script Date: 06/26/2011 14:06:23 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[AjaxCounty](
     [id] [int] NOT NULL,
     [countryName] [nvarchar](50) NOT NULL,
     [cityID] [int] NOT NULL
    ) ON [PRIMARY]

    GO


     

    CREATE PROCEDURE mysp_Cursor
        @Result varchar(255) output/*声明输出变量*/
    AS
        declare city_cursor cursor for/*声明游标变量*/
        select [ID] from AjaxCity

    set @Result=''
    declare @Field int/*声明临时存放CityID的变量*/
    open city_cursor /*打开游标*/
    fetch next from city_cursor into @Field/*将实际ID赋给变量*/
    while(@@fetch_status=0)/*循环开始*/
    begin
           if @Result = ''
               select @Result = convert(nvarchar(2),count(*))  from AjaxCounty where CityID=@Field
           else
               select @Result = @Result + ',' + convert(nvarchar(2),count(*)) from AjaxCounty where CityID=@Field
          
           fetch next from city_cursor into @Field/*下一个CityID*/
           print @Result
    end
    close city_cursor/*关闭游标*/
    deallocate city_cursor/*释放游标引用*/
    GO


    declare @Result varchar(255)
    exec mysp_Cursor @Result
    print @Result

  • 相关阅读:
    HDU 5119 Happy Matt Friends(递推)
    爬虫系列之requests
    基于SVM的python简单实现验证码识别
    python3.6配置libsvm2.2
    KNN——图像分类
    python写web服务器
    牛客练习赛9 珂朵莉的值域连续段
    【转】3次重传的机制
    【转】三个重复的ACK意味着发生拥塞?
    【转】TCP报文格式定义详解
  • 原文地址:https://www.cnblogs.com/andylaufzf/p/2090634.html
Copyright © 2020-2023  润新知