如何获取SQL Server 2005中事务日志的逻辑名称
我正在尝试编写一个 T-SQL 例程,根据数据库的逻辑名称使用 DBCC SHRINKFILE 缩小事务日志文件。 DB_NAME()
函数为您提供数据库的逻辑名称。事务日志有等效的吗?如果没有,是否有其他方法可以获取此信息?事务日志的默认名称是<<数据库名称>>_log
,但我不想依赖于此。
I am trying to write a T-SQL routine that shrink the transaction log file using DBCC SHRINKFILE based on the logical name of the database. The DB_NAME()
function gives you the logical name of the database. Is there an equivalent one for the transaction log? If not, is there some other way to get this information? The default name for the transaction logs is <<Database Name>>_log
, but I would rather not rely on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用:
对于任何database_id,日志文件的type = 1,并且所有数据库的所有文件都可以在sys.master_files中找到。
编辑:
我应该指出,您不应该定期缩小日志。您的事务日志的大小应该适当,以防止其增长,然后保持该大小。事务日志不能即时文件初始化,并且在添加空间时必须清零,这是一个缓慢的顺序操作,会降低性能。
You can use:
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.
假设一个标准数据库(例如只有一个日志文件),日志文件始终为 file_id = 2。即使您有多个数据文件(对于 NDF,id = 3+),这一点也适用。
DBCC 也采用文件 ID。因此,
DBCC SHRINKFILE (2...)
将始终有效。您无法在 DBCC 内部进行参数化,因此这可以避免动态 SQL。如果您需要该名称,请使用FILE_NAME(2)。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).生成,
SqlServer 2012
Generates,
SqlServer 2012
--或者简单地说
--OR simply