Home / Guides / Report on eClinicalWorks data

Report on eClinicalWorks data without touching production

Billing, quality and operations reports all want eClinicalWorks (eCW) data, and nobody wants a heavy report slowing the system the front desk is using. The usual answer is a separate, read-only copy of the database on your own SQL Server. This guide covers the reporting side of that copy: who can read it, when reports can run, and how a report tells its reader how fresh the data is.

Short answer.

Point your reports at the restored copy, never at production. Give them one login that can only read, grant it at server level (the copy is read-only, so you cannot add a user inside it), schedule reports after the nightly restore finishes, and print "data as of" on every report from the restore history in msdb.

What eClinicalWorks provides, and how, depends on your arrangement with them. This guide starts from a restored SQL Server copy and is not affiliated with or endorsed by eClinicalWorks. How the copy is built is in Restore the nightly eClinicalWorks backup to your own SQL Server.

Step 1: one read-only login for reports

The usual way to give a reporting account read access is a database user in the db_datareader role. That does not work on a copy kept in standby, because the database is read-only between restores:

USE [EcwCopy];
CREATE USER [ecw_reports] FOR LOGIN [ecw_reports];
-- Msg 3906: Failed to update database "EcwCopy" because the database is read-only.

Even if you could add one, a rebuild of the copy from a new starting restore would replace it. The way around both problems is to grant the permission on the server instead of in the database. Two server permissions do it: CONNECT ANY DATABASE lets the login connect, and SELECT ALL USER SECURABLES lets it read user tables and views. Microsoft documents them as a pair for read-only access such as auditing. Both exist from SQL Server 2014 on.

USE [master];

-- A SQL login for the reporting tools (use CREATE LOGIN ... FROM WINDOWS for a Windows account or group).
CREATE LOGIN [ecw_reports] WITH PASSWORD = N'<a long random password>', CHECK_POLICY = ON, DEFAULT_DATABASE = [master];

-- Server-level read access: connect to any database, and read user tables and views in it.
GRANT CONNECT ANY DATABASE       TO [ecw_reports];
GRANT SELECT ALL USER SECURABLES TO [ecw_reports];

-- Check it: as the login, a read works and a write is refused (error 229).
EXECUTE AS LOGIN = 'ecw_reports';
SELECT COUNT(*) FROM [EcwCopy].dbo.[SomeTable];
REVERT;

We ran this on SQL Server 2022 against a log-restored standby copy. The login read the data, an INSERT was refused (error 229), and the permission lives on the server, so it does not depend on what is inside the restored database. Use a Windows group (CREATE LOGIN [YOURDOMAIN\ReportReaders] FROM WINDOWS) rather than a password login if your reporting tools can sign in with Windows accounts; the grants are the same.

Two limits to know about:

  • The grants cover every database on the instance. Run the copy on its own instance, or on a server that holds nothing else sensitive, so this one login cannot read anything it should not.
  • It is read-only twice over. The copy cannot be written to between restores, and the login has no write permission either, so a badly written report cannot change data.

Step 2: respect the restore window

The nightly restore needs exclusive access to the database. With a report holding a connection open, the restore fails:

RESTORE LOG [EcwCopy] FROM DISK = N'D:\Ecw\nightly.trn' WITH STANDBY = N'D:\Ecw\YourEcwCopy.stby';
-- Msg 3101: Exclusive access could not be obtained because the database is in use.
-- Msg 3013: RESTORE LOG is terminating abnormally.

We reproduced this with one open query session. After the session ended, the same restore succeeded. Because a missed log restore stops every later night from applying, a report that is still running when the restore starts can cost you the copy, not just the report. Three habits keep reports out of its way:

  • Schedule reports after the restore, not before. If the restore usually finishes by 3:00 a.m., start subscriptions and refreshes at 6:00 a.m., not at 2:00.
  • Keep reports short. A report that runs for hours across the restore time is the one that blocks it. Aggregate into your own tables in a separate database (see "Where this stops") and report from those.
  • Disconnect users on purpose. Microsoft's own log shipping has an option to disconnect users while a restore runs. If you script the restore yourself, do the same before each restore, so that a forgotten Excel connection does not break the chain.

Step 3: print "data as of" on every report

A restore that failed last night leaves a copy that still looks perfectly healthy: it answers queries, just with old numbers. The fix is cheap. SQL Server records every restore in msdb, including the time the backup it applied was finished. Show that time in the report header:

USE [msdb];

SELECT TOP (1)
       rh.destination_database_name                      AS db,
       rh.restore_date                                   AS restored_at,
       bs.backup_finish_date                             AS data_as_of,
       DATEDIFF(HOUR, bs.backup_finish_date, GETDATE())  AS hours_old
FROM dbo.restorehistory AS rh
JOIN dbo.backupset      AS bs ON bs.backup_set_id = rh.backup_set_id
WHERE rh.destination_database_name = N'EcwCopy'
  AND rh.restore_type = 'L'             -- L = log restore
ORDER BY rh.restore_history_id DESC;

data_as_of is when the last applied log backup was taken, and restored_at is when it was applied. Make this a small dataset in each report and show hours_old next to the title. In SSRS you can also show a red "data is out of date" line when it is more than a day and a bit old (26 hours, say). Two cautions:

  • The restore history is cleared by sp_delete_backuphistory and by history cleanup jobs. If a report gets no row, treat that as "unknown", not as "fresh".
  • The rows exist even though the backups were made on someone else's server: restoring a backup adds its backup set to the restore server's msdb. We checked this by deleting the history of the source database before restoring, and the query still returned the right times.

Step 4: connect SSRS and Power BI the right way

  • SSRS. Subscriptions and scheduled processing need stored credentials on the data source (a data source set to Windows integrated security cannot run unattended). Store the read-only login there, and run subscriptions after the restore window.
  • Power BI. In Import mode, the Power BI service refreshes an on-premises SQL Server through an on-premises data gateway. The server and database names in the gateway data source must match the ones in the report, and Microsoft notes that a scheduled refresh can start up to 15 minutes after its set time, so leave a margin after the restore.
  • Excel and ad-hoc tools. Give people the same read-only login, and tell them the copy is closed for a while each night. A connection a person leaves open overnight is an easy way to make a restore fail.

Where this stops

  • You cannot add anything to the copy. No indexes, views or helper tables, because it is read-only. Create them in a separate reporting database on the same instance and reference the copy by three-part name, EcwCopy.dbo.TableName.
  • The copy is only as fresh as the last restore. It is yesterday's data, not live data. Anything that must reflect the last hour belongs in the application itself.
  • A missing night stops the chain. Until the copy is rebuilt, reports show old numbers, which is why Step 3 matters.
  • Patient data stays patient data. Limit who gets the report login, keep the copy on a server you control, and apply the same access controls and auditing you apply elsewhere. This guide is not legal or compliance advice.

Questions

Can I report straight from the eCW production database?

That depends on your arrangement with eClinicalWorks, and many practices do not have direct database access at all. Where you do, a long report competes with the people using the system. A separate read-only copy on your own SQL Server keeps reporting away from it.

Why not add a database user with db_datareader?

A copy kept in standby is read-only between restores, so SQL Server refuses to create the user (error 3906), and a rebuild of the copy would discard it anyway. Granting CONNECT ANY DATABASE and SELECT ALL USER SECURABLES to a login keeps the permission on the server, outside the restored database.

Will a running report break the nightly restore?

It can. The restore needs exclusive access, and with a connection open it fails with error 3101. A failed restore leaves a gap in the log chain, and later nights cannot be applied until the copy is rebuilt. Schedule reports after the restore and disconnect users before it.

How do I show readers how old the data is?

Query the restore history in msdb for the copy and show the finish time of the last applied log backup as data as of on the report. If the query returns no row, treat the data as unknown rather than fresh.

Does RestoreMD build the reports?

No. RestoreMD restores your nightly eClinicalWorks backup to a read-only SQL Server database every night and records each run. The reports you build on that database, in SSRS, Power BI, Excel or plain T-SQL, are yours. See the RestoreMD page.

Related

Sources (Microsoft Learn): GRANT server permissions, restorehistory, backupset, sp_delete_backuphistory, Subscriptions and delivery (SSRS), Refresh data from an on-premises SQL Server database (Power BI), Manage a SQL Server data source (gateway).

eClinicalWorks® is a registered trademark of eClinicalWorks, LLC. RestoreMD is an independent service and is not affiliated with, endorsed by, or sponsored by eClinicalWorks.

An unhandled error has occurred. 🗙