我有简单的代码
: 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%)
如何更改FILENAME参数以在默认安装路径中创建数据库,如C:\ Program Files \ Microsoft SQL Server \ MSSQL10_50.ATASSIST \ MSSQL。那么为什么我需要它:从版本到版本,从实例到SQL Server实例这个位置不同
答案 0 :(得分: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
答案 1 :(得分:1)
您可以使用以下变量,分别包含安装的根目录和数据目录:
SELECT @@basedir, @@datadir;
答案 2 :(得分: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);
感谢您的好主意!
答案 3 :(得分:1)
试试这个
您可以在不指定文件详细信息的情况下创建数据库,例如:
CREATE DATABASE DatabaseName;
答案 4 :(得分: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
语句中使用变量。这就是为什么你必须创建一个保存命令的变量并将其作为脚本执行的原因。