FAQ/Questions/Using-Stored-Procedures-In-Izenda-6.md
... ...
@@ -11,7 +11,7 @@ Since version 6.0, Izenda Reports can expose existing SPs (Stored Procedures) in
11 11
* IMPORTANT- Filtering the Equals select drop-down
12 12
* Building the report
13 13
14
-###Creating SPs in MSSQL 2005 to use in Izenda Reports
14
+##Creating SPs in MSSQL 2005 to use in Izenda Reports
15 15
16 16
When SPs are detected in your MSSQL database, their names will appear in the list of available DataSources in the Report Designer. Currently, Izenda AdHoc supports only input parameters, while parameters of other types are ignored. Input parameters play the role of the columns that are used in the WHERE clause of the SELECT statement.
17 17
To be suitable for AdHoc, SPs must return a standard SELECT statement. This will be treated as the result of a standard SELECT statement being performed on a table. The column names returned from the SP will be available as fields in your DataSource.
... ...
@@ -36,11 +36,11 @@ GO
36 36
37 37
***Figure 1.** Results of sample SP execution in MSSQL 2005.*
38 38
39
-###Using SPs in your application
39
+##Using SPs in your application
40 40
41 41
In order to utilize SPs, we will need to setup database mode first in Global.asax.
42 42
43
-####MVC applications
43
+###MVC applications
44 44
45 45
```csharp
46 46
void Session_Start(object sender, EventArgs e)
... ...
@@ -57,7 +57,7 @@ public class CustomAdHocConfig : DatabaseAdHocConfig
57 57
}
58 58
```
59 59
60
-####Standard method
60
+###Standard method
61 61
62 62
```csharp
63 63
[Serializable]
... ...
@@ -74,11 +74,11 @@ public class CustomAdHocConfig : DatabaseAdHocConfig
74 74
}
75 75
```
76 76
77
-###Making SPs visible in Izenda Reports
77
+##Making SPs visible in Izenda Reports
78 78
79 79
The VisibleTables property must contain the exact names of all SPs that should be available as DataSources. **Caveat:** when the VisibleTables property is empty, all tables are visible by default, while all SP are hidden. If VisibleTables contains any names, then all tables not included in VisibleTables will become hidden. To use them, they must also be included in VisibleTables to be used together with SPs. In this code example, the table "DummyTable" is added to VisibleTables along with our SP to make it visible.
80 80
81
-###Adding code for filtering the Equals(Select) drop-down.
81
+##Adding code for filtering the Equals(Select) drop-down.
82 82
83 83
Stored procedures generate sql for you and override the default queries created by Izenda Reports. Hence, the Equals(Select) drop-down will not be properly filtered because the stored procedure will not reflect the filter. In order to do this, you must populate the drop-down yourself using the "ProcessEqualsSelect" method.
84 84
... ...
@@ -114,7 +114,7 @@ public override string[] ProcessEqualsSelectList(Izenda.AdHoc.Database.Column co
114 114
}
115 115
```
116 116
117
-###Building a report using SPs as the DataSource
117
+##Building a report using SPs as the DataSource
118 118
119 119
Once you have completed the steps above and access your web application, you may notice there are DataSources that have "(SP)" at the end of name. After the SP is selected as the DataSource, you may continue to the Fields tab. Once you do, you may notice that some of fields have "(Param)" after their names.
120 120
... ...
@@ -122,7 +122,60 @@ These fields represent the input parameters of the SP, and can not be used as ou
122 122
123 123
To assign values to parameters of your SP, you need to select the field from the previous step (the one with "(Param)" at the end of name), then select the "Equals" operator, and type in the value for the parameter. In our example, we used the name "Gourmet" as our filter value. Now the report can be saved and executed. If you were following along, then you should have a datatable with the data [described above](#SampleOutput).
124 124
125
-
126
-Figure 5. Results of executing report, using "GetContact" SP as DataSource.
127 125
128
-Using Equals(...) filters operator with Stored Procedures
... ...
\ No newline at end of file
0
+##Using the Equals(...) operator with SPs
1
+
2
+Within the Izenda Report Designer are filter options that allow you to select a value from a list instead of typing the value directly. When only using table names as data sources, these work just fine. You just select the field name and AdHoc fetches all values for that field from the data source in the database, groups them, and pulls them into the dropdown control. If SPs are used as data sources, this becomes a bit more complex. This is due to a couple of factors:
3
+
4
+* **Input parameters:** These are denoted with (Param) beside their names and must satisfy the SP's requirements for input. (i.e. having two required input parameters means using two filters whenever the SP is used) **Input parameters can NOT be used with the Equals(...) operator as their possible values cannot be determined.**
5
+* **Output Fields:** These are the fields returned by the SP. Izenda detects what valid values can be selected with the given input parameters. **The list of output values can ONLY be built when all input parameters have defined values.**
6
+
7
+In other words, you can only get a list of values for the **output fields** when values for all **input parameters** have been defined.
8
+
9
+We will demonstrate how this works using the **SalesByCategory** SP in the **Northwind** database. To see this Stored Procedure in the data sources list, you need following line of code in your Global.asax:
10
+
11
+```csharp
12
+ AdHocSettings.VisibleDataSources = new string[] {"SalesByCategory"};
13
+```
14
+
15
+Also, you need to set the [[AllowEqualsSelectForStoredProcedures|/API/CodeSamples/AllowEqualsSelectForStoredProcedures]] setting to true to allow usage of the Equals(...) operators for Stored Procedures. After this, you will be able to select SalesByCategory as data source, and select Equals(...) operators for its fields. Once you have done so, you should be able to see the below fields available in your "Fields" dropdown on the ReportDesigner.
16
+
17
+![Fig. 1: SalesByCategory](http://www.izenda.com/Site/KB/Uploads/Images/dhj3phkc_174gmzpr5fj_b.png)
18
+
19
+**CategoryName** and **OrdYear** are input parameters (denoted with **(Param)**) whereas **ProductName** and **TotalPurchase** are output fields. After clicking on the "Filters" tab, we can set pre-defined values for our input parameters using a standard Equals operator, since they will be passed to our SP instead of being used in the query. After doing so, we can use the Equals(...) operators with our output fields. Below, we have used the values "Beverages" and "1997" for our input parameters.
20
+
21
+![Fig. 2: Filters Using SPs]()
22
+
23
+After this, you can get a list of predefined values for any output fields using the Equals(...) operators. Below, we have selected the **Equals (Multiple)** and **Equals (Select)**
24
+
25
+![Fig. 3: Output Fields With SPs]()
26
+
27
+Note that if you change the values of your **input parameters**, the output possibilities will also change. This is because the output fields depend on the input data. For instance, if we change the values that query our Northwinds database to "Meat/Poultry" and "1998", our output will change.
28
+
29
+![Fig. 4: Output Fields Changed]()
30
+
31
+##Using ProcessEqualsSelectList
32
+
33
+From the example above, you can see that it is required to specify all stored procedure parameters. But if you don't want to specify parameters every time, you can pre-define them by overriding the ProcessEqualsSelectList() method in your [[AdHocConfig|/Integration/Tutorials/Customizing-Izenda-Settings]] class.
34
+The example below will give the same results as if you had specified the values as "Beverages" and "1998" above.
35
+
36
+```csharp
37
+public override string[] ProcessEqualsSelectList(Izenda.AdHoc.Database.Column column)
38
+{
39
+ if (column.Name == "ProductName")
40
+ {
41
+ string sql = @"EXEC SalesByCategory 'Beverages', '1998'";
42
+ DataSet res = Izenda.AdHoc.AdHocContext.Driver.GetDataSet(Izenda.AdHoc.AdHocContext.Driver.CreateCommand(sql));
43
+ List results = new List();
44
+ foreach (DataRow row in res.Tables[0].Rows)
45
+ results.Add(row.ItemArray[0].ToString());
46
+
47
+ return results.ToArray();
48
+ }
49
+ return base.ProcessEqualsSelectList(column);
50
+}
51
+```
52
+
53
+Now you can just select **ProductName** and apply the **Equals(Select)** filter to it:
54
+
55
+![Fig. 5: Using ProcessEqualsSelectList results]()
... ...
\ No newline at end of file