We are connecting to a database in C# and then running a few sql scripts on it.
I need to be able to shrink the current database without specifying the name.
We do not have the database name in the program we are just given the connection and running the scripts.
This is what I started with:
ALTER DATABASE SSSIndexes SET RECOVERY SIMPLE WITH NO_WAIT
GO
DBCC SHRINKFILE(N'SSSIndexes_Log', 1) <-- my issue is here
GO
But I won't know the database name or the log file name.
Can it be done?
You can get the current database and shrink it by calling:
DECLARE @dbName VARCHAR(50)
SELECT @dbName = DB_NAME()
DBCC SHRINKDATABASE(@dbName)
To do just the log file:
DECLARE @logName VARCHAR(50)
SELECT @logName = name FROM sys.master_files WHERE database_id = db_id() AND type = 1
DBCC SHRINKFILE(@logName, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With