Initial size is not just 3MB, it is taken from the model database (if not specified during the creation of your user database.) So assuming you haven't specified a initial size during the creation of your user db and you haven't altered the model database file sizes after you have created your userdb you can do the following:
--Create testDB
CREATE DATABASE [TEST_100]
GO
--grow your database file size
ALTER DATABASE [Test] MODIFY FILE ( NAME = N'Test', SIZE = 100MB )
GO
--switch context
USE [TEST_100]
GO
--Find size of modelDB mdf file, this is your initial file size used for the userdb
DECLARE @TargetFileSize int
SELECT @TargetFileSize = (size * 8 / 1024)
FROM sys.master_files
WHERE database_id = 3 --model database
AND file_id= 1 --first file is mdf, assuming you have a model with just one mdf. If you have multiple files, change for the one you need to find.
--shrink the the first file of your current database to the target size that you just found.
DBCC SHRINKFILE (1,@TargetFileSize)
EDIT
Okay, some extra info is needed after your edits and comments.
First of all. I feel that the "Initial size" label that you see when you look at the file properties in SSMS is a misnomer. Basically, your intial size is just a concept. It's the first size that is used during creation of the database. You can either explicitly specify this in the CREATE DATABASE
statement, or you can have SQL Server implicitly copy it from the model database by ommiting that information during creation.
However, once the database is created, from a DBA perpective there is no such thing as a "initial size" there is only one property visible for a DBA and that is: the actual size. Even the "Initial size" property in SSMS just shows actual size, not the initial size.
Well how come that DBCC SHRINKFILE
or DBCC SHRINKDATABASE
"know" the initial size then? Even after you have changed the size. Interesting question.
The first page of a dattabase file is the file header page. In there you have, amongst others, 2 properties: size and minsize.
At creation of the file, both file header properties get filled with the inital value:
DBCC TRACEON(3604)
--parameters for DBCC PAGE: (Dbname, fileID, pageID, outputTypeID)
DBCC PAGE('Test_100',1,0,3)with tableresults
Both sizes are in the amount of data pages. In this case. 288 data pages.
Now if I alter the file size:
ALTER DATABASE [test_100] MODIFY FILE ( NAME = N'test', SIZE = 50MB )
You can see that the "size" property is changed to reflect the new size. However, the "MinSize" property still contains the "Initial" size. It's the minimal size to which the shrink command will go.
However, having said all this. I still don't understand why you want to complicate things by first altering the initial size and then shrink to that initial size. Instead of just shrinking directly to a targetsize.
Anyway, to answer your question. The "initial" size is not exposed as a property to the user/dba.
Is there a particular reason you are specifying TRUNCATEONLY
? As per the documentation on DBCC SHRINKFILE
:
TRUNCATEONLY
Releases all free space at the end of the file to the operating system
but does not perform any page movement inside the file. The data file
is shrunk only to the last allocated extent.
target_size is ignored if specified with TRUNCATEONLY.
If that is not the intended behavior you were looking for (which it doesn't sound like it is), then you could try without that option set.
Also, there are other factors that hinder database file shrinking as well, such as text
`ntext\
image` BLOB data. So it is worth looking into the possibility of the existence of data with these data types.
Best Answer
As others have said, shrinking a database in this situation is not at all inappropriate.
What the experts mean when they say things like "Stop Shrinking Your Database Files. Seriously. Now." is that you don't want to shrink them just because they are a little bit bigger than you expect. You want to make sure you know why your database is "too big" before you shrink it. Otherwise, you're only treating the symptom, not the disease.
For example, maybe a big weekly data import causes your database to grow by 200 GB every Sunday, and then a consolidation removes 195 GB of that data every Monday. If you look at your database on Tuesday, it'll seem to be much bigger than it needs to be. But if you shrink it, it'll just grow back on the following Sunday.
In the case presented in this question, the size of the databases is the result of an erroneous initial setup, rather than the result of normal activity. As such, shrinking it makes good sense.