Microsoft SQL Server | CREATE FILE encountered operating system error 5(Access is denied.)

You might come across this error when trying to attach an existing MDF file.

TITLE: Microsoft SQL Server Management Studio
------------------------------

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------

CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'D:\Microsoft SQL Server\MSSQL\Data\AM8K.mdf'. (Microsoft SQL Server, Error: 5123)

For help, click: https://docs.microsoft.com/sql/relational-databases/errors-events/mssqlserver-5123-database-engine-error

------------------------------
BUTTONS:

OK
------------------------------

Please do the following:

Make sure currently logged in Windows/Domain user has access to the physical folder where the MDF file resides.

Start “Microsoft SQL Server Management Studio” as administrator.

Now, try to attach the MDF file, this should resolve the Access Denied error.

Microsoft ODBC Driver 17: A previous installation required a reboot of the machine for changes to take effect.

Today I was trying to install SSMS SQL Server Management Studio 18.9.1 & the installation was stopped with a message “Microsoft ODBC Driver 17: A previous installation required a reboot of the machine for changes to take effect”

Abiding, I went ahead to reboot & instead pressed the shutdown button. After restarting the PC, tried to install SSMS once again and the installer gave up mentioning the same message.

All I needed was “reboot” the machine. Microsoft is sometimes, grr.

Microsoft SQL Server | Reduce disk usage

Recently we wanted to shutdown a server for RAID rebuild and a copy of the MS SQL database was restored on my laptop machine. While checking the vitals of the database (3rd party configured), I noticed that the database was using FULL Recovery model and the Transaction logs were consuming 97.6% of the total reserved space. As I didn’t want to waste expensive SSD storage from my laptop, decided to change the recovery model to “Simple”, which doesn’t really release the free space.

MS SQL provides a functionality called shrinking, using that you can shrink both database and transaction log files. I will not recommend you to use shrinking in a production environment, however you can always try “stuffs” on a TEST environment.

Let us see how the shrinking is initiated and how does it change the disk space usage. Before shrinking, make sure that you have changed the database Recovery model to “Simple” from “Full”

Right click on the target database and select “Tasks->Shrink->Files”. Cross verify that you selected “Files”

From the Database Files and filegroups section, for File Type Select Log from the dropdown. Verify rest of the defaults and click “OK”. This will initiate shrinking the Transaction Logs and based on the size of the file(s), this may take a while to finish. You can re-run the reports to verify that disk space usage.

After the shrinking finishes, you will see that all the unused space reserved for the Transactional Logs were released by running the Disk Usage standard report.

Although I asked you not to use shrinking in a production environment, feel free to do it from your local system. Learn what breaks and what makes systems, so that you can take a better decisions while handling production instances.

MS SQL | Backup remote database on local machine

Hi guys

At times we have to have a recent backup of the SQL database on a development machine to insure that our testing is false-proof prior the same is pushed to a production instance. As SSMS (SQL Server Management Studio) doesn’t allow remote backup functionality out of the box, we can try the following (applies to both home networks and windows domain networks)

From your local machine, setup a shared folder, giving full read/write access to the entity “Everyone”

Now, prior attempting below instructions, be sure of your privileges against the target database. Are you a sys admin? Does your database account have the rights to make a backup? If yes, go ahead

Now, start SSMS & proceed with the backup task

Depending upon the security constraints, you may disable the sharing of local folder immediately after the backup completion.

regards,

rajesh

MS SQL 2019 | Upgrade/Fresh Installation

Hi guys

My laptop has many software development components installed, say few of them are there from last many years (regardless whether I still need them) & such get me in to complicated situations (most of the times)

I’ve had Visual Studio 2013 Professional edition installed for last many years & recently I have switched to the community edition (which is as good as pro edition & costs nothing)

After upgrading my SQL Server 2017 (Developer Edition) on my home computer using 2019 ISO mounted media, I decided to upgrade the SQL Server 2017 on my laptop and it failed, miserably!

So I decided to do a fresh installation, that also failed. Luckily I came across the following thread:

https://techcommunity.microsoft.com/t5/sql-server-support/sql-server-2019-installation-error-an-error-occurred-for-a/ba-p/998033#

While, there could be more reasons for the failure to install/upgrade your existing SQL server, make sure whether you have “Microsoft SQL Server 2012 Native Client” already installed, if yes, remove it (Which might popup a warning stating the dependency of Local DB 2016 on the Native client, hence make sure that you know what you are doing)

Once the 2012 Native client uninstalled, try again to upgrade/fresh installation of SQL Server 2019.

You should be through!

regards,

rajesh

Transact SQL | Virtual Calendar

Hello guys

We’re integrating MS SQL servers everywhere for our business along with long trusted technology partner Oracle & will try to include what we have “learned” in our labs within this blogosphere.

Our bio metric attendance system reports are designed with virtual dates (weekends when the employees are not punching, yet the reporting requires the whole month dates appearing in the final output, marking the date and day name for the date) from a virtual calendar created using connect by level pseudocolumn

CREATE OR REPLACE FORCE VIEW XXFPAUTOCALNDR_V
(
   EMPLOYEE_NUMBER,
   DAY_DATE,
   PUNCH_PERIOD
)
AS
       SELECT NULL EMPLOYEE_NUMBER,
              TRUNC (TO_DATE ('01-JAN-2009', 'DD-MON-YYYY') + LEVEL) day_date,
              TO_CHAR (TRUNC (TO_DATE ('01-JAN-2009', 'DD-MON-YYYY') + LEVEL),
                       'MMYYYY')
                 punch_period
         FROM DUAL
   CONNECT BY TO_DATE ('01-JAN-2009', 'DD-MON-YYYY') + LEVEL <=
                 LAST_DAY (TRUNC (SYSDATE));

Well, MS SQL doesn’t provide connect by level approach, instead recursive CTE (common table expression) & let us see how we can create a virtual calendar using CTE on MS SQL

CREATE VIEW UnisVCalendar
as
WITH CTE(virtual_date) AS (
  SELECT cast('1/1/2016' as datetime) as virtual_date
  UNION ALL
  SELECT virtual_date+1
  FROM CTE 
  WHERE virtual_date <= GETDATE() 
)
SELECT * FROM CTE
GO

CTE allows referring the table itself, thus indirectly providing a level sort of reference.

Well, the downside of such a virtual calendar is, one should make sure that to use option(maxrecursion 0) to avoid “The maximum recursion 100 has been exhausted before statement completion” error

Select CONVERT(date,virtual_date,105) from UnisVCalendar
WHERE CONVERT(date,virtual_date,105) BETWEEN '01-JAN-2020' AND '31-JAN-2020'
option (maxrecursion 0)
GO

Now you can safely refer this virtual calendar at applicable situations.