1.分批更新数据库

declare @x int
set @x=1
while(@x<=51)
begin
begin tran
update UserFavorite set UserFavorite.firstpublishtime = product.lastpublishtime
from UserFavorite,product where UserFavorite.productid = product.id
and UserFavorite.id between (@x-1)* 10000 and @x*10000
commit tran
set @x=@x+1
WAITFOR DELAY '00:00:30'; --等待5秒
end

2.批量更新收藏表

USE [TianYi]
GO
/****** Object:  StoredProcedure [dbo].[UpdateUserFavoriteByProductid]    Script Date: 01/23/2015 13:28:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
  ALTER proc [dbo].[UpdateUserFavoriteByProductid]
  as
  begin
   create table #temp2
   (
     id int identity(1,1),
     productid int,
     LastPublishTime datetime
   )
  
   declare @ufpage int --@ufnum/10000
   declare @ufn int --收藏表循环次数
    --取出所有要更新的系列下的所有产品
     insert into #temp2 select ID,LastPublishTime from Product where serieID in(select distinct serieID from Product where IsCollectionOperate=1)
                set @ufn=1
                set @ufpage=1000--假设有10000000条数据
                while @ufn<=@ufpage
                 begin
                    begin tran
update UserFavorite set  UserFavorite.isSeriesUpdate=1,UserFavorite.ispop=1,
UserFavorite.FirstPublishTime = #temp2.LastPublishTime
from UserFavorite  inner join #temp2  on  UserFavorite.productID=#temp2.productid
where UserFavorite.ID between (@ufn-1)*10000 and @ufn*10000
                    set @ufn=@ufn+1
                    WAITFOR DELAY '00:00:02'; 
                     commit tran
                 end
                  --更新后重置更新状态
               update Product set IsCollectionOperate=0 where Id in(select productid from #temp2)
     
     truncate table #temp2 --删除原表数据,并重置自增列
     insert into #temp2 select ID,LastPublishTime from Product where Id in(select distinct productID from ProductAudio where IsCollectionOperate=1)
                set @ufn=1
                set @ufpage=1000--假设有10000000条数据
                while @ufn<=@ufpage
                 begin
                    begin tran
                      update UserFavorite set UserFavorite.isread=1,UserFavorite.ispop=1,
UserFavorite.FirstPublishTime = #temp2.LastPublishTime
from UserFavorite  inner join #temp2  on  UserFavorite.productID=#temp2.productid
where UserFavorite.ID between (@ufn-1)*10000 and @ufn*10000
                    set @ufn=@ufn+1
                    WAITFOR DELAY '00:00:02'; 
                     commit tran
                 end
                  update ProductAudio set IsCollectionOperate=0 where productid in (select productid from #temp2)
    
 end
-------------------------------------------------------------------------------------------------------------------------
 
 3. 批量更新递增列

--申明一个游标
DECLARE MyCursor CURSOR
FOR select id from productaudio where productid = 756407 order by linkurl

declare @x int
set @x=1

--打开一个游标
OPEN MyCursor

--循环一个游标
DECLARE @id int
FETCH NEXT FROM MyCursor INTO @id
WHILE @@FETCH_STATUS =0
BEGIN
update productaudio set audioname = @x where id = @id
set @x=@x+1
FETCH NEXT FROM MyCursor INTO @id
END

--关闭游标
CLOSE MyCursor
--释放资源
DEALLOCATE MyCursor

Sql语句摘要的更多相关文章

  1. 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  2. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  3. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作

    http://www.cnblogs.com/wgp13x/p/4934521.html 内容一样,样式好的版本. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据 ...

  4. 使用BAT批处理执行sql语句的代码

    使用BAT批处理执行sql语句的代码 有时候需要执行一些Sql语句时,不想开企业管理器,或者是发给客户执行但那边又不懂代码,这时就可以用下面方法 1.把待执行Sql保存在一个文件,这里为2011022 ...

  5. Python与开源GIS:在OGR中使用SQL语句进行查询

    摘要: 属性选择与空间选择都可以看作是OGR内置的选择功能,这两种功能可以解决大部分实际中的问题.但是也有这种时候,就是进行查询时的条件比较复杂.针对这种情况,OGR也提供了更加灵活的解决方案:支持使 ...

  6. 看懂SqlServer查询计划 SQL语句优化分析

    转自 http://www.cnblogs.com/fish-li/archive/2011/06/06/2073626.html 阅读目录 开始 SQL Server 查找记录的方法 SQL Ser ...

  7. 《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件

    <万能数据库查询分析器>实现使用SQL语句直接高效地访问文本文件 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要    用SQL语句来直接访问文本文件?是在做梦吗? ...

  8. 软件开发顶尖高手的杀手锏SQL语句

                  软件开发顶尖高手的杀手锏SQL语句                                                                     ...

  9. 由一条sql语句想到的子查询优化

    摘要:相信大家都使用过子查询,因为使用子查询可以一次性的完成很多逻辑上需要多个步骤才能完成的SQL操作,比较灵活,我也喜欢用,可最近因为一条包含子查询的select count(*)语句导致点开管理系 ...

随机推荐

  1. php时间 显示刚刚 几分钟前等

    功能示例: $now = time();foreach($sys_res as $k => $v){ $day = intval(floor(($now - $v->system_time ...

  2. git的操作

    学习源头: https://www.cnblogs.com/yaoxc/p/3946280.html https://www.cnblogs.com/jeremylee/p/5715289.html ...

  3. SQL万能密码:' or 1='1

    select name,pass from tbAdmin where name='admin' and pass='123456' 输入用户名:' or 1='1 SQL变成下面这个样子: sele ...

  4. 使用wireshark观察SSL/TLS握手过程--双向认证/单向认证

    SSL/TLS握手过程可以分成两种类型: 1)SSL/TLS 双向认证,就是双方都会互相认证,也就是两者之间将会交换证书.2)SSL/TLS 单向认证,客户端会认证服务器端身份,而服务器端不会去对客户 ...

  5. oracle里的查询转换

    oracle里的查询转换的作用 Oracle里的查询转换,有称为查询改写,指oracle在执行目标sql时可能会做等价改写,目的是为了更高效的执行目标sql 在10g及其以后的版本中,oracle会对 ...

  6. 转: SQL中的where条件,在数据库中提取与应用浅析

    SQL中的where条件,在数据库中提取与应用浅析 http://hedengcheng.com/?p=577 1问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当 ...

  7. MySQL 5.6/5.7 linux常见安装(tar,yum,script)

    该文章总结一下MySQL的常见安装方式,以tar,yum,script 三种方式来演示: 一般的公司都会有自己统一的数据库安装规范和模板,在生产环境请按照自己的规范来安装和使用,这里只演示和测试,供需 ...

  8. 静态实现AOP(翻译自MSDN)

    在.net实现AOP 本文通过一个简单的例子实现静态AOP.改例子主要实现客户的增删改查,突然有一天你的老板需要在程序上跟踪每个方法操作的运行日志. 主要分为5个步骤. 第一步:创建接口IReposi ...

  9. MySQL 性能测试经验

    一.背景 近期在进行资源调度管理平台的重构工作,其中的Resource/Property数据库设计,在没有更加优化的方案前,打算沿用当前平台的数据库结构:这就需要对当前平台的数据库结构进行剖析研究,并 ...

  10. temp3