create procedure #pr_CreateFileGroup
@dbname nvarchar(max),
@filegroupname nvarchar(max)
as
begin
declare @sql nvarchar(max)
select @sql = "
if not exists (select 1 from sys.filegroups where [name] = """ + @filegroupname + """)
begin
alter database " + @dbname + " add filegroup " + @filegroupname + "
end
"
print @sql
exec sp_executesql @sql
end
go
-- ================================================================================================
--调用临时存储过程,创建文件组
exec #pr_CreateFileGroup "DB_Test2", N"Project_data"
exec #pr_CreateFileGroup "DB_Test2", N"Project_index"
-- ================================================================================================
脚本运行情况:
数据库中文件组添加效果: