国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

根據(jù)當(dāng)月數(shù)據(jù)庫自動生成下個月數(shù)據(jù)庫--1

2024-07-21 02:05:36
字體:
供稿:網(wǎng)友

/*--原帖地址:http://community.csdn.net/expert/topic/3818/3818559.xml?temp=.9593317--*/

/*--處理要求

在sql數(shù)據(jù)庫里有一個名為pos200502的database,每個月會有一個類似于這樣名稱(pos200502 pos200503)的database

我該如何利用sql server的自動作業(yè)+一段存儲過程,實現(xiàn)以下功能:1.每個月的25號,自動創(chuàng)建一個下一個月的database,database名字定為:posyyyymm (yyyymm是年和月,始終是執(zhí)行操作時間的下一個月)2.再將本月database的所有結(jié)構(gòu)(包括表、視圖、存儲過程等)一模一樣的復(fù)制到下一個月的database中。(注意僅復(fù)制結(jié)構(gòu),不復(fù)制任何數(shù)據(jù)!)--*/

---方法1. 備份+恢復(fù)use mastergo

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sp_proccopydb]') and objectproperty(id, n'isprocedure') = 1)drop procedure [dbo].[sp_proccopydb]go

/*--數(shù)據(jù)庫自動復(fù)制

將指定前緣的數(shù)據(jù)庫,復(fù)制為一個以當(dāng)前月份+1為庫名的數(shù)據(jù)庫中,并且清除所有的數(shù)據(jù)例如,數(shù)據(jù)庫前緣為 pos ,當(dāng)前日期為 2005-3-27則要求復(fù)制數(shù)據(jù) pos200503 為 pos200504,并且清空里面的數(shù)據(jù)

用備份+恢復(fù)的方法實現(xiàn)好處是在清理數(shù)據(jù)時,可以設(shè)置條件,保留指定的數(shù)據(jù)不好的地方是數(shù)據(jù)多時,速度慢,消耗的資源多

--鄒建 2005.03(引用請保留此信息)--*/

/*--調(diào)用示例

-- 復(fù)制 posexec sp_proccopydb 'pos'--*/create proc [email protected]_head sysname=n''  --數(shù)據(jù)庫前綴asdeclare @sdbname sysname,@ddbname sysnamedeclare @s nvarchar(4000),@bkfile nvarchar(1000),@move nvarchar(4000)

--復(fù)制的源庫名及目標(biāo)庫名if @db_head is null set @db_head=n''

select @[email protected]_head+convert(char(6),getdate(),112),@[email protected]_head+convert(char(6),dateadd(month,1,getdate()),112)

if db_id(@sdbname) is nullbeginraiserror(n'源數(shù)據(jù)庫"%s"不存在',1,16,@sdbname)returnend

if db_id(@ddbname) is not nullbeginraiserror(n'目標(biāo)數(shù)據(jù)庫"%s"已經(jīng)存在',1,16,@ddbname)returnend

--臨時備份文件名select top 1 @bkfile=rtrim(reverse(filename)) from master.dbo.sysfiles where name=n'master'select @bkfile=stuff(@bkfile,1,charindex('/',@bkfile),n''),@bkfile=reverse(stuff(@bkfile,1,charindex('/',@bkfile),n''))+n'/backup/'+cast(newid() as nvarchar(36))+n'.bak'

--數(shù)據(jù)文件移動語句set @s=n'set @move=n''''select @[email protected]+n'',move ''+quotename(rtrim(name),n'''''''')+n'' to ''+quotename(rtrim(case when charindex(n'+quotename(@sdbname,n'''')+n',filename)>0then stuff(filename,charindex(n'+quotename(@sdbname,n'''')+n',filename),'+cast(len(@sdbname) as nvarchar)+n',n'+quotename(@ddbname,n'''')+n')else reverse(stuff(reverse(filename),charindex(''/'',reverse(filename)),0,+n''_''+reverse(n'+quotename(@ddbname,n'''')+n')))end),n'''''''')from '+quotename(@sdbname)+n'.dbo.sysfiles'exec sp_executesql @s,n'@move nvarchar(4000) out',@move out

--備份源數(shù)據(jù)庫set @s=n'backup database '+quotename(@sdbname)+n' to [email protected] with format'exec sp_executesql @s,n'@bkfile nvarchar(1000)',@bkfile

--還原為目標(biāo)數(shù)據(jù)庫set @s=n'restore database '+quotename(@ddbname)+n' from [email protected] with replace'[email protected] sp_executesql @s,n'@bkfile nvarchar(1000)',@bkfile

--刪除臨時備份文件set @s='del "'[email protected]+'"'exec master..xp_cmdshell @s,no_output

--清理目標(biāo)數(shù)據(jù)庫中的所有數(shù)據(jù)set @s=n'use '+quotename(@ddbname)+n'exec sp_msforeachtable @command1=n''truncate table ?'',@whereand=n'' and objectproperty(o.id,n''''tablehasforeignref'''')=0''exec sp_msforeachtable @command1=n''delete from ?'',@whereand=n'' and objectproperty(o.id,n''''tablehasforeignref'''')=1'''exec sp_executesql @sgo

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湟源县| 马关县| 霍邱县| 安仁县| 上林县| 朝阳区| 海口市| 荣昌县| 元朗区| 博湖县| 新宾| 红安县| 临汾市| 平顺县| 双城市| 阳东县| 石渠县| 高陵县| 衡山县| 砀山县| 阿荣旗| 木里| 綦江县| 塔城市| 同仁县| 乐亭县| 随州市| 滨州市| 舟山市| 锦州市| 桐庐县| 泰顺县| 罗田县| 沂源县| 浏阳市| 晴隆县| 龙门县| 常宁市| 察雅县| 云和县| 宽甸|