2011-03-31 18 views
5

que tienen código simpleCREAR BASE DE DATOS en la ruta predeterminada

: 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ómo puedo cambiar el parámetro FILENAME para crear base de datos en la ruta de instalación predeterminada como C: \ Archivos de programa \ Microsoft SQL Server \ MSSQL10_50.ATASSIST \ MSSQL . Así que por qué lo necesito: a partir de una versión a otra, de una instancia a instancia de SQL Server esta ubicación difiere

+0

posible duplicado de [CREATE DATABASE utilizando el archivo de ruta predeterminada] (http://stackoverflow.com/questions/1637628/create-database-using-file-in-default-path) – Castrohenge

Respuesta

1

Puede utilizar las siguientes variables, que contienen, respectivamente, el directorio raíz de la instalación y el directorio de datos:

SELECT @@basedir, @@datadir; 
+0

y cómo puedo modificar el código anterior? – Eliazar

+1

Simplemente incluya '@@ datadir' en su parámetro FILENAME. – Wookai

+1

: CREATE DATABASE [asst]: ON (NAME = 'asst_dat', FILENAME = @@ datadir, SIZE = 62, FILEGROWTH = 10%): LOG ON (NOMBRE = 'asst_log', FILENAME = @@ datadir, SIZE = 146 , FILEGROWTH = 10%) Esto no funcionó, la sintaxis incorrecta – Eliazar

0

Suponiendo que está utilizando SQL Server 2005 o 2008, debería hacerlo. Aunque es probable que sea un poco más largo de lo esperado :-) Podría poner la parte principal del código en una función y llamarla cada vez que necesite crear una base de datos.

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); 

No puede usar variables en la instrucción CREATE DABASE. Es por eso que debe crear una variable que contenga el comando y ejecutarlo como un script.

1

Gracias, überjesus, he simplificado el código un poco

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); 

Gracias por una gran idea!

1

probar esto

Puede crear una base de datos sin especificar los detalles del archivo, como:

CREATE DATABASE DatabaseName; 
5

En primer lugar crear la base de datos y luego altera las propiedades del archivo, según sea necesario.

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 
Cuestiones relacionadas