點擊新增作業表單左側的步驟項,點擊右側地區下方的建立按鈕,輸入步驟名稱,類型請選擇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=‘WaterNet_DaFeng‘
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‘D:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\‘[email protected]+‘.ndf‘
--此處該為自己的資料檔案路徑,lui注釋2015-5-4(右擊伺服器-屬性-資料庫設定可看到)
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 RIGHT FOR 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檔案名稱
選擇建立分區左側的計劃項,然後點擊右側地區下方的建立按鈕,設定建立分區的時間間隔,圖中設定的是每天建立一個新的分區,使用者也可以自行修改,按月,按周,按自訂時間等
其他的條目,通知,警報,目標可自行設定,也可不設定,至此自動建立分區的計劃任務已成功設定.
END