微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver 常用脚本

1.数据库中表的备份

SELECT * INTO DestTable FROM SourceTable

2.同表中的数据复制,例子:将2013年4月11日当日的数据复制一份(且日期字段增加1年)

insert into table
select dateadd(month,12,date),id,value from table
where date between '2013-04-11 00:00:01' and '2013-04-11 23:59:59'

3.同表中的数据更新,例子:将某一时间段中的数据替换成另一时间段中的数据(需要保证原时间段数据量大于目标时间段数据量)

alter procedure  test_proc 
as
begin 
  declare @date datetime
  declare @id int
  declare @value varchar(50)
  declare @date1 datetime
  declare @id1 int
  declare @value1 varchar(50)
  
  DECLARE cursor_dest_list cursor  
  FOR SELECT date,value FROM table_1 where date between '2013-04-11 14:00:00' and '2013-04-11 15:00:00'
  order by date asc 
  OPEN cursor_dest_list 
  
  declare cursor_source_list cursor
  FOR SELECT date,value FROM table_1 where date between '2013-04-11 19:00:00' and '2013-04-11 22:00:00'  
  order by date asc
  open cursor_source_list
              
  FETCH NEXT from cursor_dest_list into @date,@id,@value  
  WHILE @@FETCH_STATUS = 0     
  BEGIN
    FETCH NEXT from cursor_source_list into @date1,@id1,@value1  
    WHILE @@FETCH_STATUS = 0     
    BEGIN
      update table_1 set value = @value1  where date = @date
      break
    END
    FETCH NEXT from cursor_dest_list into @date,@value   
  END
   
  CLOSE cursor_source_list               
  DEALLOCATE cursor_source_list
  CLOSE cursor_dest_list               
  DEALLOCATE cursor_dest_list
end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐