2011-03-31 131 views
5

我有簡單的代碼創建默認路徑數據庫

: CREATE DATABASE [asst] 
: ON (NAME = 'asst_dat', FILENAME = 'C:\data' , SIZE = 62, FILEGROWTH = 10%) 
: LOG ON (NAME = 'asst_log', FILENAME = 'C:\data' , SIZE = 146, FILEGROWTH = 10%) 

如何更改文件名參數,如C默認安裝路徑來創建數據庫:\ Program Files文件\ Microsoft SQL Server的\ MSSQL10_50.ATASSIST \ MSSQL 。那麼,爲什麼我需要它:因版本,從實例到SQL Server實例的這個位置不同

+0

的[創建使用默認路徑文件數據庫(http://stackoverflow.com/questions/1637628/create-database-using-file-in-default-path) – Castrohenge 2014-04-15 10:05:26

回答

1

您可以使用以下變量,分別包含你的根目錄下安裝和數據目錄:

SELECT @@basedir, @@datadir; 
+0

怎麼可能重複我能修改上面的代碼? – Eliazar 2011-03-31 11:40:17

+1

只需在您的FILENAME參數中包含'@@ datadir'。 – Wookai 2011-03-31 11:53:36

+1

:CREATE DATABASE [asst]:ON(NAME ='asst_dat',FILENAME = @@ datadir,SIZE = 62,FILEGROWTH = 10%):LOG ON(NAME ='asst_log',FILENAME = @@ datadir,SIZE = 146 ,FILEGROWTH = 10%) 這不起作用,錯誤的語法 – Eliazar 2011-03-31 12:02:43

0

假設你使用的是SQL Server 2005或2008,應該這樣做。雖然它可能比您預期的要長一些:-)您可以將代碼的主要部分放入函數中,並在您每次需要創建數據庫時調用它。

declare @instance_name nvarchar(200), 
     @system_instance_name nvarchar(200), 
     @registry_key nvarchar(512), 
     @path_data nvarchar(260), 
     @path_log nvarchar(260), 
     @value_name nvarchar(20), 
     @script nvarchar(4000); 

set @instance_name = coalesce(convert(nvarchar(20), serverproperty('InstanceName')), 'MSSQLSERVER'); 

exec master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instance_name, @system_instance_name output; 
set @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\MSSQLServer'; 

/* determine default location for data files */ 
exec master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', @registry_key, N'DefaultData', @path_data output; 
if @path_data is null 
begin 
    /* this is only executed if we are using the default instance */ 
    set @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\Setup'; 
    exec master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', @registry_key, N'SQLDataRoot', @path_data output; 
    set @path_data = @path_data + '\Data'; 
end; 

/* determine default location for log files */ 
exec master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', @registry_key, N'DefaultLog', @path_log output; 
if @path_log is null 
begin 
    /* this is only executed if we are using the default instance */ 
    set @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\Setup'; 
    exec master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', @registry_key, N'SQLDataRoot', @path_log output; 
    set @path_log = @path_log + '\Data'; 
end; 

set @script = 'CREATE DATABASE [asst] 
    ON (NAME = ''asst_dat'', FILENAME = ''' + @path_data + '\yourfile.mdf'' , SIZE = 62, FILEGROWTH = 10%) 
    LOG ON (NAME = ''asst_log'', FILENAME = ''' + @path_log + '\yourfile.ldf'' , SIZE = 146, FILEGROWTH = 10%);' 

exec(@script); 

您不能在CREATE DABASE語句中使用變量。這就是爲什麼你必須創建一個變量來保存命令並將其作爲腳本執行。

1

感謝,überjesus,我已經簡化你的代碼一點點

DECLARE @rows varchar(MAX), 
     @script nvarchar(MAX); 
SET @rows = (SELECT physical_name AS current_file_location 
FROM sys.master_files 
where name = 'master'); 
SET @rows = Replace(@rows, 'master.mdf', '') 
SELECT @rows; 
set @script = 'CREATE DATABASE [assist1] 
ON (NAME = ''asst_dat'', FILENAME = ''' + @rows + 'assist1.mdf'' , SIZE = 62, FILEGROWTH = 10%)  
LOG ON (NAME = ''asst_log'', FILENAME = ''' + @rows + 'assist1_log.ldf'' , SIZE = 146, FILEGROWTH = 10%);' 
exec(@script); 

感謝一個偉大的想法!

1

試試這個

您可以在不指定文件的詳細信息創建一個數據庫,如:

CREATE DATABASE DatabaseName; 
5

首先創建數據庫,然後根據需要更改文件屬性。

CREATE DATABASE [DBName] 
GO 

ALTER DATABASE [DBName] MODIFY FILE 
(NAME = N'DBName' , SIZE = 512MB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB) 
GO 

ALTER DATABASE [DBName] MODIFY FILE 
(NAME = N'DBName_log' , SIZE = 256MB , MAXSIZE = UNLIMITED , FILEGROWTH = 10%) 
GO