小編給大家分享一下數據庫中如何創建分區的SP和job,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
站在用戶的角度思考問題,與客戶深入溝通,找到昭通網站設計與昭通網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:網站制作、網站設計、企業官網、英文網站、手機端網站、網站推廣、域名注冊、網絡空間、企業郵箱。業務覆蓋昭通地區。
創建SP
點擊(此處)折疊或打開
create procedure sp_maintain_partion_fg (
@tableName varchar(50),
@inputdate datetime
)
as begin
declare
@fileGroupName varchar(50),
@ndfName varchar(50),
@newNameStr varchar(50),
@fullPath varchar(50),
@newDay varchar(50),
@oldDay datetime,
@partFunName varchar(50),
@schemeName varchar(50),
@sqlstr varchar(1000),
@sql1 varchar(4000)
--set @tableName='DYDB'
set @newDay=CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,@inputdate), 0), 23 )--CONVERT(varchar(100), @inputdate, 23)--23:按天 114:按時間
set @oldDay=cast(CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,@inputdate)-1, 0), 112 ) as datetime)
set @newNameStr=left(Replace(Replace(@newDay,':','_'),'-','_'),7)
set @fileGroupName=N'G'+@newNameStr
set @ndfName=N'F'+@newNameStr+''
set @fullPath=N'F:\\SQLData\\ecodata\\'+@ndfName+'.ndf'
set @partFunName=N'pf_Time'
set @schemeName=N'ps_Time'
--print @fullPath
--print @fileGroupName
--print @ndfName
--創建文件組
if exists(select * from sys.filegroups where name=@fileGroupName)
begin
print '文件組存在,不需添加'
end
else
begin
exec('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
--print 'exec '+('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
print '新增文件組'
if exists(select * from sys.partition_schemes where name =@schemeName)
begin
exec('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
--print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print '修改分區方案'
end
print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print '修改分區方案'
if exists(select * from sys.partition_range_values where function_id=(select function_id from
sys.partition_functions where name =@partFunName) and value=@oldDay)
begin
exec('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
--print 'exec '+('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
print '修改分區函數'
end
end
--創建NDF文件
if exists(select * from sys.database_files where [state]=0 and (name=@ndfName or physical_name=@fullPath))
begin
print 'ndf文件存在,不需添加'
end
else
begin
exec('ALTER DATABASE '+@tableName+' ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']')
print 'ALTER DATABASE '+@tableName+' ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']'
print '新創建ndf文件'
end
--/*--------------------以上創建數據庫的文件組和物理文件------------------------*/
end
----分區函數
--if exists(select * from sys.partition_functions where name =@partFunName)
--begin
--print '此處修改需要在修改分區函數之前執行'
--end
--else
--begin
--exec('CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')')
----print 'CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')'
--print '新創建分區函數'
--end
----分區方案
--if exists(select * from sys.partition_schemes where name =@schemeName)
--begin
--print '此處修改需要在修改分區方案之前執行'
--end
--else
--begin
--exec('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')
----print ('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')
--print '新創建分區方案'
2. 增加job
點擊(此處)折疊或打開
declare @date date
set @date= DATEADD(mm,1,getdate())
print @date
exec sp_maintain_partion_fg 'ecodata',@date
看完了這篇文章,相信你對“數據庫中如何創建分區的SP和job”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
網站名稱:數據庫中如何創建分區的SP和job
網站路徑:http://www.2m8n56k.cn/article36/iesopg.html
成都網站建設公司_創新互聯,為您提供手機網站建設、網站設計公司、網站建設、企業網站制作、動態網站、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯