SQL on FHIR
2.1.0-pre - release
SQL on FHIR, published by SQL on FHIR Working Group. This guide is not an authorized publication; it is the continuous build for version 2.1.0-pre built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/HL7/sql-on-fhir/ and changes regularly. See the Directory of published versions
| Official URL: http://sql-on-fhir.org/OperationDefinition/$viewdefinition-run | Version: 2.1.0-pre | |||
| Active as of 2026-06-13 | Computable Name: ViewDefinitionRun | |||
Execute a view definition against supplied or server data.
The $viewdefinition-run operation applies a ViewDefinition to transform FHIR resources into a tabular format and returns the results synchronously.
Use Cases:
FHIR Versions:
Operation may work in FHIR R4 compatibility mode or in R6 mode.
In R4 mode, operation can only be on system level ( {BaseURl}/$viewdefinition-run ),
for R6 mode operation can appear on type and instance level
({BaseURl}/ViewDefinition/$viewdefinition-run and {BaseURl}/ViewDefinition/{id}/$viewdefinition-run).
Endpoints:
URL: [base]/$viewdefinition-run
URL: [base]/CanonicalResource/$viewdefinition-run
URL: [base]/CanonicalResource/[id]/$viewdefinition-run
| Use | Name | Scope | Cardinality | Type | Binding | Documentation |
| IN | _format | system, type, instance | 0..1 | code | Output Format Codes (Extensible) | Output format for the result (json, ndjson, csv, parquet, fhir). Use fhir to return results as a FHIR Parameters resource. Optional; if omitted, the server returns ndjson by default. See Common Operation Behavior (operations-common.html). |
| IN | header | system, type, instance | 0..1 | boolean | Include CSV headers (default true). Applies only when csv output is requested. | |
| IN | viewReference | system, type | 0..1 | Reference | Reference to a ViewDefinition stored on the server. | |
| IN | viewResource | system, type | 0..1 | CanonicalResource (View Definition) | Inline ViewDefinition resource to execute. | |
| IN | patient | system, type, instance | 0..1 | Reference | Restrict execution to the specified patient. | |
| IN | group | system, type, instance | 0..* | Reference | Restrict execution to members of the given group(s). | |
| IN | source | system, type, instance | 0..1 | string | External data source to use (for example a URI or bucket name). | |
| IN | resource | system, type, instance | 0..* | Resource | FHIR resources to transform instead of using server data. Repeatable. A Bundle supplied here is unwrapped: the ViewDefinition runs against each Bundle.entry[*].resource rather than against the Bundle itself. See OperationDefinition-ViewDefinitionRun notes (Resource Parameter and Bundle Inputs). | |
| IN | _limit | system, type, instance | 0..1 | integer | Maximum number of rows to return. | |
| IN | _since | system, type, instance | 0..1 | instant | Include only resources modified after this instant. | |
| OUT | return | 1..1 | Binary | Transformed data in the requested output format, returned as a raw binary stream in the format's native media type, not a serialized Binary resource envelope. When _format=fhir is requested, the response is a Parameters resource instead. See Common Operation Behavior (operations-common.html). |
When using the GET method, the following limitations apply:
viewResource parameter (inline ViewDefinition)resource parameter (direct resources to transform)_format - Output format specificationheader - Include CSV headers (for CSV format)patient - Filter by patient referencegroup - Filter by group membership_since - Filter by last updated time_limit - Limit number of result rowssource - External data sourceviewResource parameterresource parameter for transformationThe operation can process data from:
resource parameter in the requestsource parameterThis operation uses the shared output-format enumeration (json, ndjson,
csv, parquet, fhir), content-negotiation rules, and return-representation
rules defined once in
Common Operation Behavior. Only a summary is repeated
here.
The format is selected by (in order of precedence):
_format parameter: shortened format names (json, ndjson, csv, parquet, fhir)Accept header: standard MIME types (application/json, application/x-ndjson, text/csv, application/octet-stream, application/fhir+json)Examples:
_format=json or Accept: application/json_format=ndjson or Accept: application/x-ndjson_format=csv or Accept: text/csv_format=parquet or Accept: application/octet-stream_format=fhir or Accept: application/fhir+jsonThe Accept header also governs a second, independent axis — whether the body
is the raw payload (the default) or a serialized Binary resource envelope.
See Content Negotiation.
Optional filtering parameters:
patient - Filter by patient referencegroup - Filter by group membership_since - Filter by last updated time_limit - Limit number of result rowsContent-Type set to the format’s native media type (not a serialized Binary envelope unless a FHIR media type is requested — see Return Representation)OperationOutcome resourceTransfer-Encoding: chunked. Chunked transfer is an HTTP transport choice, independent of the format; it is distinct from incremental result production. See Streaming and Transfer Encoding| Name | Type | Scope | Required | Max | Description |
|---|---|---|---|---|---|
| viewReference | Reference | type, instance | Conditional¹ | 1 | Reference to ViewDefinition on the server. Details |
| viewResource | ViewDefinition | type | Conditional¹ | 1 | Inline ViewDefinition resource |
¹ Either viewReference or viewResource is required at type level; neither allowed at instance level
| Name | Type | Scope | Required | Max | Description |
|---|---|---|---|---|---|
| _format | code | type, instance | No | 1 | Output format: json, ndjson, csv, parquet, fhir. Details |
| header | boolean | type, instance | No | 1 | Include CSV headers (default: true). Only applies to csv format |
| Name | Type | Scope | Required | Max | Description |
|---|---|---|---|---|---|
| patient | Reference | type, instance | No | 1 | Filter by patient reference. Details |
| group | Reference | type, instance | No | * | Filter by group membership. Details |
| _since | instant | type, instance | No | 1 | Include only resources modified after this time. Details |
| _limit | integer | type, instance | No | 1 | Maximum number of rows to return |
| Name | Type | Scope | Required | Max | Description |
|---|---|---|---|---|---|
| resource | Resource | type, instance | No | * | FHIR resources to transform (alternative to server data) |
| source | string | type, instance | No | 1 | External data source (e.g., URI, bucket name) |
| Name | Type | Description |
|---|---|---|
| return | Binary | Transformed data as a raw stream in the format’s native media type, not a serialized Binary envelope (a Parameters resource when _format=fhir is requested). See Return Representation |
Only one of the viewReference or viewResource parameters can be provided.
When invoking this operation at the instance level (e.g. ViewDefinition/{id}/$run), the server SHALL automatically infer the viewReference parameter from the path parameter.
The viewReference parameter MAY be specified using any of the following formats:
| A canonical URL (e.g. “http://specification.org/fhir/ViewDefinition/123 | 1.0.0”) |
Servers MAY choose which reference formats they support. Servers SHALL document which reference formats they support in their CapabilityStatement.
For servers that want to support all types of references, it is recommended to use the following algorithm:
The supported formats (json, ndjson, csv, parquet, fhir), the default,
the Accept-vs-_format precedence rule, and the optional fhir shape are
defined in Common Operation Behavior
and apply to this operation:
json, ndjson and csv by default; servers MAY
support parquet and fhir, and SHALL document supported formats in the
CapabilityStatement._format is omitted (and no format is derivable from Accept), the server
SHALL return the result in ndjson format._format is supplied, its value SHALL take precedence over Accept._format=fhir returns a Parameters resource with one repeating row per
result row, using the
SQL to FHIR type mapping.When provided, the server SHALL NOT return resources in the patient compartments belonging to patients outside of this list.
If a client requests patients who are not present on the server,
the server SHOULD return details via a FHIR OperationOutcome resource in an error response to the request.
When provided, the server SHALL NOT return resources that are not a member of the supplied Group.
If a client requests groups that are not present on the server,
the server SHOULD return details via a FHIR OperationOutcome resource in an error response to the request.
Resources will be included in the response if their state has changed after the supplied time
(e.g., if Resource.meta.lastUpdated is later than the supplied _since time).
In the case of a Group level export, the server MAY return additional resources modified prior to the supplied time
if the resources belong to the patient compartment of a patient added to the Group after the supplied time (this behavior SHOULD be clearly documented by the server).
For Patient- and Group-level requests, the server MAY return resources that are referenced by the resources being returned
regardless of when the referenced resources were last updated.
For resources where the server does not maintain a last updated time,
the server MAY include these resources in a response irrespective of the _since value supplied by a client.
The resource parameter is repeatable and carries the discrete FHIR resources
to transform instead of using server data. Because a Bundle is itself a
Resource, a Bundle satisfies the parameter’s Resource type. To avoid
ambiguity, the following rule applies:
When a resource value is a Bundle, the server SHALL unwrap it and run the
ViewDefinition against each Bundle.entry[*].resource, exactly as if those
entries had been supplied as individual repeated resource values. The
Bundle itself is not treated as an input resource for the ViewDefinition.
Unwrapping is applied one level deep. Resources within the bundle are evaluated
against the ViewDefinition’s resource type just like directly supplied
resources: entries whose type does not match the ViewDefinition’s resource are
ignored. Mixing discrete resource values and Bundle values in the same
request is permitted; the effective input is the union of the discrete
resources and every unwrapped bundle entry.
GET /ViewDefinition/patient-demographics/$run HTTP/1.1
Accept: text/csv
HTTP/1.1 200 OK
Content-Type: text/csv
Transfer-Encoding: chunked
id,birthDate,family,given
pt-1,1990-01-15,Smith,John
pt-2,1985-03-22,Johnson,Mary
pt-3,1992-07-08,Williams,Robert
POST /ViewDefinition/$run HTTP/1.1
Accept: application/json
Content-Type: application/fhir+json
{
"resourceType": "Parameters",
"parameter": [{
"name": "viewResource",
"resource": {
"resourceType": "ViewDefinition",
"resource": "Patient",
"select": [{
"column": [
{"name": "id", "type": "id", "path": "getResourceKey()"},
{"name": "birthDate", "type": "date", "path": "birthDate"},
{"name": "family", "type": "string", "path": "name.family"},
{"name": "given", "type": "string", "path": "name.given"}
]
}]
}
}]
}
HTTP/1.1 200 OK
Content-Type: application/json
[
{"id": "pt-1", "birthDate": "1990-01-15", "family": "Smith", "given": "John"},
{"id": "pt-2", "birthDate": "1985-03-22", "family": "Johnson", "given": "Mary"},
{"id": "pt-3", "birthDate": "1992-07-08", "family": "Williams", "given": "Robert"}
]
POST /ViewDefinition/$run HTTP/1.1
Accept: text/csv
Content-Type: application/fhir+json
{
"resourceType": "Parameters",
"parameter": [{
"name": "viewResource",
"resource": {
"resourceType": "ViewDefinition",
"resource": "Patient",
"select": [{
"column": [
{"name": "id", "type": "id", "path": "getResourceKey()"},
{"name": "birthDate", "type": "date", "path": "birthDate"},
{"name": "family", "type": "string", "path": "name.family"},
{"name": "given", "type": "string", "path": "name.given"}
]
}]
}
},
{
"name": "resource",
"resource": {
"resourceType": "Patient",
"id": "pt-1",
"name": [{
"use": "official",
"family": "Cole",
"given": ["Joanie"]
}],
"birthDate": "2012-03-30"
}
},
{
"name": "resource",
"resource": {
"resourceType": "Patient",
"id": "pt-2",
"name": [{
"use": "official",
"family": "Doe",
"given": ["John"]
}],
"birthDate": "2012-03-30"
}
}]
}
HTTP/1.1 200 OK
Content-Type: text/csv
id,birthDate,family,given
pt-1,2012-03-30,Cole,Joanie
pt-2,2012-03-30,Doe,John
GET /ViewDefinition/encounters/$run?patient=Patient/123&_limit=10&_format=ndjson HTTP/1.1
HTTP/1.1 200 OK
Content-Type: application/x-ndjson
Transfer-Encoding: chunked
{"id":"enc-1","patient":"Patient/123","status":"finished","class":"ambulatory","period_start":"2023-01-15T10:00:00Z"}
{"id":"enc-2","patient":"Patient/123","status":"finished","class":"emergency","period_start":"2023-02-20T14:30:00Z"}
{"id":"enc-3","patient":"Patient/123","status":"in-progress","class":"inpatient","period_start":"2023-03-01T08:00:00Z"}
A Bundle supplied as a resource value is unwrapped; the ViewDefinition runs
against each entry. This request is equivalent to Example 3, which passed the
two Patients as discrete resource values.
POST /ViewDefinition/$run HTTP/1.1
Accept: text/csv
Content-Type: application/fhir+json
{
"resourceType": "Parameters",
"parameter": [{
"name": "viewResource",
"resource": {
"resourceType": "ViewDefinition",
"resource": "Patient",
"select": [{
"column": [
{"name": "id", "type": "id", "path": "getResourceKey()"},
{"name": "birthDate", "type": "date", "path": "birthDate"},
{"name": "family", "type": "string", "path": "name.family"},
{"name": "given", "type": "string", "path": "name.given"}
]
}]
}
},
{
"name": "resource",
"resource": {
"resourceType": "Bundle",
"type": "collection",
"entry": [
{ "resource": { "resourceType": "Patient", "id": "pt-1", "name": [{"family": "Cole", "given": ["Joanie"]}], "birthDate": "2012-03-30" } },
{ "resource": { "resourceType": "Patient", "id": "pt-2", "name": [{"family": "Doe", "given": ["John"]}], "birthDate": "2012-03-30" } }
]
}
}]
}
HTTP/1.1 200 OK
Content-Type: text/csv
id,birthDate,family,given
pt-1,2012-03-30,Cole,Joanie
pt-2,2012-03-30,Doe,John
The operation uses standard HTTP status codes to indicate the outcome:
| Status Code | Description | When to Use |
|---|---|---|
| 200 OK | Success | Operation completed successfully, results returned |
| 400 Bad Request | Client Error | Invalid parameters, unsupported parameters, or malformed request |
| 404 Not Found | Not Found | ViewDefinition resource not found (instance-level invocation) |
| 422 Unprocessable Entity | Business Logic Error | Valid request but ViewDefinition is invalid or cannot be processed |
| 500 Internal Server Error | Server Error | Unexpected server error during processing |
All error responses (4xx and 5xx) SHOULD include an OperationOutcome resource providing details about the error.
When the server does not support certain parameters, it should return 400 Bad Request:
GET /ViewDefinition/123/$run?_since=2021-01-01 HTTP/1.1
Accept: application/json
HTTP/1.1 400 Bad Request
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "not-supported",
"diagnostics": "The server does not support the _since parameter",
"expression": ["_since"]
}
]
}
When the provided ViewDefinition is invalid, return 422 Unprocessable Entity:
POST /ViewDefinition/$run HTTP/1.1
Content-Type: application/fhir+json
Accept: application/json
{
"resourceType": "Parameters",
"parameter": [{
"name": "viewResource",
"resource": {
"resourceType": "ViewDefinition",
"resource": "Patient",
"select": [{
"column": [
{"name": "id", "path": "invalid.path.syntax"}
]
}]
}
}]
}
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "invalid",
"diagnostics": "The ViewDefinition is invalid: column 'id' contains invalid FHIRPath expression",
"expression": ["viewResource.select[0].column[0].path"]
}
]
}
When the referenced ViewDefinition does not exist:
GET /ViewDefinition/non-existent/$run HTTP/1.1
Accept: application/json
HTTP/1.1 404 Not Found
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "not-found",
"diagnostics": "ViewDefinition with id 'non-existent' not found"
}
]
}
When required parameters are missing:
POST /ViewDefinition/$run HTTP/1.1
Content-Type: application/fhir+json
Accept: text/csv
{
"resourceType": "Parameters",
"parameter": []
}
HTTP/1.1 400 Bad Request
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "required",
"diagnostics": "Either viewReference or viewResource parameter is required when invoking at type level"
}
]
}
When an unsupported format is requested:
GET /ViewDefinition/123/$run?_format=xml HTTP/1.1
HTTP/1.1 400 Bad Request
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "not-supported",
"diagnostics": "Format 'xml' is not supported. Supported formats: json, ndjson, csv, parquet, fhir",
"expression": ["_format"]
}
]
}
When filtering by a patient that doesn’t exist:
GET /ViewDefinition/lab-results/$run?patient=Patient/non-existent HTTP/1.1
Accept: application/json
HTTP/1.1 400 Bad Request
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "not-found",
"diagnostics": "Patient with id 'non-existent' not found",
"expression": ["patient"]
}
]
}
When errors occur during data transformation:
HTTP/1.1 500 Internal Server Error
Content-Type: application/fhir+json
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "processing",
"diagnostics": "Error processing Patient/123: Required field 'birthDate' is missing",
"expression": ["resource[2]"]
}
]
}