Alter DB modify log file.

  • I can run this command to make changes for 1 DB USE [master]

    GO

    ALTER DATABASE [DBName] MODIFY FILE ( NAME = N'Name_log', FILEGROWTH = 10000KB )

    GO

    Is it possible to create a script which changes log file size to let's say 100MB for all DBs in all servers instead of running the above command by logging into each server? We have about 200 servers and close to 3000 DBs.

  • You can run the following with the results to text and it will generate the statements for you.

    SELECT 'ALTER DATABASE ['+d.name+'] MODIFY FILE (NAME=N'''+mf.name+''', FILEGROWTH = 10000KB)'+CHAR(13)+'GO'

    FROM sys.master_files mf

    JOIN sys.databases d

    ON mf.database_id=d.database_id

    WHERE mf.type=1

    AND d.database_id>4

  • Chitown (11/6/2015)


    Is it possible to create a script which changes log file size to let's say 100MB for all DBs in all servers instead of running the above command by logging into each server?

    How do you imagine running a script without connecting to the server? Telepathically over the air? 🙂

    In the Registered Servers window (View -> Registered Servers) you can create a New Server Group, then add all the 200 servers into the group. Then select that group and click New Query. It will open a new window and connect automatically to all the servers. So any query you run in this window will be executed on all the servers.


    Alex Suprun

  • JeremyE (11/6/2015)


    You can run the following with the results to text and it will generate the statements for you.

    SELECT 'ALTER DATABASE ['+d.name+'] MODIFY FILE (NAME=N'''+mf.name+''', FILEGROWTH = 10000KB)'+CHAR(13)+'GO'

    FROM sys.master_files mf

    JOIN sys.databases d

    ON mf.database_id=d.database_id

    WHERE mf.type=1

    AND d.database_id>4

  • the script provided just generates the commands, soy uo can review them easily.

    you'd want to run the results of the commands, or stick them into a variable as a stack of commands via FOR XML and EXECUTE(@sqlcommand)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Jeremy's script wont alter any database,

    you need to select the out put of the query and run it in another query window.

    You can develop dynamic query on top of this and if you know the list of servers, you can try generating sqlcmd command for each server from the list.

    ----------------------------------------------------------------------------------------------------------------------------------------------------
    Roshan Joe

    Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • So I ran the above script from the central Management servers and inserted all the data into a temp table.

    CREATE TABLE #TEST

    (SQLQUERY VARCHAR (1000))

    INSERT INTO #TEST SELECT 'ALTER DATABASE ['+d.name+'] MODIFY FILE (NAME=N'''+mf.name+''', FILEGROWTH = 102400KB)'+CHAR(13)+'GO'

    FROM sys.master_files mf

    JOIN sys.databases d

    ON mf.database_id=d.database_id

    WHERE mf.type IN (1) AND d.database_id>4

    Now I have the server name column and sqlquery column.

    Now, I was thinking maybe using the cursor to execute the alter statement.

  • Update:

    Still stuck.

    declare @sqlcommand nvarchar (500)

    declare CRS cursor

    for

    select command from #test

    open CRS

    FETCH NEXT FROM CRS

    WHILE @@FETCH_STATUS = 0

    begin

    set @sqlcommand = N'command from #test'

    exec SP_EXECUTESQL @sqlcommand

    END

    deallocate CRS

  • You almost got it right:

    declare @sqlcommand nvarchar (500)

    declare CRS cursor

    for

    select command from #test

    open CRS

    FETCH NEXT FROM CRS INTO @sqlcommand

    WHILE @@FETCH_STATUS = 0

    begin

    -- PRINT @sqlcommand

    exec SP_EXECUTESQL @sqlcommand

    FETCH NEXT FROM CRS INTO @sqlcommand

    END

    deallocate CRS


    Alex Suprun

  • ALTER DATABASE [SSISDB] MODIFY FILE (NAME=N'log', FILEGROWTH = 66000KB)

    GO;

    ALTER DATABASE [PRODUCT_DB_SSIS_SOURCE] MODIFY FILE (NAME=N'PRODUCT_DB_SSIS_SOURCE_log', FILEGROWTH = 66000KB)

    GO;

    So when I run this declare @sqlcommand nvarchar (500)

    declare CRS cursor

    for

    select command from #test

    open CRS

    FETCH NEXT FROM CRS INTO @sqlcommand

    WHILE @@FETCH_STATUS = 0

    begin

    -- PRINT @sqlcommand

    exec SP_EXECUTESQL @sqlcommand

    FETCH NEXT FROM CRS INTO @sqlcommand

    END

    deallocate CRS I get this error when query #1 gets executed. "Incorrect syntax near 'GO'."

  • Finally :-):-)

    drop table #test

    create table #test (command varchar (500))

    insert into #test SELECT 'USE [MASTER] ' + CHAR(13) + 'ALTER DATABASE [' + d.name + N'] '

    + 'MODIFY FILE (NAME = N''' + mf.name + N''', FILEGROWTH = 66000KB)'

    + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)

    FROM sys.master_files mf

    JOIN sys.databases d ON mf.database_id = d.database_id

    WHERE d.database_id > 4

    AND d.state_desc <> 'offline'

    AND mf.type_desc = 'LOG';

    select * from #test

    declare @sqlcommand nvarchar (500)

    declare CRS cursor

    for

    select command from #test

    open CRS

    FETCH NEXT FROM CRS INTO @sqlcommand

    WHILE @@FETCH_STATUS = 0

    begin

    -- PRINT @sqlcommand

    exec SP_EXECUTESQL @sqlcommand

    FETCH NEXT FROM CRS INTO @sqlcommand

    END

    deallocate CRS

  • Remove GO. It's a batch separator for SSMS, not a command.


    Alex Suprun

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply