Skip to content

Add example for selecting from view with parameters #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions cds/cdl.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,37 @@ By using a cast, annotations and other properties are inherited from the provide

### Views with Parameters

You can equip views with parameters that are passed in whenever that view is queried. Default values can be specified. Refer to these parameters in the view's query using the prefix `:`.
You can equip views with parameters that are passed in whenever that view is queried. Default values can be specified.
Refer to these parameters in the view's query using the prefix `:`.

```cds
entity SomeView ( foo: Integer, bar: Boolean )
as SELECT * from Employees where ID=:foo;
```

When selecting from a view with parameters, the parameters are passed by name.
In the following example, `UsingView` also has a parameter `bar` that is passed down to `SomeView`.

```cds
entity UsingView ( bar: Boolean )
as SELECT * from SomeView(foo: 17, bar: :bar);
```

For Node.js, there's no programmatic API yet. You need to provide a [CQN snippet](/cds/cqn#select).

In CAP Java, run a select statement against the view with named [parameter values](/java/working-with-cql/query-execution#querying-views):

::: code-group
```js [Node]
SELECT.from({ id: 'UsingView'. args: { bar: { val: true }}})
```
```Java [Java]
var params = Map.of("bar", true);
Result result = service.run(Select.from("UsingView"), params);
```
:::


[Learn more about how to expose views with parameters in **Services - Exposed Entities**.](#exposed-entities){ .learn-more}
[Learn more about views with parameters for existing HANA artifacts in **Native SAP HANA Artifacts**.](../advanced/hana){ .learn-more}

Expand Down Expand Up @@ -1817,7 +1842,7 @@ service MyOrders {
entity OrderWithParameter( foo: Integer ) as select from data.Orders where id=:foo;
}
```
A [`view with parameter`](#views-with-parameters) modeled in the previous example, can be exposed as follows:
A parametrized view like modeled in the section on [`view with parameter`](#views-with-parameters) can be exposed as follows:

```cds
service SomeService {
Expand Down