Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can a shrink be performed on the current database without specifying the database name

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?

like image 950
ErocM Avatar asked Jan 14 '23 01:01

ErocM


1 Answers

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)
like image 107
Adam Plocher Avatar answered Jan 19 '23 10:01

Adam Plocher