My FNZ Studio Version

Scripting

Latest Update: 27.03.2024 |
Recipe
2024
2023
2022.2
2022.1
11
10
9
8
7

Problem

I need to call a stored procedure. How can I do that?

Solution

1 SqlQuery Calling Stored Procedure

1.1 Procedure Definition

Let's assume that we have a stored procedure that looks as follows:

ALTER PROCEDURE [dbo].[appwayTest] 
 @Database varchar(3), 
 @Profile char(4),
 @Value3 bit = 0,
 @Value4 bit = 0
AS
BEGIN
 SET NOCOUNT ON;
 
 IF @Value4 = 1
 BEGIN
  IF NOT EXISTS (SELECT 1 FROM Thingee WHERE UserCode = @Profile)
   RETURN -1
 END
 
 SELECT DISTINCT des.CodAction, des.DesAction, CONVERT(bit, CASE ISNULL(az.UserCode, '####') WHEN '####' THEN 0 ELSE 1 END) AS Autorization
 FROM AutorizationAction des
 LEFT OUTER JOIN TableB az
 ON az.CodDatabase = des.CodDatabase AND az.CodAction = des.CodAction AND az.UserCode = @Profile
 WHERE des.CodDatabase = @Database
  AND (@Value3 = 0 AND az.CodUtente IS NOT NULL OR @Value3 = 1)
 ORDER BY des.DesAction
 RETURN 0
END

1.2 Calling the Stored Procedure

Calling this stored procedure can be done using the following Appway script.

Indexed Any $rs;
Named Any $columnValues;
Integer $i;
//
$rs := SqlQuery('userdb', 'DECLARE @return_value int; EXEC @return_value = appwayTest @Database = ?, @Profile = ?, @Value3 = ?, @Value4 = ?; SELECT \'Return Value\' = @return_value;', ['LX', '1509', 0, 1]:Any);
//
//test return value
If $rs.size() == 1 and $rs[1]['Return Value'] != null && TOINTEGER($rs[1]['Return Value']) == -1 Then 
   PRINTLN('Return value was \'-1\'');
Else
   PRINTLN('Return value was \'0\'');
   For $i := 1 Condition $i <= $rs.size() Step $i := $i + 1 Do 
      $columnValues := CAST(Named Any, $rs[$i]);
      PRINTLN($columnValues);
   End
End

Line 5 executes the sql query which calls the stored procedure. As with standard queries we may choose how to pass parameters.

Either by assembling the query string placing the parameters at the right position.

JOIN('DECLARE @return_value int; EXEC @return_value = appwayTest @Database = \'', 'LX', '\', @Profile = \'', '1509', '\', @Value3 = ', 0, ', @Value4 = ', 1, '; SELECT \'Return Value\' = @return_value;')

Or by specifying the parameter using the '?' placeholder in the query and passing a parameter collection.

'DECLARE @return_value int; EXEC @return_value = appwayTest @Database = ?, @Profile = ?, @Value3 = ?, @Value4 = ?; SELECT \'Return Value\' = @return_value;'

In the example above we pass the values for the 4 parameters using an indexed collection.

['LX', '1509', 0, 1]:Any

1.2.1 Stored Procedures Without Return Values

Calling stored procedures without return values is easy:

SqlQuery('datastoretest', '{CALL GetActivity(?)}', [1]:Any)

1.2.2 Stored Procedure With OUTPUT Parameters

Calling a stored procedure with OUTPUT parameters is just as easy:

SqlQuery('datastoretest', 'DECLARE @return_value int, @activityR nvarchar(255); EXEC @return_value = GetActivity @awid = ?, @activity = @activityR OUTPUT; SELECT @activityR as N\'activity\';', [1254906942967]:String)

2 SqlCall Calling Stored Procedure

2.1 Procedure Returning a Result Set

CREATE PROCEDURE [dbo].[GetAudit] (
    @TheTable NVARCHAR(50),
    @TheUserId NVARCHAR(255) )
AS
SELECT *
FROM dbo.DsAudit
WHERE "table" LIKE @TheTable and userId LIKE @TheUserId
Indexed Any $data := SqlCall('mssql', '{call dbo.GetAudit(?,?)}', [{'type'='in', 'value'='DynaProperties'}:Any, {'type'='in', 'value'='suter'}:Any]:Any)

2.2 Procedure Returning a Value

CREATE PROC [dbo].[TestReturn] (@InValue int)
AS
Return @Invalue

3 SqlQuery Calling Function

3.1 Calling 'Upper' Function

We are using a simple select statement which executes the function to generate a result column value.

Indexed Any $data := SqlQuery('mssql', 'select Upper(?)', ['hello']:String)
© 2026 Appway AG. All Rights Reserved