sql2008?

固定增量的資料,自動建立分割槽作業.

工具/原料

MsSQL2008

步驟一:建立分割槽的計劃任務

開啟MsSQL2008,找到作業該項,如果打不開或者SQL Server代理是未啟動狀態,請先在windows服務中啟動SQL Server代理(參考圖片),

sql2008 計劃自動建立資料庫分割槽

sql2008 計劃自動建立資料庫分割槽

右擊MsSQL2008物件資源管理器中的作業,選擇新建作業,輸入該作業你想用的名稱,類別不用管,說明裡面是輸入一些該作業完成的功能,可不寫,請務必勾選已啟用複選框.

sql2008 計劃自動建立資料庫分割槽

點選新建作業窗體左側的步驟項,點選右側區域下方的新建按鈕,輸入步驟名稱,型別請選擇Transact-SQL指令碼(T-SQL),執行身份預設,資料庫請選擇要進行分割槽的資料庫,請不要選擇master預設的,命令文字框中輸入如下程式碼:

/*--------------------建立資料庫的檔案組和物理檔案------------------------*/

declare @tableName varchar(50), @fileGroupName varchar(50), @ndfName varchar(50), @newNameStr varchar(50), @fullPath

varchar(50), @newDay varchar(50), @oldDay datetime, @partFunName varchar(50), @schemeName varchar(50)

set @tableName='要分割槽的資料庫名稱'

set @newDay=CONVERT(varchar(100), GETDATE(), 23)--23:按天 114:按時間

set @oldDay=cast(CONVERT(varchar(10),dateadd(day,-1,getdate()), 120 ) as datetime)

set @newNameStr=Replace(Replace(@newDay,':','_'),'-','_')

set @fileGroupName=N'G'[email protected]

set @ndfName=N'F'[email protected]+''

set @fullPath=N'E:\\SQLDataBase\\UserData\\'[email protected]+'.ndf'

set @partFunName=N'pf_Time'

set @schemeName=N'ps_Time'

--建立檔案組

if exists(select * from sys.filegroups where [email protected])

begin

print '檔案組存在,不需新增'

end

else

begin

exec('ALTER DATABASE '[email protected]+' ADD FILEGROUP ['[email protected]+']')

print '新增檔案組'

if exists(select * from sys.partition_schemes where name [email protected])

begin

exec('alter partition scheme '[email protected]+' next used ['[email protected]+']')

print '修改分割槽方案'

end

if exists(select * from sys.partition_range_values where function_id=(select function_id from

sys.partition_functions where name [email protected]) and [email protected])

begin

exec('alter partition function '[email protected]+'() split range('''[email protected]+''')')

print '修改分割槽函式'

end

end

--建立NDF檔案

if exists(select * from sys.database_files where [state]=0 and ([email protected] or [email protected]))

begin

print 'ndf檔案存在,不需新增'

end

else

begin

exec('ALTER DATABASE '[email protected]+'ADD FILE (NAME ='[email protected]+',

FILENAME = '''[email protected]+''')TO FILEGROUP ['[email protected]+']')

print '新建立ndf檔案'

end

/*--------------------以上建立資料庫的檔案組和物理檔案------------------------*/

--分割槽函式

if exists(select * from sys.partition_functions where name [email protected])

begin

print '此處修改需要在修改分割槽函式之前執行'

end

else

begin

exec('CREATE PARTITION FUNCTION '[email protected]+'(DateTime)AS RANGE RIGHTFOR VALUES ('''[email protected]

+''')')

print '新建立分割槽函式'

end

--分割槽方案

if exists(select * from sys.partition_schemes where name [email protected])

begin

print '此處修改需要在修改分割槽方案之前執行'

end

else

begin

exec('CREATE PARTITION SCHEME '[email protected]+' AS PARTITION '[email protected]+' TO

(''PRIMARY'','''[email protected]+''')')

print '新建立分割槽方案'

end

print '---------------以下是變數定義值顯示---------------------'

print '當前資料庫:'[email protected]

print '當前日期:'[email protected]+'(用作隨機生成的各種名稱和分割槽界限)'

print '合法命名方式:'[email protected]

print '檔案組名稱:'[email protected]

print 'ndf物理檔名稱:'[email protected]

print '物理檔案完整路徑:'[email protected]

print '分割槽函式:'[email protected]

print '分割槽方案:'[email protected]

/*

--檢視建立的分割槽函式

select * from sys.partition_functions

--檢視分割槽函式的臨界值

select * from sys.partition_range_values

--查詢分割槽方案

select * from sys.partition_schemes

--查詢表資料在哪個分割槽中儲存,where條件查詢第一個分割槽中存在的資料

select *,$partition.pf_SaveTime(分割槽欄位) as Patition from 表名 where $partition.pf_SaveTime(分割槽欄位)=1

*/

GO

點選確定按鈕

上述程式碼中的變數名稱,路徑等均可自行修改,上述是按天為單位,以G開頭的日期作為檔案組名稱,以F開頭的日期作為物理分割槽檔名即ndf檔名稱

sql2008 計劃自動建立資料庫分割槽

選擇新建分割槽左側的計劃項,然後點選右側區域下方的新建按鈕,設定新建分割槽的時間間隔,圖中設定的是每天建立一個新的分割槽,使用者也可以自行修改,按月,按周,按自定義時間等

其他的條目,通知,警報,目標可自行設定,也可不設定,至此自動建立分割槽的計劃任務已成功設定.

sql2008 計劃自動建立資料庫分割槽

步驟二:對錶應用分割槽方案和分割槽函式

右擊要分割槽的表,選擇儲存選單下的建立分割槽,上述步驟一中建立的分割槽函式是按datetime型別進行的分割槽,所以建立分割槽的時候需要選擇相應型別的欄位作為分割槽依據,使用者也可以根據int型或其他型別的欄位進行分割槽,選擇下一步,使用現有分割槽函式下一步使用現有分割槽方案,下一步會自動按照分割槽方案執行的日期進行分割槽,繼續點選下一步選擇立即執行,完成後即可完成的整體的表分割槽自動執行.

需注意:剛設定完第一步的計劃任務,可能不會執行第一步的分割槽方案的程式碼,也就意味著沒有建立分割槽函式和分割槽方案,第二步設定的時候使用現有分割槽函式和使用現有分割槽方案也就不可用,可先把第一步的程式碼執行一遍即可.

sql2008 計劃自動建立資料庫分割槽

sql2008 計劃自動建立資料庫分割槽

sql2008 計劃自動建立資料庫分割槽

sql2008 計劃自動建立資料庫分割槽

sql2008 計劃自動建立資料庫分割槽

注意事項

建立分割槽程式碼中的變數等一些設定,需根據使用者自身環境自行設定,不要直接複製

資料, 計劃, 資料庫, 作業,
相關問題答案