I have the task of getting the free space in the database each month, and emailing the results using Database mail.
Database Mail is setup and working. I have a query (copied from Greg Robidoux) that gets the data.
Select DB_Name() as DB,
name as DB_File,
size/128.0 as Size_MB,
size/128.0 - cast(FileProperty(Name, 'SpaceUsed') as Int)/128.0 as Free_MB
From sys.database_files;
When I try to add the query in "Job Step Properties" I get a parse error.
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
Incorrect syntax near 'SpaceUsed'. (Microsoft SQL Server, Error: 102)
For help, click: https://docs.microsoft.com/sql/relational-databases/errors-events/mssqlserver-102-database-engine-error
The command I added is below
@query = N'Select DB_Name() as DB,
name as DB_File,
size/128.0 as Size_MB,
size/128.0 - cast(FileProperty(Name, 'SpaceUsed') as Int)/128.0 as Free_MB
From sys.database_files;'
Clearly doesn't like 'SpaceUsed'. How can I workaround this problem?