我需要为我在 SQL Server 中的架构创建一个文件组。数据库是空的,我只想创建模式及其文件组。
我怎样才能做到这一点?
您可以使用手动指定的名称创建数据库文件组:MY_DB_NAME_Data.MDF、MY_DB_NAME_Log.LDF 等(对于 SQL Express,请在 c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ 搜索):
DECLARE @sqlcmd nvarchar(1000);
DECLARE @phys_path nvarchar(1000);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @db_name nvarchar(1000);
SET @db_name = N'MY_DB_NAME';
SET @ParmDefinition = N'@phys_pathOUT varchar(1000) OUTPUT';
SET @sqlcmd = 'SELECT @phys_pathOUT = SUBSTRING(physical_name, 1, CHARINDEX('+CHAR(39)+'master.mdf'+CHAR(39)+', LOWER(physical_name)) - 1)
FROM master.sys.master_files
WHERE database_id = 1 AND file_id = 1'
DECLARE @CREATE_DATABASE_TEMPLATE VARCHAR(MAX);
SET @CREATE_DATABASE_TEMPLATE = 'CREATE DATABASE ['+ @db_name+ '] ON PRIMARY
( NAME = '+CHAR(39)+@db_name+'_Data'+CHAR(39)+', FILENAME = '+CHAR(39)+'{PHYSICAL_PATH}'+@db_name+'_Data.MDF'+CHAR(39)+' , SIZE = 11712KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%), FILEGROUP [ARCH]
( NAME = '+CHAR(39)+@db_name+'_Arch_Data'+CHAR(39)+', FILENAME = '+CHAR(39)+'{PHYSICAL_PATH}'+@db_name+'_Arch_Data.NDF'+CHAR(39)+' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 102400KB ) LOG ON
( NAME = '+CHAR(39)+@db_name+'_Log'+CHAR(39)+', FILENAME = '+CHAR(39)+'{PHYSICAL_PATH}'+@db_name+'_Log.LDF'+CHAR(39)+', SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)'
EXEC sp_executesql @sqlcmd, @ParmDefinition, @phys_pathOUT = @phys_path OUTPUT
SET @CREATE_DATABASE_TEMPLATE = REPLACE( @CREATE_DATABASE_TEMPLATE , '{PHYSICAL_PATH}', @phys_path )
EXECUTE( @CREATE_DATABASE_TEMPLATE ) -- creating database at existing physical path location
GO
要在数据库中创建“mysch”模式,请调用:
USE [MY_DB_NAME]
GO
CREATE SCHEMA mysch
GO