感谢Raj和Vaibhav Parmar的评论,我更改了我的脚本,现在它运行良好。
主要错误是应该删除的 GO 语句,因为它用于将 SQL 命令分组到一起发送到服务器的批处理。批处理中包含的命令,即自上一个 GO 命令或会话开始以来的命令集,必须在逻辑上保持一致。您不能在一个批次中定义一个变量,然后在另一个批次中使用它,因为变量的范围仅限于定义它的批次。
第二个错误是我试图将事务与不允许它们的语句一起使用
我将更正后的脚本留在那里作为参考:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
declare
@dbname nvarchar(50) ,
@userName nvarchar(50) ,
@password nvarchar(50);
SET @dbname = N'DATABASENAME';
SET @userName = N'DBUSERNAME';
SET @password= N'DBUSERPASSWORD';
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = @dbname)
BEGIN
exec ('
DROP USER '+ @userName + '
DROP login '+ @userName + '
alter database '+ @dbname + '
SET OFFLINE
WITH ROLLBACK IMMEDIATE;
ALTER DATABASE '+ @dbname + '
SET ONLINE;
DROP DATABASE '+ @dbname)
END
EXEC ('CREATE DATABASE '+ @dbname);
EXEC (@dbname+ '.[dbo].[sp_fulltext_database] @action = ''disable''');
EXEC ('ALTER DATABASE '+ @dbname + ' SET COMPATIBILITY_LEVEL = 100');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ANSI_NULL_DEFAULT OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ANSI_NULLS ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ANSI_PADDING ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET AUTO_CLOSE OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET AUTO_SHRINK OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET QUOTED_IDENTIFIER ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET RECOVERY FULL');
EXEC ('ALTER DATABASE '+ @dbname + ' SET PAGE_VERIFY CHECKSUM');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ANSI_WARNINGS ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ARITHABORT ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET AUTO_CREATE_STATISTICS ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET AUTO_UPDATE_STATISTICS ON');
EXEC ('ALTER DATABASE '+ @dbname + ' SET CURSOR_CLOSE_ON_COMMIT OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET CURSOR_DEFAULT GLOBAL');
EXEC ('ALTER DATABASE '+ @dbname + ' SET CONCAT_NULL_YIELDS_NULL OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET NUMERIC_ROUNDABORT OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET RECURSIVE_TRIGGERS OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ENABLE_BROKER');
EXEC ('ALTER DATABASE '+ @dbname + ' SET AUTO_UPDATE_STATISTICS_ASYNC OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET DATE_CORRELATION_OPTIMIZATION OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET TRUSTWORTHY OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET ALLOW_SNAPSHOT_ISOLATION OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET PARAMETERIZATION SIMPLE');
EXEC ('ALTER DATABASE '+ @dbname + ' SET READ_COMMITTED_SNAPSHOT OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET HONOR_BROKER_PRIORITY OFF');
EXEC ('ALTER DATABASE '+ @dbname + ' SET READ_WRITE');
EXEC ('ALTER DATABASE '+ @dbname + ' SET MULTI_USER');
EXEC ('ALTER DATABASE '+ @dbname + ' SET DB_CHAINING OFF');
EXEC ('create login '+ @userName + ' with password = '''+ @password+ ''', default_database = ' + @dbname);
EXEC ('use '+ @dbname+ ' create user '+ @userName + ' for login '+ @userName);
EXEC ('use '+ @dbname+ ' EXEC sp_addrolemember ''db_owner'', '+ @userName);