Skip to content

Stored procedures

Teams on SQL Server, Oracle and PostgreSQL keep business logic in stored procedures. By default sqlguardllm rejects every EXEC and CALL. Add procedures to the policy to allow specific calls:

procedures:
  dbo.SalesByRegion:
    params:
      "@region": nvarchar(50)
      "@year": int
      "@tenant_id": {bind: "{user.tenant_id}"}   # set from context, never by the model

What is accepted

  • EXEC dbo.SalesByRegion @region = N'North', @year = 2026 on SQL Server: named parameters only, literal values type-checked.
  • CALL public.refresh_totals(a => 1, b => 'a') on PostgreSQL: named arguments only, literal values type-checked.
  • Context-bound parameters are injected by the guard as typed literals.

What is rejected

Case Code
Procedure not listed procedure_not_allowed
Dynamic SQL (EXEC(@sql), sp_executesql) procedure_not_allowed
Linked servers (EXEC (...) AT ...) procedure_not_allowed
Positional, unknown, repeated or non-literal parameters procedure_param_invalid
Wrong literal type procedure_param_invalid
The model passes a context-bound parameter procedure_param_invalid

Security

A listed procedure is trusted code: it runs with its owner's permissions and may write. Row filters, masks and table allowlists do not apply inside it. Context-bound parameters are how calls are scoped to a tenant or user. List only procedures you would let the caller run.

Anything the parser does not recognise is rejected (fail closed).

Try the policy locally

Run the following Python example with uv run python from the repository. It checks and rewrites a procedure call without connecting to a database.

from sqlguardllm import Guard, Policy

policy = Policy(
    dialect="tsql",
    procedures={
        "dbo.SalesByRegion": {
            "params": {
                "@region": "nvarchar(50)",
                "@tenant_id": {"bind": "{user.tenant_id}"},
            },
        },
    },
)
guard = Guard(policy)
context = {"user": {"tenant_id": 7}}
allowed = guard.check("EXEC dbo.SalesByRegion @region = N'North'", context)
assert allowed.ok and allowed.sql is not None
print(allowed.sql)  # Includes the tenant ID from context.

refused = guard.check("EXEC dbo.SalesByRegion @region = N'North', @tenant_id = 9", context)
assert not refused.ok
print(refused.violations[0].code.value)  # procedure_param_invalid