国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 青岛市| 云安县| 霍林郭勒市| 龙里县| 偏关县| 宾川县| 桐庐县| 滨海县| 磴口县| 普兰店市| 叙永县| 巨野县| 保亭| 巧家县| 宜宾市| 张家港市| 九寨沟县| 鹤山市| 昆明市| 永靖县| 丹东市| 海伦市| 湘潭县| 武鸣县| 上林县| 丰县| 普洱| 彭山县| 阿巴嘎旗| 镇平县| 佳木斯市| 铁岭市| 阳原县| 平度市| 郸城县| 晋中市| 饶阳县| 泸溪县| 闸北区| 交口县| 大足县|