Home Forums SQL Server 2012 SQL 2012 - General AlwaysOn: automatically add new databases to SQL 2012 AlwaysOn by script/T-SQL or PowerShell RE: AlwaysOn: automatically add new databases to SQL 2012 AlwaysOn by script/T-SQL or PowerShell

  • AllenValk66 (4/26/2013)


    If you run this script manually in SSMS, be sure to run the command “DROP TABLE dbo.#dbs” first, otherwise it won’t run manually in SSMS.[/Quote]

    the temporary table should be disposed once your session has been closed, but just in case add this to your script just before the create table

    IF (OBJECT_ID('tempdb..#dbs')) IS NOT NULL

    BEGIN

    DROP TABLE #dbs

    END

    create table #dbs(a int primary key identity, dbname varchar(100))

    declare @nextdb varchar(100)

    declare @restorestring varchar(400)

    AllenValk66 (4/26/2013)


    It will run perfectly as a SQL Server Agent job, however. (not sure why that is!)

    Since at that point it will be running under a different account that is why it succeeds.

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉