r/SQL • • 9d ago • Bravo 1

SQL Server I Create Things in SQL that Shouldn't be in SQL

Hi there, I have been using SQL Server since 1998 - Version 6.0 I think it was and also a SQL Server MVP for 6 years. I have started publishing the articles I have written over these years but never published. I have quite a varied mix. I Hope you enjoy them.

Mostly on https://www.medium.com/@trevor.dwyer but also published on https://sqlartist.github.io

One rule - it has to run in SQL.

Titles include:

Running Doom Inside SQL Server

Running Elite in SQL Server

I Built My Head Out of Lego. Inside SQL Server 2025.

Cracking Feynman’s Safes in SQL Server
Can a Database Rave? Making SQL Server Play Music

and quite a few others

280 Upvotes

30 comments sorted by

38

u/delsystem32exe 9d ago

this is the coolest thing i have seen in a year!

23

u/Appropriate-Run-7005 9d ago edited 9d ago

You made my day saying that - wait until you see what else is on there :)

21

u/redaloevera 9d ago

I’m not even really sure what it means to run games or draw a head in sql server?

3

u/Appropriate-Run-7005 7d ago

Just as the other replies have nailed the reason for doing this - it makes people think differently, I was thinking of a real application for the running of games for example. In the same way as the Wayback machine hosts old games that I think are even playable for some formats, you could literally store old format games inside a database, or like I did for the ZX Spectrum store the machine itself and then run the stored games from the emulation. I mean people are probably doing this anyway but the database method does then give you all the benefits of using a database.?

26

u/mhac009 9d ago

Nobody does but it gets the people going!

12

u/ThortheAssGuardian 8d ago

It’s PROVOCATIVE

5

u/lord_xl 8d ago

Ball so hard ..

17

u/rainu1729 9d ago

A database can't run this.. OP: hold my 🍺..

2

u/Appropriate-Run-7005 7d ago

I love this comment :)

97

u/cybertrips 9d ago

But can it run…. Oh.

12

u/Sa0t0me 8d ago

Next is a Nintendo 64 emulator running Mario kart

5

u/Appropriate-Run-7005 8d ago • Mindblown

The article on ZX Spectrum running Daley Thompson Decathlon nearly killed me - I wanted to do the Commodore 64 version but could not work out how to do it so I resorted to the Spectrum version which was hard enough as the entire ZX Spectrum had to be emulated and running inside SQL Server and then load a version of Decathlon which wasn’t copy protected / turns out the copy protection on cassettes was real - back then I had no idea that’s why it wasn’t loading. But the version on the micro drive wasn’t protected. So once the hardware was emulated I could then work out the load from this. 

2

u/Sa0t0me 8d ago

You just reminded me of old cassette protection in old music from the 80s and how I wanted to reuse this country cassette for my music , the recorder would not engage them realized the missing flap and replaced it with tape … I was in jr high lol

6

u/OGxPePe 9d ago

I dont know if you are a genius, madman or both. Very cool stuff.

7

u/Appropriate-Run-7005 8d ago

Weirdly I have a tattoo on my arm that says "There is no genuis without madness" or it actually says "nullum magnum ingenium sine mixtura dementiae fuit"

6

u/Appropriate-Run-7005 8d ago

Someone asked this but then removed it - I'm happy to say what my background is and why I have taken it on.

The articles do say why I am doing this -

Due to my Chronic Cluster Headaches mentioned in the story “The Cloud Has No Mercy” The Cloud Has No Mercy, I was told after many years of devastating pain that I would require an operation and a device called a Deep Brain Stimulator installed as they had tried everything else open to them. I had this operation and that is detailed in the Six Billion Dollar Man article https://medium.com/@trevor.dwyer/the-six-billion-dollar-man-8ed53378971d but then I got Stage 4 cancer and had to deal with that.

By the end of it all and somehow still here I decided it would be a shame to sit on these ideas any longer and I started in Late August and have created 23 articles since then. As I say even if one person gets something from one of the ideas or bits of code or simply not what to do then I would be happy. Having said that making the eventual book, the film about my life and the inevitable awards would be far better (Except I am stuck not being able to go more than 10 mins from my house ever again) - that is the deal with keeping me alive I guess

2

u/dbxp 8d ago

Are you cheating with cli sprocs?

6

u/Appropriate-Run-7005 8d ago

pure SQL, No cheating. The only time I go out of SQL Server is to PostgreSQL due to its ability to handle 3D geometry in one of the articles. Even the music one is all done in SQL. All the code is available to try yourself.

3

u/dbxp 8d ago

I once worked for a place which composed their web UI via string concatenation in SQL, it was horrible 

5

u/Appropriate-Run-7005 8d ago

That is actually how the AS/400 screens were created both for real and in the emulator

2

u/lord_xl 8d ago

All the code is available to try yourself.

We're not worthy

2

u/Yellowcat123567 8d ago

Can you create UI or HTML web pages? I actually love SQL and would consider using it for literally everything. I can understand backend code use cases but how would you do front end?

1

u/Appropriate-Run-7005 7d ago

if anyone knows of a better way to post this please let me know and I will change the format to the correct one.

Strange request - you could probably find this out fairly easily with a simple search, however as you said "you love SQL" I have to encourage that so here you go. This works on SQL Server but literally any database engine could do the same thing. Most of the have something built in which will send a formatted HTML email

CREATE OR ALTER PROCEDURE dbo.GenerateProductReportHTML
     NVARCHAR(100) = 'Product Inventory Dashboard',
     NVARCHAR(MAX) OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    -- 1. Create sample data
    DECLARE  TABLE (
        ProductName NVARCHAR(50),
        Category NVARCHAR(50),
        StockLevel INT,
        Price DECIMAL(10, 2)
    );

    INSERT INTO  (ProductName, Category, StockLevel, Price)
    VALUES
        ('Laptop Pro', 'Electronics', 45, 1200.00),
        ('Wireless Mouse', 'Accessories', 5, 25.00),    -- Low stock
        ('Mechanical Keyboard', 'Accessories', 12, 150.00),
        ('Monitor 27"', 'Electronics', 0, 300.00),      -- Out of stock
        ('USB-C Hub', 'Accessories', 85, 45.00);

    -- 2. Define the CSS (The "Clever UI" styling)
    DECLARE  NVARCHAR(MAX) = N'
        <style>
            body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; background-color: #f8f9fa; color: #333; padding: 30px; }
            h2 { color: #2c3e50; border-bottom: 3px solid #3498db; padding-bottom: 10px; display: inline-block; }
            table { border-collapse: collapse; width: 100%; max-width: 900px; background: #fff; box-shadow: 0 4px 12px rgba(0,0,0,0.05); border-radius: 8px; overflow: hidden; margin-top: 20px; }
            th, td { padding: 15px 20px; text-align: left; border-bottom: 1px solid #edf2f7; }
            th { background-color: #2c3e50; color: #ffffff; text-transform: uppercase; font-size: 0.85em; letter-spacing: 0.5px; }
            tr:hover { background-color: #f8f9fa; }
            
            /* Dynamic Data Classes */
            .stock-good { color: #10b981; font-weight: 600; }
            .stock-low { color: #f59e0b; font-weight: 600; }
            .stock-out { color: #ef4444; font-weight: 600; text-decoration: line-through; }
            
            .badge { padding: 5px 10px; border-radius: 20px; font-size: 0.8em; color: white; font-weight: bold; }
            .bg-elec { background-color: #8b5cf6; }
            .bg-acc { background-color: #0ea5e9; }
        </style>
    ';

    -- 3. Generate the table rows dynamically
    DECLARE  NVARCHAR(MAX);

    SELECT  = STRING_AGG(
        CAST(
            N'<tr>' +
            N'<td>' + ProductName + N'</td>' +
            
            -- Inject CSS class based on Category
            N'<td><span class="badge ' +
                CASE Category
                    WHEN 'Electronics' THEN 'bg-elec'
                    ELSE 'bg-acc'
                END + N'">' + Category + N'</span></td>' +
                
            -- Inject CSS class based on Stock thresholds
            N'<td class="' +
                CASE
                    WHEN StockLevel = 0 THEN 'stock-out'
                    WHEN StockLevel < 10 THEN 'stock-low'
                    ELSE 'stock-good'
                END + N'">' + CAST(StockLevel AS NVARCHAR(10)) + N'</td>' +
                
            N'<td>£' + CAST(Price AS NVARCHAR(20)) + N'</td>' +
            N'</tr>'
        AS NVARCHAR(MAX)),
        N'' -- Empty string separator because we are adjoining <tr> tags
    )
    FROM ;

    -- 4. Assemble the final HTML document
    SET  = N'
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <title>' +  + N'</title>' +
             + N'
        </head>
        <body>
            <h2>' +  + N'</h2>
            <table>
                <thead>
                    <tr>
                        <th>Product Name</th>
                        <th>Category</th>
                        <th>Stock Level</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>' +
                    ISNULL(@TableRows, N'<tr><td colspan="4" style="text-align:center;">No data available.</td></tr>') + N'
                </tbody>
            </table>
        </body>
        </html>
    ';
END;




DECLARE  NVARCHAR(MAX);

EXEC dbo.GenerateProductReportHTML
     = 'Live Inventory Status',
     = u/MyWebPage OUTPUT;

-- Return the HTML string. 
-- In SSMS, you can click the resulting XML-like hyperlink to view the raw code, 
-- or save the output as an .html file to view it in a browser.
SELECT  AS [GeneratedHTML];

1

u/Yellowcat123567 7d ago

Cool - thank you!!

19

u/CHILLAS317 9d ago

Before clicking: oh, like the guy who did Doom in SQL Server After clicking: oh!

5

u/santathe1 9d ago

Wow that’s actually impressive.

1

u/crulge 7d ago

Love this, but Pangram says your Doom blog is 100% AI. How much of this did you do yourself, versus through an LLM?

3

u/Appropriate-Run-7005 6d ago

The only two articles I used AI on I have listed as using AI out of 23 articles The ZXSpectrum because no right minded person is reversing engineering a ZXSpectrum but the loading of the game was all me. You can check other AI tools and apparently they don't say AI is used so I'm not letting it bother me - the work stands for itself.

1

u/crulge 6d ago

Hell yeah

1

u/somacomadreams 9d ago

This is awesome.