Estoy intentando construir un script de creación de base de datos de manera dinámica en un SQL Server 2012. Como "plantilla" he cogido el código que genera el Management con la opción "Create New Database" del menú contextual. Un script como este.
CREATE DATABASE [Prueba]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Prueba', FILENAME = N'E:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Prueba.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'Prueba_log', FILENAME = N'E:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Prueba_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
COLLATE Modern_Spanish_100_CI_AS
GO
ALTER DATABASE [Prueba] SET COMPATIBILITY_LEVEL = 110
GO
ALTER DATABASE [Prueba] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Prueba] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Prueba] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Prueba] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Prueba] SET ARITHABORT OFF
GO
ALTER DATABASE [Prueba] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Prueba] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Prueba] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [Prueba] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Prueba] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Prueba] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [Prueba] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Prueba] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Prueba] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Prueba] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Prueba] SET DISABLE_BROKER
GO
ALTER DATABASE [Prueba] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Prueba] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Prueba] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Prueba] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Prueba] SET READ_WRITE
GO
ALTER DATABASE [Prueba] SET RECOVERY FULL
GO
ALTER DATABASE [Prueba] SET MULTI_USER
GO
ALTER DATABASE [Prueba] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Prueba] SET TARGET_RECOVERY_TIME = 0 SECONDS
GO
USE [Prueba]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [Prueba] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO
A partir de este lo modifico para parametrizar el nombre de las base de datos, las rutas de los ficheros y el collation; y ahora es cuando viene el problema.
Cuanto intento hacer el execute sp_executesql @variablesql
me da errores de sintaxis cerca de los "GO"; he intentado añadirle retorno de carro y de línea antes y después de cada "GO" pero no hay manera y cuando he eliminado los "GO" del script me da el error "El nombre del archivo lógico "DBNAME" ya esta en uso" cuando en la ruta especificada no existe ningún fichero con el nombre de la BD.
La verdad es que ya no se me ocurre que más hacer. Gracias de antemano.