灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3133回复:0

[SQL Server]自己写的游标操作语句

楼主#
更多 发布于:2012-09-04 18:08


自己写的游标操作语句

北大国家发展研究院论坛原来采用老式论坛,为接入Discuz论坛,需要对原来的数据进行处理,
经过一周的数据处理,终于远程升级。

部分升级脚本(迁移贴子)如下:

declare my_cursor cursor scroll  
for    www.atcpu.com  
SELECT [ID]
      ,[TITLE]
      ,[CONTENT]
      ,[AUTHOR_ID]
      ,[AUTHOR_NAME]
      ,[AUTHOR_TEXT]
      ,[AUTHOR_SIG]
      ,[REPLY_TO]
      ,[POST_TIME]
      ,[BELONG_TO]
      ,[LOCKED]
      ,[POLL_COUNT]ccc
      ,[CLICK_COUNT]
      ,[REPLY_COUNT]
      ,[LAST_UPDATE]
      ,[DELETED]
      ,[IP]
      ,[LINKURL]
      ,[LINKIMG]
      ,[Flinkname]
      ,[Flinkurl]
FROM [ccerforum].[dbo].[ARTICLES] where id>0   ORDER BY ID

open my_cursor

declare @ID int,@TITLE nvarchar(100)
      ,@CONTENT nvarchar(3000)
      ,@AUTHOR_ID int   www.atcpu.com  
      ,@AUTHOR_NAME   nvarchar(40)
      ,@AUTHOR_TEXT   nvarchar(120)
      ,@AUTHOR_SIG nvarchar(100)
      ,@REPLY_TO   int  
      ,@POST_TIME   smalldatetime
      ,@BELONG_TO   int
      ,@LOCKED smallint
      ,@POLL_COUNT int
      ,@CLICK_COUNT int
      ,@REPLY_COUNT int
      ,@LAST_UPDATE smalldatetime
      ,@DELETED smallint
      ,@IP nchar(15)
      ,@LINKURL nvarchar(128)
      ,@LINKIMG   nvarchar(128)
      ,@Flinkname nvarchar(64)
      ,@Flinkurl   nvarchar(64)
declare @NewTopicId int,@NewLayer int,@AttachmentNum int,@lastpostid int

fetch next from my_cursor into @ID ,@TITLE,@CONTENT,@AUTHOR_ID,
@AUTHOR_NAME,@AUTHOR_TEXT,@AUTHOR_SIG,@REPLY_TO,@POST_TIME,@BELONG_TO,
@LOCKED,@POLL_COUNT,@CLICK_COUNT,@REPLY_COUNT,@LAST_UPDATE,@DELETED,@IP,
@LINKURL,@LINKIMG,@Flinkname,@Flinkurl    

while(@@fetch_status=0)

begin  
if @REPLY_TO is null or @REPLY_TO=''
set @NewLayer=0
else
set @NewLayer=1
if @Flinkurl is null or @Flinkurl=''
set @AttachmentNum=0
else
set @AttachmentNum=1

if @AUTHOR_ID is null or @AUTHOR_ID=''
set @AUTHOR_ID=isnull((SELECT [ID] FROM [ccerforum].[dbo].[USERS] where [NAME]=@AUTHOR_TEXT ),0)

if @REPLY_TO is null   or @REPLY_TO=''
begin   www.atcpu.com  
print 'replayto= null'
set @lastpostid=(select(max(pid)+1) from [nsd_forum_discuz25].[dbo].[dnt_posts1] )
INSERT INTO [nsd_forum_discuz25].[dbo].[dnt_topics]
           ([fid]
           ,[iconid]
           ,[typeid]
           ,[readperm]
           ,[price]
           ,[poster]
           ,[posterid]
           ,[title]
           ,[postdatetime]
           ,[lastpost]
           ,[lastpostid]
           ,[lastposter]
           ,[lastposterid]
           ,[views]
           ,[replies]
           ,[displayorder]
           ,[highlight]
           ,[digest]
           ,[rate]
           ,[hide]
           ,[poll]
           ,[attachment]
           ,[moderated]
           ,[closed]
           ,[magic]
           ,[identify]
           ,[special])
     VALUES
           (@BELONG_TO
           ,0
           ,0
           ,0
           ,0
           ,@AUTHOR_NAME
           ,@AUTHOR_ID
           ,@TITLE  
           ,@POST_TIME  
           ,@POST_TIME  
           ,@AUTHOR_ID
           ,@AUTHOR_NAME
           ,@AUTHOR_ID
           ,100
           ,@REPLY_COUNT
           ,0
           ,''
           ,0
           ,0
           ,0
           ,0
           ,@AttachmentNum
           ,0
           ,0   www.atcpu.com  
           ,0
           ,0
           ,0)
set @NewTopicId=@@IDENTITY

end
else
begin
set @NewTopicId=isnull((select tid from [nsd_forum_discuz25].[dbo].[dnt_posts1] where pid=@REPLY_TO),999)
end
if @AttachmentNum=1
begin
INSERT INTO [nsd_forum_discuz25].[dbo].[dnt_attachments]
           ([uid]
           ,[tid]
           ,[pid]
           ,[postdatetime]
           ,[readperm]
           ,[filename]
           ,[description]
           ,[filetype]
           ,[filesize]
           ,[attachment]
           ,[downloads])
     VALUES
           (@AUTHOR_ID
           ,@NewTopicId
           ,@ID
           ,@POST_TIME  
           ,0
           ,isnull(@Flinkurl,'')
           ,' '
           ,'image/pjpeg'
           ,751660
           ,isnull(@Flinkurl,'')
           ,100)

end

INSERT INTO [nsd_forum_discuz25].[dbo].[dnt_posts1]
           ([pid],[fid]
           ,[tid]
           ,[parentid]
           ,[layer]
           ,[poster]
           ,[posterid]
           ,[title]
           ,[postdatetime]
           ,[message]
           ,[ip]
           ,[lastedit]
           ,[invisible]
           ,[usesig]
           ,[htmlon]
           ,[smileyoff]
           ,[parseurloff]
           ,[bbcodeoff]
           ,[attachment]
           ,[rate]
           ,[ratetimes])
     VALUES
           (@ID
           ,@BELONG_TO
           ,@NewTopicId
           ,isnull(@REPLY_TO,0)  
           ,@newlayer
           ,@AUTHOR_NAME
           ,@AUTHOR_ID
           ,@TITLE
           ,@POST_TIME
           ,@CONTENT
           ,@IP
           ,@AUTHOR_NAME
           ,0
           ,1
           ,1
           ,1
           ,1
           ,1
           ,@AttachmentNum
           ,0   www.atcpu.com  
           ,0)

set @lastpostId=@ID
update [nsd_forum_discuz25].[dbo].[dnt_topics] set [lastpostId]=@lastpostId where tid=@NewTopicId
fetch next from my_cursor into @ID ,@TITLE  
      ,@CONTENT    
      ,@AUTHOR_ID    
      ,@AUTHOR_NAME    
      ,@AUTHOR_TEXT    
      ,@AUTHOR_SIG  
      ,@REPLY_TO    
      ,@POST_TIME    
      ,@BELONG_TO    
      ,@LOCKED  
      ,@POLL_COUNT    
      ,@CLICK_COUNT  
      ,@REPLY_COUNT  
      ,@LAST_UPDATE  
      ,@DELETED  
      ,@IP  
      ,@LINKURL  
      ,@LINKIMG    
      ,@Flinkname  
      ,@Flinkurl    

end  
go
--fetch first from my_cursor into @fname  
--
--update authors set au_fname='aa' where current of my_cursor

close my_cursor

deallocate my_cursor

--更新论坛dnt_topics的fid  
--update [nsd_forum_discuz25].[dbo].[dnt_topics] a set a.fid=(select b.fid from [nsd_forum_discuz25].[dbo].[dnt_forums] b where b.oldfid=a.fid)

declare topic_cursor cursor scroll  
for  
SELECT [tid],[fid] from [nsd_forum_discuz25].[dbo].[dnt_topics] where fid<>0 ORDER BY tid

open topic_cursor

declare @tid int,@fid int

fetch next from topic_cursor into @tid,@FID    

while(@@fetch_status=0)

begin  

update [nsd_forum_discuz25].[dbo].[dnt_topics] set fid=isnull((select fid from [nsd_forum_discuz25].[dbo].[dnt_forums] where oldfid=@fid),999) where tid=@tid  
print str(@fid)
fetch next from topic_cursor into @tid,@FID
  www.atcpu.com  
end  

close topic_cursor

deallocate topic_cursor

--更新论坛 dnt_posts1的fid
declare post_cursor cursor scroll  
for  
SELECT [pid],[fid] from   [nsd_forum_discuz25].[dbo].[dnt_posts1] where pid<>0 ORDER BY pid

open post_cursor

declare @pid int

fetch next from post_cursor into @pid,@fid    

while(@@fetch_status=0)
  www.atcpu.com  
begin  

update [nsd_forum_discuz25].[dbo].[dnt_posts1] set fid=(select fid from [nsd_forum_discuz25].[dbo].[dnt_forums] where oldfid=@fid) where pid=@pid  
--print str(@parentid)
fetch next from post_cursor into @pid,@fid

end  

close post_cursor

deallocate post_cursor


作者 ctz168

喜欢0 评分0
游客

返回顶部