logo
small logo
  • Produkty
  • Zamów
  • Wsparcie techniczne
  • About
  • Customer panel Wsparcie techniczne
    • en
    • de
    • JP
    • ZH
  • Glówna strona
  • /
  • Articles
  • /
  • How to use SQL functions in FastReport.Net
  • Jak wybrać najwyższe wartości w macierzy?

    22 kwietnia 2021

    Artykuł jest aktualny do wersji 2022.1. FastReport .NET posiada świetne narzędzie do wyświetlania danych w postaci

    read more
  • Jak połączyć się z bazą danych Sybase SQL Anywhere

    29 lutego 2020

    Baza danych Sybase SQL Anywhere ma szereg przydatnych funkcji, które sprawiają, że bardzo się wyróżnia

    read more
  • Raportowanie z PostgreSQL w aplikacji .NET 5 dla Debian 10

    25 maja 2022

    Z pewnością wielu potrzebuje rozwiązania, które będzie generować raporty dla systemów Linux, a także wspierać

    read more
  • FastCube – szybki OLAP Cube Engine i Pivot Grid

    29 lutego 2020

    Trudno sobie wyobrazić wykonanie analizy danych bez technologii OLAP (On-Line Analytical Processing). Jednak są różne

    read more
  • How to use Online Designer in ASP .NET Core

    17 stycznia 2018

    One of the novelties by FastReport .NET 2018 was the adaptation of OnlineDesigner to the

    read more

How to use SQL functions in FastReport.Net

14 sierpnia 2018

To obtain data in MS SQL, you can use sql queries, stored procedures, and stored functions. We have already discussed how to use dynamic queries and stored procedures as a report data source. In this article, we will create a table and scalar function and use them in the report.

I recall that table functions return tables, and scalar functions return single values.

Let’s create a table function in MS SQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
USE [testdb]
GO
 
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Description: Returns customers who live in the specified city
-- =============================================
 
CREATE FUNCTION [dbo].[GetCustomersFromCity]
(
 @city VARCHAR(20)
)
RETURNS TABLE
AS
RETURN
(
SELECT * FROM dbo.CUSTOMER WHERE CITY = @city
)

 The function takes one parameter - the name of the city, and returns a list of customers in this city.

 Let's add one more, now scalar function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
USE [testdb]
GO
 
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Description: Returns last added customer
-- =============================================
CREATE FUNCTION [dbo].[GetLastCustomer]
()
RETURNS VARCHAR(30)
AS
BEGIN
 DECLARE @Name VARCHAR(30)
 SELECT TOP 1 @Name = CONCAT(ind.FIRST_NAME, ' ', ind.LAST_NAME) FROM dbo.Customer cus
 JOIN dbo.Individual ind ON ind.CUST_ID = cus.CUST_ID
 ORDER BY cus.CUST_ID
 RETURN @Name
END

 This function does not accept parameters, but returns the last registered client name.

Now let's move on to the report.

First of all, create a parameter in the report. We will use it to transfer the city name to the function, to retrieve the data.

 

Add a connection to the MS SQL database:

 

Then, in the next step we are asked to select the tables, but we will use the Add SQL query ... button

 

In the next step, set the name of the new table - TableFunction. Click Next.

 

Functions, unlike stored procedures, are used as tables. That is, you need to use Select to get the data.

 

In the next step, we need to add the city query parameter. In its Expression property, select the Param parameter of the report. We go further and click Finish.

And we get a new data source:

 

Now add the dialog form and drag the Param parameter of the report to it.

Drag the fields from the TableFunction table to the Data band.

Run the report. Enter the value in the dialog form:

 

And we get a sample from the table function:

Did you forget that at the beginning of the article we created two functions? This example is even simpler, because I did not add an incoming parameter to this function. Let's create one more data source. Also, as for the first time - connection to MS SQL. And again click the Add SQL query ... button.

Calling a scalar function is slightly different from calling a table function:

We skip all the other steps.

 

Add one more page of the report and drag the data to a single field from the new data source - ScalarFunc.

Run the report. Go to the second page:

 

As you can see, using the functions to get the data is very simple, and they will help you save time while developing the report and its execution.

about product zamów
avatar
Dmitriy Fedyashov
Technical Writer
Fast Reports Team: Dmitriy Fedyashov - Technical Writer at Fast Reports
.NET FastReport SQL

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Zamów
  • Pobierz
  • Dokumentacja
  • Opinie użytkowników
  • Jak odinstalować nasze produkty
  • FAQ
  • Tutorial Video
  • Forum
  • Support SLA
  • Articles
  • Our News
  • Prasa o nas
  • Partnerzy
  • Extended licensing
  • Kontakty

© 1998-2023 by Fast Reports Inc.

  • Poufność

Trustpilot