dhl:不用游标批量更新的SQL语句
2009-04-21 15:32
351 查看
在更新一批记录时使用如下语句:
update publish set contentid=
(select top 1 articles.contentid from articles
where articles.articleID=publish.objectID
)
--where publish.objectid=@objectID
前提是:publish表的记录不能大于Article的记录,即要插入的目标表中示能插入null,否则会提示错误。
全来没办法,改为游标:
SET NOCOUNT ON
DECLARE @contentID int
declare @objectID int
declare @countnumber int
set @countnumber=0
DECLARE publish_cursor CURSOR FOR
select a.contentid,a.articleID from publish p
inner join articles a on a.articleID=p.objectID
where objectid>0 and p.contentid<> a.contentid
and (p.cellid=160 or cellid=138)
OPEN publish_cursor
FETCH NEXT FROM publish_cursor
INTO @contentID,@objectID
WHILE @@FETCH_STATUS = 0
BEGIN
print @contentID
print @objectID
--修改记录
update publish set ContentID=@contentID where objectid=@objectID
--修改结束
FETCH NEXT FROM publish_cursor into @contentID,@objectID
END
CLOSE publish_cursor
DEALLOCATE publish_cursor
GO
select p.publishid,p.contentid,a.contentid,p.objectID,a.articleID from publish p
inner join articles a on a.articleID=p.objectID
where objectid>0 and p.contentid<> a.contentid
and (p.cellid=160 or cellid=138)
go
-- update publish set contentid=0 where (cellid=160 or cellid=138)
-- select * from publish p where ( p.cellid=160 or cellid=138)
在没有更好的办法呢?
其实还可以这样:
update publish set contentid= a.contentid
from articles a inner join publish p on p.objectID=a.articleID
where cellid=138
-- select * from publish where cellid=138
-- update publish set contentid=0 where cellid=138
update publish set contentid=
(select top 1 articles.contentid from articles
where articles.articleID=publish.objectID
)
--where publish.objectid=@objectID
前提是:publish表的记录不能大于Article的记录,即要插入的目标表中示能插入null,否则会提示错误。
全来没办法,改为游标:
SET NOCOUNT ON
DECLARE @contentID int
declare @objectID int
declare @countnumber int
set @countnumber=0
DECLARE publish_cursor CURSOR FOR
select a.contentid,a.articleID from publish p
inner join articles a on a.articleID=p.objectID
where objectid>0 and p.contentid<> a.contentid
and (p.cellid=160 or cellid=138)
OPEN publish_cursor
FETCH NEXT FROM publish_cursor
INTO @contentID,@objectID
WHILE @@FETCH_STATUS = 0
BEGIN
print @contentID
print @objectID
--修改记录
update publish set ContentID=@contentID where objectid=@objectID
--修改结束
FETCH NEXT FROM publish_cursor into @contentID,@objectID
END
CLOSE publish_cursor
DEALLOCATE publish_cursor
GO
select p.publishid,p.contentid,a.contentid,p.objectID,a.articleID from publish p
inner join articles a on a.articleID=p.objectID
where objectid>0 and p.contentid<> a.contentid
and (p.cellid=160 or cellid=138)
go
-- update publish set contentid=0 where (cellid=160 or cellid=138)
-- select * from publish p where ( p.cellid=160 or cellid=138)
在没有更好的办法呢?
其实还可以这样:
update publish set contentid= a.contentid
from articles a inner join publish p on p.objectID=a.articleID
where cellid=138
-- select * from publish where cellid=138
-- update publish set contentid=0 where cellid=138
相关文章推荐
- 利用游标循环进行更新插入的SQL事务语句
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- ASP.NET基础教程-使用CommandBuilder对象自动生成SQL语句对数据进行批量更新
- ASP.NET基础教程-使用CommandBuilder对象自动生成SQL语句对数据进行批量更新
- mybatis--mysql 批量插入批量更新在一条sql语句中完成
- 批量更新的sql语句写法
- SQL 批量更新语句
- 【数据库SQL】批量更新促发器游标的方法
- Update Select 嵌套更新或批量更新一句SQL语句搞定
- 动态组合SQL语句方式实现批量更新
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- 批量更新sql 语句
- PHP拼接SQL语句批量更新多个字段
- 不用游标也能遍历记录的sql语句实例
- 动态组合SQL语句方式实现批量更新的实例
- 巧用LinqToSql做数据库快速单表备份,增量更新,批量更新等的Sql语句.
- 利用游标循环进行更新插入的SQL事务语句
- 批量插入更新的 sql语句
- 一条insert语句批量插入多条记录 AND 多条件游标更新 变CASE WHEN更新法提高速度
- 利用游标循环进行更新插入的SQL事务语句