加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver 常用脚本

发布时间:2020-12-12 14:04:11 所属栏目:MsSql教程 来源:网络整理
导读:1.数据库中表的备份 SELECT * INTO DestTable FROM SourceTable 2.同表中的数据复制,例子:将2013年4月11日当日的数据复制一份(且日期字段增加1年) insert into tableselect dateadd(month,12,date),id,value from tablewhere date between '2013-04-11 00:0

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读