How to get the logical name of the transaction log in SQL Server 2005

孤人 提交于 2019-12-04 00:02:15

You can use:

SELECT name
FROM sys.master_files
WHERE database_id = db_id()
  AND type = 1

Log files have type = 1 for any database_id and all files for all databases can be found in sys.master_files.

EDIT:

I should point out that you shouldn't be shrinking your log on a routine basis. Your transaction log should be sized appropriately to keep it from ever having to grow, and then left at that size. The transaction log can not be instant file initialized and has to be zero'd out when space is added to it, which is a slow sequential operation that degrades performance.

Assuming a standard database (eg only one log file), the log file is always file_id = 2. This applies even if you have multiple data files (id = 3+ for NDFs).

The DBCC also takes the file id too. So, DBCC SHRINKFILE (2...) will always work. You can't parameterise inside the DBCC so this avoids dynanmic SQL. If you want the name, use FILE_NAME(2).

select Name
from sys.database_files

Generates,

SomeDb_Data  
SomeDb_Log

SqlServer 2012

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!