Dev.to WebDev 🛠 Dev 👁 0 📖 7 min read

A multi-step form with no session, no state machine and no step counter.

Every click re-runs this T-SQL script from line 1. On purpose. A multi-step form usually needs somewhere to keep the half-finished answers: a session, a draft row, or a state machine with a current_step column. Then so

Every click re-runs this T-SQL script from line 1. On purpose.

A multi-step form usually needs somewhere to keep the half-finished answers: a session, a draft row, or a state machine with a current_step column. Then some state on the client, an endpoint per step on the server, and a job that cleans up after the people who wander off halfway through step two.

We build TSQL.APP, a framework where business applications live entirely inside SQL Server: screens, buttons and permissions are T-SQL and metadata. That rules all of the above out. A T-SQL batch runs and finishes. It cannot sit at line 14 for forty seconds while someone decides what to type. There is no await in T-SQL, and a language with no continuations cannot be suspended halfway.

So nothing is suspended. Every click re-runs the script from line 1, and the step the user is on is not stored anywhere. That sounds like a workaround. It is actually a well-known idea in an unfamiliar place, and the rules that come with it apply wherever you build multi-step flows.

A two-step dialog in T-SQL

Ask for a title, then for a due date, then save:

-- Step 1
EXEC sp_api_modal_text   N'New task', N'h4'
EXEC sp_api_modal_input  @name = '@title', @value = @title OUT, @placeholder = N'What needs doing?'
EXEC sp_api_modal_button @name = '@next', @value = 'Next', @valueout = @next OUT
IF @next IS NULL RETURN                     -- pass 1 ends here

-- Step 2
EXEC sp_api_modal_clear                     -- remove step 1 from this pass's screen
EXEC sp_api_modal_text   @title, N'h4'      -- step 2 already knows the answer to step 1
EXEC sp_api_modal_input  @name = '@due', @value = @due OUT, @placeholder = N'Due date (yyyy-mm-dd)'
EXEC sp_api_modal_button @name = '@save', @value = 'Save', @valueout = @save OUT
IF @save IS NULL RETURN                     -- pass 2 ends here

-- Only the last pass gets this far
INSERT INTO task (title, due_date) VALUES (@title, TRY_CAST(@due AS date))
EXEC sp_api_modal_clear

There are no DECLAREs because the framework declares the variables a script uses. task stands in for any table of yours.

Each text, input and button call adds one element to the screen and returns at once. None of them waits for the user. The IF ... RETURN lines are the only flow control, and they do not pause anything. They end the batch. The request's transaction commits, the connection goes back to the pool, and while the user is reading the screen, nothing of this script is running on the server.

The user clicks Next, and the same script runs again from the top:

Pass The request carries The script stops at The user sees next
1 no answers the first guard: @next is NULL step 1: a title field and Next
2 @title, @next the second guard: @save is NULL step 2: the title, a date field and Save
3 @title, @next, @due, @save the end the dialog closes; the row is inserted

On pass 2, step 1 is drawn again and wiped straight away by sp_api_modal_clear. On pass 3 both steps are drawn and wiped, and the INSERT finally runs.

One script, three executions, no continuation. Nothing records that the user is on step 2. It is derived: step N is the first question whose answer is not in the request yet.

Where the answers live

In the request. Every click posts a JSON body with the answers so far, keyed by element name. Trimmed to the part that matters, pass 2 sends:

{
  "name": "New task",
  "values": {
    "@title": "Write the release notes",
    "@next": "Next"
  }
}

The server sends that body back along with the next screen. The client adds the values of the elements it just drew and posts the whole object again on the next click. So the answer set is held by the client, seeded by the server, and grows by one step per round trip:

sequenceDiagram
    participant B as Browser
    participant S as SQL Server
    B->>S: open the dialog, no answers
    activate S
    Note right of S: line 1 to the first guard
    S-->>B: step 1, plus the request echoed back
    deactivate S
    Note over B: title typed, Next clicked
    B->>S: values @title, @next
    activate S
    Note right of S: line 1 to the second guard
    S-->>B: step 2, plus the request echoed back
    deactivate S
    Note over B: date typed, Save clicked
    B->>S: values @title, @next, @due, @save
    activate S
    Note right of S: line 1 to the end, INSERT
    S-->>B: the dialog closes
    deactivate S

The bars on the SQL Server line are the only time anything of the dialog exists on the server. Between clicks, the dialog's state lives entirely in the browser.

On the server, every collector looks in the request for an answer under its own name, and if it finds one, uses it. This is that check in the button procedure, verbatim:

EXEC sp_api_modal_get_value @name,@value = @postvalue OUT
IF @postvalue is not null
    BEGIN
        SET @valueout = @postvalue
    END

Before the click, @next is NULL. After it, it holds the button's caption. That is the entire flow-control primitive, and the framework's own settings screens are built on it the same way.

You have seen this shape before

Re-running code from the top and keeping the state somewhere else is not new. The interesting differences are where the state lives and how each piece of it is identified.

Dear ImGui draws and asks in one call. if (ImGui::Button("Save")) Save(); renders a button and returns true on the frame it was clicked: the same move as sp_api_modal_button followed by IF @save IS NULL RETURN, with an HTTP round trip as the frame. ImGui identifies a widget by a hash of its label and the scope it sits in. Its FAQ calls a clash of identical labels the most common user mistake (click either button and the first one fires), and the fixes are a hidden suffix such as "Play##1", or PushID() inside loops.

React re-runs a component function on every render and hands the state back through hooks. It matches state to hooks by call order, which is why hooks cannot go inside conditions or loops.

Durable Functions and Temporal re-execute an orchestrator from the start whenever it wakes up, replaying earlier results from a history kept in storage. The code has to be deterministic, and the Durable Functions docs warn that an orchestrator that logs can emit the same message again on replay.

The T-SQL version keys state by name, like ImGui. Position does not matter, which is why every step after the first can sit behind a guard; in React terms, that would be a conditional hook. And unlike a durable-execution engine, it keeps no history on the server. The history is the request.

The rules that come with it

Writes go below the last guard. Everything above the first unanswered guard runs again on every later pass. Move the INSERT above the second guard and the example writes two rows: one on pass 2, without a due date because nobody has been asked for it yet, and another on pass 3. React's StrictMode renders twice in development to flush out exactly this kind of side effect. Put writes after the final guard, or make them idempotent.

Clear at the start of each step. Leave out the sp_api_modal_clear at the top of step 2 and pass 2 shows both steps at once, because the replay drew step 1 before it reached step 2.

Names are the key. Two inputs named @x in one script share one answer. A loop that draws one input per row needs a distinct name per row, which is what the collectors' @seed parameter does: it is appended to the name. It is ImGui's ## suffix under another name.

The transaction belongs to the request, not to the dialog. Each pass commits on its own. An error on the last pass rolls back that pass; what earlier passes committed stays.

The answers come from the client. Treat them as user input, because that is what they are. The flip side is useful: the final pass runs every line above it, so a check you wrote for step 1 also runs on the pass that writes, even if someone posts all the answers in a single request.

What it costs, and what it buys

It costs the long imperative script that reads top to bottom as one story with pauses in it. You think about idempotence every time you write above a guard, and the first step of a two-step dialog runs three times.

What it buys is the absence of a whole category of state. There is no session behind a dialog to expire, nothing to clean up when someone closes the tab on step 2, and no server-side wizard state for two users to trip over. A lost request does not leave anyone stranded halfway: posting the same answers again produces the same screen, because the screen is derived from the answers and the database. The one exception is the final write, which is the idempotence rule again.

None of this needs T-SQL. A CLI wizard, a chat flow or a server-rendered form can work the same way: re-run the flow from the top on every input, let each question look up its own answer by name, and stop at the first one that has none.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.