How to execute script oin all databases of an instance.

  • Hi,

    I would like to execute a 100 lines script in all the user databases of a sql server instance.

    Can someone suggest how to do it ?

    Thanks in advance.

    Smith.

  • Powershell is an option.

    or

    exec sp_msforeachdb 'USE ?; SELECT ?;'

    Personally, I'd consider writing a generic powershell script which took a .sql file as parameter.

  • I do not want to use sp_msforechdb because its a long script.

    Also it may be necessary to execute that in a few speceific databaseas in future.

    I tried using cursor by saving the required databases in a temptable browsing through each tables.

    Pbm is it's actualy not changing the database. 'use '+@dbname doesn't work.

    Thanks.

  • Well, if Powershell doesn't work for you, you could consider SQLCMD (either like this or like this), or SSIS.

    John

Viewing 4 posts - 1 through 3 (of 3 total)

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