千家信息网

数据库中如何创建分区的SP和job

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,小编给大家分享一下数据库中如何创建分区的SP和job,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!创建SP点击(此处)折叠或打开create procedure sp_maintai
千家信息网最后更新 2026年11月01日数据库中如何创建分区的SP和job

小编给大家分享一下数据库中如何创建分区的SP和job,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

  1. 创建SP

点击(此处)折叠或打开

  1. create procedure sp_maintain_partion_fg (

  2. @tableName varchar(50),

  3. @inputdate datetime

  4. )

  5. as begin

  6. declare

  7. @fileGroupName varchar(50),

  8. @ndfName varchar(50),

  9. @newNameStr varchar(50),

  10. @fullPath varchar(50),

  11. @newDay varchar(50),

  12. @oldDay datetime,

  13. @partFunName varchar(50),

  14. @schemeName varchar(50),

  15. @sqlstr varchar(1000),

  16. @sql1 varchar(4000)



  17. --set @tableName='DYDB'

  18. set @newDay=CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,@inputdate), 0), 23 )--CONVERT(varchar(100), @inputdate, 23)--23:按天 114:按时间

  19. set @oldDay=cast(CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,@inputdate)-1, 0), 112 ) as datetime)

  20. set @newNameStr=left(Replace(Replace(@newDay,':','_'),'-','_'),7)

  21. set @fileGroupName=N'G'+@newNameStr

  22. set @ndfName=N'F'+@newNameStr+''

  23. set @fullPath=N'F:\\SQLData\\ecodata\\'+@ndfName+'.ndf'

  24. set @partFunName=N'pf_Time'

  25. set @schemeName=N'ps_Time'



  26. --print @fullPath

  27. --print @fileGroupName

  28. --print @ndfName





  29. --创建文件组

  30. if exists(select * from sys.filegroups where name=@fileGroupName)

  31. begin

  32. print '文件组存在,不需添加'

  33. end

  34. else

  35. begin

  36. exec('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')

  37. --print 'exec '+('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')

  38. print '新增文件组'

  39. if exists(select * from sys.partition_schemes where name =@schemeName)

  40. begin

  41. exec('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')

  42. --print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')

  43. print '修改分区方案'

  44. end



  45. print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')

  46. print '修改分区方案'



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

  48. sys.partition_functions where name =@partFunName) and value=@oldDay)

  49. begin

  50. exec('alter partition function '+@partFunName+'() split range('''+@newDay+''')')

  51. --print 'exec '+('alter partition function '+@partFunName+'() split range('''+@newDay+''')')

  52. print '修改分区函数'

  53. end

  54. end



  55. --创建NDF文件

  56. if exists(select * from sys.database_files where [state]=0 and (name=@ndfName or physical_name=@fullPath))

  57. begin

  58. print 'ndf文件存在,不需添加'

  59. end

  60. else

  61. begin

  62. exec('ALTER DATABASE '+@tableName+' ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']')

  63. print 'ALTER DATABASE '+@tableName+' ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']'



  64. print '新创建ndf文件'

  65. end

  66. --/*--------------------以上创建数据库的文件组和物理文件------------------------*/

  67. end





  68. ----分区函数

  69. --if exists(select * from sys.partition_functions where name =@partFunName)

  70. --begin

  71. --print '此处修改需要在修改分区函数之前执行'

  72. --end

  73. --else

  74. --begin

  75. --exec('CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')')

  76. ----print 'CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')'

  77. --print '新创建分区函数'

  78. --end

  79. ----分区方案

  80. --if exists(select * from sys.partition_schemes where name =@schemeName)

  81. --begin

  82. --print '此处修改需要在修改分区方案之前执行'

  83. --end

  84. --else

  85. --begin

  86. --exec('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')

  87. ----print ('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')

  88. --print '新创建分区方案'


2. 增加job

点击(此处)折叠或打开

  1. declare @date date

  2. set @date= DATEADD(mm,1,getdate())

  3. print @date

  4. exec sp_maintain_partion_fg 'ecodata',@date

看完了这篇文章,相信你对"数据库中如何创建分区的SP和job"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

文件 方案 函数 数据 数据库 篇文章 完了 时间 更多 物理 知识 行业 资讯 资讯频道 频道 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 百单网互联网科技有限公司 互联网科技公司华为 科技制造互联网公司 互联网公司 科技公司 互联网金融 科技 亿账通互联网科技 居家互联网科技有限公司 佩信高科技互联网 团飞科技与伯堂互联网服务平台 茂名市互联网科技产业园 栖莱科技互联网股份有限公司 上海钢联属于互联网科技股吗 云南省自由互联网科技有限公司 上海互联网科技公司ppt 互联网风格科技公司名称 广州有谷有贝互联网科技有限公司 北京互联网科技类企业分布 安徽零酒互联网科技有限公司 数字科技和互联网金融哪个好 高科技和互联网的现代国家 互联网科技有限公司 中关村互联网 科技集团 江苏根荣互联网科技 互联网科技公司华为 美国互联网科技巨头监管 互联网科技博客 广西拼得多互联网科技有限公司 西安小鹰速贷互联网科技有限公司 沐思想互联网科技有限公司 陕西九四连强互联网科技有限公司
0