我正在尝试为SqlServer创建基本数据库和登录/用户创建的脚本 当我运行我的脚本时,我收到许多错误,如:
必须声明标量变量“@varname”
在脚本执行期间第二次在EXEC
语句中使用变量时。
declare
@dbname nvarchar(50) ,
@userName nvarchar(50) ,
@password nvarchar(50);
SET @dbname = N'DBNAME';
SET @userName = N'DBUSERNAME';
SET @password= N'PASSWORD';
Begin transaction
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = @dbname)
BEGIN
exec ('
DROP login '+ @userName + '
DROP USER '+ @userName + '
alter database '+ @dbname + '
SET OFFLINE
WITH ROLLBACK IMMEDIATE;
ALTER DATABASE '+ @dbname + '
SET ONLINE;
DROP DATABASE '+ @dbname)
END
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
EXEC ('CREATE DATABASE '+ @dbname)
GO
EXEC (@dbname+ '.[dbo].[sp_fulltext_database] @action = ''disable''')
GO
EXEC ('ALTER DATABASE '+ @dbname + ' SET COMPATIBILITY_LEVEL = 100')
GO
...
EXEC ('ALTER DATABASE '+ @dbname + ' SET DB_CHAINING OFF')
GO
EXEC('create login '+ @userName + 'with password = '+ @password);
EXEC('use '+ @dbname+ '
create user '+ @userName + ' for login '+ @userName);
EXEC ('EXEC sp_addrolemember ''db_owner'', '+ @userName)
commit transaction
我是否要为每个查询使用sp_executesql?是否有更短的解决方案?
答案 0 :(得分:3)
GO语句告诉查询分析器批处理完成。
http://technet.microsoft.com/en-us/library/ms188037.aspx
因此,在动态代码执行时,设置的声明变量超出范围。
如果您真的想在交易中使用此功能,请在 BEGIN CATCH / END CATCH 中使用 BEGIN TRY / END TRY 进行包装,执行 ROLLBACK < /强>
我从未尝试使用CREATE DATABASE语句。这可能是一个有趣的练习。它是否撤消数据库创建?要添加到我的清单列表中的东西可以尝试。
此外,在组合多个命令时,您需要使用分号(;)。否则,您将收到语法错误。
答案 1 :(得分:1)
感谢Raj和Vaibhav Parmar条评论,我更改了我的脚本,现在工作正常。
主要错误是应该删除的GO语句,因为它 用于将SQL命令分组为发送到的批处理 服务器一起。批处理中包含的命令,即集合 自上一个GO命令或会话开始以来的命令, 必须在逻辑上一致。您无法在一个批次中定义变量 因为变量的范围有限,所以在另一个中使用它 到它定义的批次。
第二个错误是我尝试使用transaction with statement 不允许他们的
我将更正后的脚本留在那里作为参考:
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);
答案 2 :(得分:0)
使用下面的代码可能会有所帮助:
declare @dbname nvarchar(50) ,@userName nvarchar(50) ,@password nvarchar(50);
SET @dbname = N'DBNAME';
SET @userName = N'DBUSERNAME';
SET @password= N'PASSWORD';
Begin transaction
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = @dbname)
BEGIN
exec ('
DROP login '+ @userName + '
DROP USER '+ @userName + '
alter database '+ @dbname + '
SET OFFLINE
WITH ROLLBACK IMMEDIATE;
ALTER DATABASE '+ @dbname + '
SET ONLINE;
DROP DATABASE '+ @dbname)
END
else
begin
--SET ANSI_NULLS ON
--GO
SET QUOTED_IDENTIFIER ON
--GO
--SET ANSI_PADDING ON
--GO
EXEC ('CREATE DATABASE '+ @dbname)
--GO
EXEC (@dbname+ '.[dbo].[sp_fulltext_database] @action = ''disable''')
--GO
EXEC ('ALTER DATABASE '+ @dbname + ' SET COMPATIBILITY_LEVEL = 100')
--GO
EXEC ('ALTER DATABASE '+ @dbname + ' SET DB_CHAINING OFF')
--GO
EXEC('create login '+ @userName + 'with password = '+ @password);
EXEC('use '+ @dbname+ '
create user '+ @userName + ' for login '+ @userName);
EXEC ('EXEC sp_addrolemember ''db_owner'', '+ @userName)
end
commit transaction