1

我正在尝试为基本数据库创建脚本并为 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 ?有更短的解决方案吗?

4

3 回答 3

3

GO 语句告诉查询分析器批处理已完成。

http://technet.microsoft.com/en-us/library/ms188037.aspx

因此,在执行动态代码时,设置的声明变量已超出范围。

如果您真的希望在事务中使用它,则 在BEGIN CATCH /END CATCH中使用BEGIN TRY/END TRY包装它,执行ROLLBACK

http://craftydba.com/?p=5930

我从来没有用 CREATE DATABASE 语句尝试过这个。这可能是一个有趣的练习。它会撤消数据库创建吗?一些东西可以添加到我的遗愿清单中尝试。

此外,在组合多个命令时,您需要使用分号 (;) 。否则,您将收到语法错误。

于 2013-11-13T13:40:06.453 回答
1

感谢RajVaibhav 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);
于 2013-11-13T10:53:28.093 回答
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
于 2013-11-13T10:32:14.493 回答