Doorgaan naar hoofdcontent

Crystal Reports: On SQL Expression Fields

This is based on an article from the SAP site:

http://search.sap.com/notes?id=0001217871&boj=/sap/bc/bsp/spn/scn_bosap/notes.do?access=69765F6D6F64653D3939382669765F7361706E6F7465735F6E756D6265723D30303031323137383731

I tried to add some more examples to it.

SQL Expression Field definition

SQL Expression fields are similar to formula fields, but they are written in Structured Query Language (SQL). They are useful in optimizing report performance because the tasks they execute are performed on the database server.

You can use SQL Expression fields to perform pre-defined functions on database fields. The list of available functions depends on the type of database in use. This list is available in the Function Tree of the SQL Expression Editor dialog box.

NOTE: If Crystal Reports recognizes the function name it will turn blue in the SQL Expression Editor dialog box. Additional database functions may be available to you, depending on your database, but they will not turn blue.

SQL Expression field versus a Crystal Reports formula field

Often an SQL Expression field and a Crystal Reports formula field can accomplish the same goal. For example, a report design requirement is to display the first 3 letters of the first name of an employee.

A formula field or an SQL Expression field can fulfill this requirement. The advantage of the SQL Expression field is that the processing of the request will be done on the database server instead of in the Crystal Reports Designer. This results in a faster processing time.

SQL Expression for SQL Server:

{fn LEFT("Employee"."First Name",3)}

Crystal Reports formula:

left({Employee.First Name},3)

SQL Expression Fields and SELECT Statements

Using a SELECT statement in a SQL Expression field is not supported. Generally, an SQL Expression field cannot contain a SELECT statement because Crystal Reports can only process one SELECT statement per main report. If a SELECT statement is included in an SQL Expression field, an error message, similar to the following, may appear:


 The exact wording of the message depends upon the database in use. In this case, I used SQL Server.
 
Although SQL Expressions with SELECT statements are not supported, they can work if they only return a single value.  For example, Maximum, Minimum and Count are functions that return a single value.

It appears to be neceassary to enclose the SELECT statement with parentheses.  For example, a Microsoft SQL Server requires the SELECT statement to be enclosed in parentheses and so does Microsoft Access.

Example for the SQL Server error message when you leave the parenthesis out:

Example with the parenthesis:
 
(
SELECT DISTINCT 'Yes'
FROM orders
WHERE CustomerId = "Customers"."CustomerID"
)

Same example for Microsoft Access:

(
SELECT DISTINCT 'Yes'
FROM orders
WHERE CustomerId = `Customers`.`CustomerID`
)

In both cases, the Show SQL Query box will look something like this:


Database Functions Not Pre-Defined in Crystal Reports

Database functions that are not pre-defined in Crystal Reports can be used in SQL Expression fields.
For example, in case of the SQL Server:

LOWER("table"."field")

LOWER is not a pre-defined function in Crystal Reports and, therefore, will not turn blue in the SQL Expression Editor. However, the syntax will be accepted and you can now insert this field into your report.
Another example for the SQL Server:
 
DATEPART("WW", "Orders"."OrderDate")
 
Another example for Microsoft Access:
 
DATEPART("WW", `Orders`.`OrderDate`,2,2)

In both cases the Show SQL Query box will look something like this:

Parameters

You can not use parameters in an SQL Expression field. In case you want to use them, you have to switch to Commands.

Reacties

Populaire posts van deze blog

Excel: gegevenslabel alleen bij de laatste waarde in een grafiek

Creatief met Corona Bij het maken van een grafiek over de voortschrijdende Corona cijfers in Nederland liep ik tegen een probleem aan. Als je bij een reeks gegevens in een grafiek de gegevenslabels aan zet, krijg je die labels bij elke waarde. Ik wilde dit label alleen bij de laatste waarde tonen. Nu kun je natuurlijk alle andere gegevenslabels afzonderlijk wissen, maar dat is een hoop werk. Op zoek naar een andere manier dus. De cijfers datum aantal 1-3-2020 3 2-3-2020 8 3-3-2020 5 4-3-2020 15 5-3-2020 44 6-3-2020 46 7-3-2020 60 8-3-2020 76 9-3-2020 57 10-3-2020 61 11-3-2020 121 12-3-2020 111 13-3-2020 190 14-3-2020 155 15-3-2020 176 16-3-2020 278 17-3-2020 292 18-3-2020 250 19-3-2020 20-3-2020 21-3-2020 22-3-2020 23-3-2020 24-3-2020 25-3-2020 ...

Excel: VBA script om wachtwoord te verwijderen

Af en toe krijg ik een vraag om een wachtwoord van een Excel blad te halen. Doodsimpel met VBA. Hier een script dat ik gebruik: Sub WachtwoordCrack()     Dim a As Integer, b As Integer, c As Integer, d As Integer, _     e As Integer, f As Integer, g As Integer, h As Integer, _  I As Integer, j As Integer, k, m As Integer     Dim begin As Date, eind As Date     Dim duur As String     Dim objSheet As Worksheet     begin = TimeValue(Time)     On Error Resume Next     For Each objSheet In Application.Worksheets         For a = 65 To 66: For b = 65 To 66: For c = 65 To 66             For d = 65 To 66: For e = 65 To 66: For f = 65 To 66                 For g = 65 To 66: For h = 65 To 66: For I = 65 To 66                     For j = 65 To 66: For k = 65 To...

Vrouwen van het Friese verzet. Gevecht op vele fronten.

  Al toen mijn vorige boek, de biografie van Piet Oberman zijn voltooiing naderde, rees bij mij het idee onderzoek te doen naar de bijdrage van vrouwen aan het Friese verzet. Hoe groot was hun bijdrage eigenlijk geweest? En wie waren deze vrouwen precies? Eenmaal aan de slag werden me van alle kanten namen aangereikt. Of ik aan Wieke Bosch gedacht had? Nee, nooit van gehoord zelfs. Of Bouwina Meindertsma? Ja, die naam was ik eerder tegen-gekomen. Zelf ging ik ook op onderzoek uit en zo onstond gaande-weg een lijst met tweeëndertig namen van vrouwen die in de oorlog betrokken waren bij het verzet tegen de Duitse overheerser. Voor een deel afkomstig uit Friesland, maar voor een groot deel ook uit de rest van Nederland. Uit Groningen, Utrecht en Noord- en Zuid-Holland. En zelfs een jonge Joodse vrouw uit Duitsland. Ze moesten in de Tweede Wereldoorlog het hoofd bieden aan een wrede bezetter. Maar het boek gaat niet alleen over de oorlog.  De vrouwen moesten ook nog opboksen ...