zoukankan      html  css  js  c++  java
  • 数据库 表数据 每一条数据排序 放到另一张表中--存储过程

    要查的数据----数据表名xuan5kjjgid    

    创建另一个表  ---xuan3to5  (列名除了four five 两列  其他列名基本一致) -----把表xuan5kjjgid   one  two three 三列从小到大排序   放到xuan3to5表中 one two three

    存储过程

    --如果有这个表 删除
      if(object_id('xuan3to5','U')is not null)
      drop table xuan3to5
      go
      --创建一张表  
      create table xuan3to5
      (
      id int not null,
      code nvarchar(200),
      one int,
      two int,
      three int,
      dates nvarchar(200),
      qiansanshangci nvarchar(200),
      qiansanjiange int,
      )
      
      --如果有proc_table_to_table 存储过程 删除
      
      if(OBJECT_ID('proc_table_to_table','P')is not null)
      drop proc proc_table_to_table
      go
     -- 创建proc_table_to_table存储过程
      create proc proc_table_to_table
        --不需要输出参数  所以不需要写参数
      as  
      --过程要用到的参数
      declare @table1_count int,
      @table_id int,
      @table_code nvarchar(200),
      @table_one int,
      @table_two int,
      @table_three int,
      @table_dates nvarchar(200),
      @table_qiansanshangci nvarchar(200),
      @table_qiansanjiange int
    
       ---取出条数
      select @table1_count=COUNT(*) from xuan5kjjgid
      --准备循环
      declare @i int;
      set @i=0;
         --循环每一条数据
        while @i<@table1_count
        begin
        --查出每一条数据
        select  @table_id=id,@table_code=code,@table_dates=dates,@table_qiansanshangci=qiansanshangci,@table_qiansanjiange=qiansanjiange from xuan5kjjgid  where id =188831+@i   
     ---创建变量表  因为表只能初始化一次 一直到循环完成才能变量表删除 下面中只能删除数据
       declare @tb table(num int)
       --添加变量表 @tb 3条数据 (one(一条) two(第二条) three(第三条)数据)
       insert into @tb 
       select one from xuan5kjjgid  where id =188831+@i 
       union 
       select two from xuan5kjjgid  where id =188831+@i 
       union 
       select three from xuan5kjjgid  where id =188831+@i 
       --创建另一个变量表  并把@tb  表排序  添加进去
       declare @te table(id int identity(1,1),num int)
      insert into @te select num from @tb order by num
      ---取出排序后的数据  放到变量 @table_one @table_two @table_three
      select @table_one=(select top 1 num from @te )
       select @table_two=(select top 1 num from @te where id not in(select top 1 id from @te))
        select @table_three=(select top 1 num from @te where id not in(select top 2 id from @te))
        
        --把数据添加到最终的表中   
        insert into xuan3to5 values(@table_id,@table_code,
        @table_one,@table_two,@table_three,
        @table_dates,@table_qiansanshangci,@table_qiansanjiange)
        set @i=@i+1;
        --因为变量表  只能初始化一次  只能删除数据
      delete @tb
      delete @te
      --循环结束
        end
    --存储过程结束
    
    
    --执行存储过程
    exec proc_table_to_table

                                       

  • 相关阅读:
    webpack里CommonJS的require与ES6 的module.exports加载模块有何不同
    前端项目使用module.exports文件一定要Webpack编译吗?请问gulp可以编译这种文件吗
    Webpack之“多页面开发”最佳实战
    webpack 单页面应用实战
    也谈谈同源策略和跨域问题
    (转)Babel-现在开始使用 ES6
    webpack 配置简单说几句 ?
    Javascript 严格模式详解
    JavaScript6 新语法 let 有什么优势
    js中let和var定义变量的区别
  • 原文地址:https://www.cnblogs.com/zhangwei99com/p/7349069.html
Copyright © 2011-2022 走看看