首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Sybase >

求教在Sybase Esql中使用游标对数据库表逐条备份并删除的方法解决思路

2012-02-13 
求教在Sybase Esql中使用游标对数据库表逐条备份并删除的方法前提条件:1. 数据库表结构完全未知2. 使用游

求教在Sybase Esql中使用游标对数据库表逐条备份并删除的方法
前提条件:
1. 数据库表结构完全未知
2. 使用游标逐行将数据备份到另一张结构完全相同的表后,删除该行数据
望高手指教!

[解决办法]
在存储过程中使用游标,循环读取,然后依次插入到另外一张表中。

以下给出一个参考例子(该存储过程中使用了临时表作为中间表来转换):

 

SQL code
CREATE PROCEDURE dbo.import_acs_emp_info        @o_err int OUTPUT    AS  begin      declare     @i_emp_id  int,     @i_degree char(2),         --学历     @i_prof_promote_rank  char(2),   --专技工资职务     @i_prof_official      char(3),   --工人专技职务     @i_emp_class          char(6),   --人员类别                          @emp_id               int,     @org_id               int,     @dept_id              int,             @work_level           char(8)        -------------------------------------------           --用来获取执行时间                declare       @nowtime    varchar(100)           -------------------------------------------                              create table #temp_emp_info     (             emp_id     int    null,             org_id     int    null,             dept_id    int    null,         )          select @nowtime=getdate()                           print @nowtime                           print '开始插入000'               insert into #temp_emp_info   select EMP_ID,START_ORG_ID,LOCAL_OFFICE from hrmsemp_info where EMP_CLASS <> '37'                                       select  @o_err=@@error               if @o_err<>0                   begin                      return @o_err                  end         select @work_level ='01'         declare emp_curs cursor  for               select emp_id,org_id,dept_id from #temp_emp_info          open emp_curs        fetch emp_curs into @emp_id,@org_id,@dept_id              while (@@sqlstatus=0)                 begin                    insert into t_acsemp_info_bas(emp_id,org_id,dept_id,work_level) values               (@emp_id,@org_id,@dept_id,@work_level)                       fetch emp_curs into    @emp_id,@org_id,@dept_id                 end     select  @o_err=@@error               if @o_err<>0                   begin                      return @o_err                  end         close  emp_curs           deallocate cursor emp_curs          select @nowtime=getdate()                   print @nowtime                   print '导入人员结束'     end 

热点排行