For each database-sp_MSForEachDB

Source: Internet
Author: User

Sp_helptext 'SP _ msforeachdb'

Declare @ ssql nvarchar (4000)
Set @ ssql ='
If ''? ''Not in (''tempdb'', ''master'', ''model'', ''msdb'') begin
Use [?]
Declare @ tsql nvarchar (4000) set @ tsql = ''''
Declare @ iLogFile int
Declare @ sLogFileName varchar (55)
Declare @ RecoveryModel nvarchar (10)
Declare LogFiles cursor
Select fileid from sysfiles where status & 0x40 = 0x40
Open LogFiles
Fetch next from LogFiles into @ iLogFile
While @ fetch_status = 0
Begin
Set @ tsql = @ tsql + ''dbcc SHRINKFILE (''+ cast (@ iLogFile as varchar (5) +'', 10 )''
Fetch next from LogFiles into @ iLogFile
End
Select @ RecoveryModel = CONVERT (nvarchar (10), Databasepropertyex (name, ''recovery '') from master .. sysdatabases where name = ''? ''
Set @ tsql = ''USE [?]; ''+ @ Tsql +'' alter database [?] Set recovery simple ''+ @ tsql
Set @ tsql = @ tsql + ''; alter database [?] Set recovery ''+ @ RecoveryModel + '';''
-- Print @ tsql -- for debugging
Exec (@ tsql)
Close LogFiles
DEALLOCATE LogFiles
End'

Exec sp_msforeachdb @ ssql



Http://www.sqllion.com/2010/08/for-each-database-sp_msforeachdb/

Http://codesnippets.joyent.com/posts/show/665

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.