Getting more out of the SQL reports and stored procedures you already have
Every business that has run on a SQL database for a few years has a collection of report queries. Daily sales by department. Stock below reorder level. Supplier statement reconciliation. Monthly commission. Some live in stored procedures, some in .sql files on someone's desktop, some inside an old reporting tool that only one person still knows how to use.
These queries are valuable. They encode years of knowledge about how your data really works: which status codes to exclude, which table holds the real cost price, how returns are recorded. They are also often trusted more than any new tool, because the numbers have been checked against reality for years.
Yet many of them are run rarely, by one person, and only when someone asks. This post is about getting more out of them.
Step 1: Collect them in one place
Before improving anything, find what you have:
- Stored procedures in the database. In SQL Server Management Studio they are under Programmability → Stored Procedures. In MySQL,
SHOW PROCEDURE STATUS WHERE Db = 'your_db';lists them. - Views, which are often saved report queries.
.sqlfiles on the computers of whoever does the reporting.- Queries inside reporting tools (Crystal Reports, Excel connections, older BI tools).
Put copies in one shared folder, one file per report, with a first-line comment saying what question it answers, for example "-- Daily sales by department".
Step 2: Make the dates easy to change
Many old report queries have dates typed straight into them:
WHERE SaleDate BETWEEN '2025-01-01' AND '2025-01-31'
Every time someone runs it for a new period, they edit the text and hope they changed both dates. Replace them with named parameters at the top, so the query is clearly reusable:
-- Daily sales by department
DECLARE @from date = '2025-01-01';
DECLARE @to date = '2025-01-31';
SELECT d.DeptName, CAST(s.SaleDate AS date) AS Day, SUM(s.Amount) AS Sales
FROM dbo.Sales s
JOIN dbo.Departments d ON d.DeptCode = s.Dept
WHERE s.SaleDate >= @from AND s.SaleDate < DATEADD(day, 1, @to)
GROUP BY d.DeptName, CAST(s.SaleDate AS date)
ORDER BY Day, d.DeptName;
Note the < DATEADD(day, 1, @to): if SaleDate includes a time, BETWEEN ... AND '2025-01-31' quietly misses everything after midnight on the last day. It is one of the most common bugs in old reports.
Step 3: Check which procedures change data
Some stored procedures only read. Others update summary tables, close periods or recalculate stock. Before anyone runs a procedure casually, or connects a tool to it, know which kind it is.
Open each procedure's definition and look for INSERT, UPDATE, DELETE, MERGE, TRUNCATE or calls to other procedures. Mark the ones that change data clearly, and make sure reporting logins cannot run them. A read-only login (see our guides for SQL Server and MySQL) cannot run procedures at all unless you grant EXECUTE on specific ones.
Step 4: Turn the important ones into warnings
The biggest waste with report queries is that they only answer when someone asks. Yet many of them describe exactly the numbers that matter most, and those numbers have a normal range.
Take daily sales by department. If you ran it every night and compared each department with the same weekday over recent weeks, it would tell you the morning after a department had an unusually bad (or good) day. You would not need to remember to run it.
You can do this with a scheduled job that runs the query nightly, stores the result, and emails a short note when a value is far from its usual range for that weekday. It is a few hours of work for someone comfortable with SQL Agent or cron, and it turns a report you look at monthly into a warning you get the next morning.
Step 5: Let people ask for them in plain words
Most people who need a report do not want to open SQL Server Management Studio. They want to ask "what were sales by department last week?" and get the answer that matches the report they trust.
That is the idea behind how Cuedis uses your existing reports. Under Settings → Advanced → Your report queries you give each query or stored procedure the question it answers ("Daily sales by department") and paste the SQL, give the procedure's name, or bring a whole .sql file at once. Cuedis then:
- answers with your query when someone asks that question, on Ask or on Telegram, filling in the dates the question names (written as
@fromand@to, or picked up fromDECLARElines and date ranges like the ones above); - checks procedures before running them, only runs ones that do not change data, and undoes anything they do;
- can watch a report every night if you tick watch, and tells you in the morning briefing when a day is far from normal for that weekday.
Your years of report knowledge keep working, and the answers people get match the numbers they already trust.
The short version
- Collect your reports in one place, each labelled with the question it answers.
- Replace typed dates with
@fromand@to, and fix theBETWEENend-date bug. - Know which procedures change data, and keep reporting logins read-only.
- Run the important ones nightly and compare with the usual range, so they warn you instead of waiting to be asked.
Old report queries are some of the most valuable code a business has. They deserve better than a folder nobody opens.