如何使用EXEC()语句创建SQL数据库

时间:2014-08-27 13:08:45

标签: sql sql-server database

需要在其中创建文件夹和数据库。由于无法使用变量名称和路径创建数据库,因此我尝试使用EXEC()语句。但它失败并出现错误''LOG''附近的语法不正确。我做错了什么?

USE Master;
GO
SET NOCOUNT ON

-- 1 - Variable declaration
DECLARE @DBName1 sysname
DECLARE @DataPath1 nvarchar(500),
DECLARE @LogPath1 nvarchar(500)
DECLARE @DirTree TABLE (subdirectory nvarchar(255), depth INT)
DECLARE @version sysname
declare @sql nvarchar(500)
declare @fulldbpath varchar(500)
declare @fulllogpath varchar(500)

-- 2 - Initialize variables
SET @version= 
      CASE SUBSTRING(CONVERT(VARCHAR(50), SERVERPROPERTY('productversion')), 1, 4)
        WHEN '9.00' THEN '2005'
        WHEN '10.0' THEN '2008'
        WHEN '10.5' THEN '2008R2'
        WHEN '11.0' THEN '2012'
        WHEN '12.0' THEN '2014'
    END
SET @DBName1 = 'DB1'
SET @DataPath1 = 'G:\StorageGroup1\SQL' + @version+ '\'+ @DBName1 
SET @LogPath1 = 'F:\StorageGroup1\SQL\' + @version+ '\'+ @DBName1
SET @fulldbpath=@DataPath1+'\'+@DBName1+'.mdf'
SET @fulllogpath=@LogPath1+'\'+@DBName1+'_log.ldf'

-- 3 - @DataPath values
INSERT INTO @DirTree(subdirectory, depth)
EXEC master.sys.xp_dirtree @DataPath1

-- 4 - Create the @DataPath directory
IF NOT EXISTS (SELECT 1 FROM @DirTree WHERE subdirectory = @DBName1)
EXEC master.dbo.xp_create_subdir @DataPath1

-- 5 - Remove all records from @DirTree
DELETE FROM @DirTree

-- 6 - @LogPath values
INSERT INTO @DirTree(subdirectory, depth)
EXEC master.sys.xp_dirtree @LogPath1

-- 7 - Create the @LogPath directory
IF NOT EXISTS (SELECT 1 FROM @DirTree WHERE subdirectory = @DBName1)
EXEC master.dbo.xp_create_subdir @LogPath1


--8 -Create database
  set @sql = 'CREATE DATABASE [@DBName1]
    ON (NAME = [@DBName1],
        FILENAME = [@fulldbpath]
    LOG ON (NAME = [@DBName1]
        FILENAME = [@fulllogpath]
            )'                      
    EXEC(@sql)          


SET NOCOUNT OFF
GO

2 个答案:

答案 0 :(得分:1)

您的CREATE DATABASE声明是:

CREATE DATABASE [@DBName1]
    ON (NAME = [@DBName1],
        FILENAME = [@fulldbpath]
    LOG ON (NAME = [@DBName1]
        FILENAME = [@fulllogpath]
            )

[@fulldbpath]之后你错过了一个右括号:

set @SQL = ' CREATE DATABASE QUOTENAME(' + @DBName1 + ')
        ON (NAME = QUOTENAME(' + @DBName1 + '_data),
            FILENAME = QUOTENAME(' + @fulldbpath + '))
        LOG ON (NAME = QUOTENAME(' + @DBName1 + '_log)
            FILENAME = QUOTENAME(' + @fulllogpath + '))'

此外,请在声明@DataPath1

后删除逗号

答案 1 :(得分:0)

这应该有用(当我尝试的时候会这样做):

USE MASTER;
GO
SET NOCOUNT ON

-- 1 - Variable declaration
DECLARE @DBName1 sysname
DECLARE @DataPath1 nvarchar(500)
DECLARE @LogPath1 nvarchar(500)
DECLARE @DirTree TABLE (subdirectory nvarchar(255), depth INT)
DECLARE @version sysname
declare @sql nvarchar(500)
declare @fulldbpath varchar(500)
declare @fulllogpath varchar(500)

-- 2 - Initialize variables
SET @version= 
      CASE SUBSTRING(CONVERT(VARCHAR(50), SERVERPROPERTY('productversion')), 1, 4)
        WHEN '9.00' THEN '2005'
        WHEN '10.0' THEN '2008'
        WHEN '10.5' THEN '2008R2'
        WHEN '11.0' THEN '2012'
        WHEN '12.0' THEN '2014'
    END
SET @DBName1 = 'DB1'
SET @DataPath1 = 'G:\StorageGroup1\SQL' + @version+ '\'+ @DBName1 
SET @LogPath1 = 'F:\StorageGroup1\SQL\' + @version+ '\'+ @DBName1    
SET @fulldbpath=@DataPath1+'\'+@DBName1+'.mdf'
SET @fulllogpath=@LogPath1+'\'+@DBName1+'_log.ldf'

-- 3 - @DataPath values
INSERT INTO @DirTree(subdirectory, depth)
EXEC master.sys.xp_dirtree @DataPath1

-- 4 - Create the @DataPath directory
IF NOT EXISTS (SELECT 1 FROM @DirTree WHERE subdirectory = @DBName1)
EXEC master.dbo.xp_create_subdir @DataPath1

-- 5 - Remove all records from @DirTree
DELETE FROM @DirTree

-- 6 - @LogPath values
INSERT INTO @DirTree(subdirectory, depth)
EXEC master.sys.xp_dirtree @LogPath1

-- 7 - Create the @LogPath directory
IF NOT EXISTS (SELECT 1 FROM @DirTree WHERE subdirectory = @DBName1)
EXEC master.dbo.xp_create_subdir @LogPath1


--8 -Create database
select @fulldbpath
select @fulllogpath
set @sql = 'CREATE DATABASE ['+@DBName1+']
    ON (NAME = ['+@DBName1+'],
        FILENAME = ['+@fulldbpath+'])
    LOG ON (NAME = ['+@DBName1+'_log],
        FILENAME = ['+@fulllogpath+']
            )'                      
  EXEC(@sql)          

    print @sql


SET NOCOUNT OFF
GO

声明部分中有一个剩余的逗号,@ sql中缺少一些逗号。此外,日志和数据库具有相同的名称 - 需要区别(我添加_log作为日志的后缀),并且需要转义的变量,否则他们不会得到扩展。