2008-09-23 53 views

回答

30

當您創建新數據庫時,指定位置。例如:

USE [master] 
GO 

    CREATE DATABASE [AdventureWorks] ON PRIMARY 
    (NAME = N'AdventureWorks_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf' , SIZE = 167872KB , MAXSIZE = UNLIMITED, FILEGROWTH = 16384KB) 
    LOG ON 
    (NAME = N'AdventureWorks_Log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 16384KB) 
    GO 
10

從SQL Server叢書,其中數據庫文件名都明確地定義一個例子:

USE master 
GO 
CREATE DATABASE Sales 
ON 
(NAME = Sales_dat, 
    FILENAME = 'c:\program files\microsoft sql server\mssql\data\saledat.mdf', 
    SIZE = 10, 
    MAXSIZE = 50, 
    FILEGROWTH = 5) 
LOG ON 
(NAME = 'Sales_log', 
    FILENAME = 'c:\program files\microsoft sql server\mssql\data\salelog.ldf', 
    SIZE = 5MB, 
    MAXSIZE = 25MB, 
    FILEGROWTH = 5MB) 
GO 
1
  1. 你的文件系統上創建文件夾:d:\ TEMP \ dbFolder \
  2. 運行腳本:

    USE master; 
    GO 
    CREATE DATABASE TestDB1 
    ON  (NAME = Sales_dat, FILENAME = 'D:\temp\dbFolder\TestDB1.mdf') 
    LOG ON (NAME = Sales_log, FILENAME = 'D:\temp\dbFolder\TestDB1.ldf'); 
    GO 
    
0

請參閱此鏈接:CREATE DATABASE (Transact-SQL)

CREATE DATABASE [ADestinyDb] CONTAINMENT = NONE ON PRIMARY 
(NAME = N'ADestinyDb', 
    FILENAME = N'D:\temp\dbFolder\ADestinyDb.mdf' , 
    SIZE = 3136 KB , MAXSIZE = UNLIMITED, 
    FILEGROWTH = 1024 KB) 
LOG ON 
(NAME = N'ADestinyDb_log', 
    FILENAME = N'D:\temp\dbFolder\_log.ldf' , 
    SIZE = 832KB , MAXSIZE = 2048 GB , FILEGROWTH = 10 %) 
0

你的文件系統上創建文件夾:d:\ TEMP \ dbFolder \,並運行下面的腳本 (嘗試 '山' 登錄)

USE master 
CREATE DATABASE [faltu] ON PRIMARY 
(NAME = N'faltu', FILENAME = N'D:\temp\dbFolder\faltu.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB) 
LOG ON 
(NAME = N'faltu_log', FILENAME = N'D:\temp\dbFolder\faltu_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%) 
2

使用Studio Manager中的變量擴展了前面的例子。

創建文件夾和子文件夾。
示例:根文件夾E:\ MSSQL \ DATA 子文件夾E:\ MSSQL \ DATA \ DB和E:\ MSSQL \ DATA \ Logs。

MKDIR "E:\MSSQL\DATA\DB" 
MKDIR "E:\MSSQL\DATA\Logs" 

更改數據庫名稱@DBNAME變量@Test_DB」你 'DesiredName_DB'

更改根文件夾路徑@DataPath 'E:\ MSSQL \ DATA' 你按照上面創建的文件夾。

在工作室經理

DECLARE @DBNAME VARCHAR(MAX) 
DECLARE @DataPath AS NVARCHAR(MAX) 
DECLARE @sql VARCHAR(MAX) 

SET @DBNAME = N'Test_DB' 
SET @DataPath = N'E:\MSSQL\DATA' 

SELECT @sql = 'USE MASTER' 
EXEC (@sql) 

SELECT @sql = 'CREATE DATABASE '+ quotename(@DBNAME) + ' 
ON 
PRIMARY 
( 
NAME = ''' + @DBNAME + '_DB'', 
FILENAME = ''' + @DataPath + '\DB\' + @DBNAME + '.mdf'', 
SIZE = 3136 KB , MAXSIZE = UNLIMITED, 
FILEGROWTH = 1024 KB 
) 
LOG ON 
(
NAME = '''+ @DBNAME + '_Log'', 
FILENAME = '''+ @DataPath + '\Logs\' + @DBNAME + '_log.ldf'', 
SIZE = 832KB , MAXSIZE = 2048 GB , FILEGROWTH = 10 % 
)' 


EXEC (@sql) 

或者以上一個變運行下面的。

DECLARE @DBNAME VARCHAR(MAX) 
DECLARE @DataFilePath AS NVARCHAR(MAX) 
DECLARE @LogFilePath AS NVARCHAR(MAX) 
DECLARE @sql VARCHAR(MAX) 

SET @DBNAME = N'Test_DB' 
SET @DataFilePath = N'E:\MSSQL\DATA\DB\' 
SET @LogFilePath = N'E:\MSSQL\DATA\Logs\' 

SELECT @sql = 'USE MASTER' 
EXEC (@sql) 

SELECT @sql = 'CREATE DATABASE '+ quotename(@DBNAME) + ' 
ON 
PRIMARY 
( 
NAME = ''' + @DBNAME + '_DB'', 
FILENAME = ''' + @DataFilePath + @DBNAME + '.mdf'', 
SIZE = 3136 KB , MAXSIZE = UNLIMITED, 
FILEGROWTH = 1024 KB 
) 
LOG ON 
(
NAME = '''+ @DBNAME + '_Log'', 
FILENAME = '''+ @LogFilePath+ @DBNAME + '_log.ldf'', 
SIZE = 832KB , MAXSIZE = 2048 GB , FILEGROWTH = 10 % 
)' 


EXEC (@sql)