4b5cb533f5e6ab58fba92ba800443477ae070f14
FAQ/Questions/Using-Stored-Procedures-In-Izenda-6.md
| ... | ... | @@ -16,7 +16,7 @@ Since version 6.0, Izenda Reports can expose existing SPs (Stored Procedures) in |
| 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. |
| 18 | 18 | |
| 19 | -Example of a SP using the Northwind database: |
|
| 19 | +**Example of a SP using the Northwind database:** |
|
| 20 | 20 | |
| 21 | 21 | ```sql |
| 22 | 22 | USE northwind; |
| ... | ... | @@ -178,4 +178,78 @@ public override string[] ProcessEqualsSelectList(Izenda.AdHoc.Database.Column co |
| 178 | 178 | |
| 179 | 179 | Now you can just select **ProductName** and apply the **Equals(Select)** filter to it: |
| 180 | 180 | |
| 181 | -![Fig. 5: Using ProcessEqualsSelectList results]() |
|
| ... | ... | \ No newline at end of file |
| 0 | +![Fig. 5: Using ProcessEqualsSelectList results]() |
|
| 1 | + |
|
| 2 | +##Using PreExecuteReportSet |
|
| 3 | + |
|
| 4 | +This example uses a stored procedure to populate a table before report design or execution. Every time a report is created or viewed, the below stored procedure will update the results of the **StoredProcResults** table also defined below. Let's first create the table to hold the data. |
|
| 5 | + |
|
| 6 | +```sql |
|
| 7 | +CREATE TABLE [dbo].[StoredProcResults] |
|
| 8 | + [ProductID] [int] NOT NULL, |
|
| 9 | + [OrderQuantity] [int] NOT NULL, |
|
| 10 | + [Total] [int] NOT NULL, |
|
| 11 | + [DueDate] [smalldatetime] NOT NULL |
|
| 12 | + ) ON [PRIMARY] |
|
| 13 | +``` |
|
| 14 | + |
|
| 15 | +Now we will create the SP to interface with this table. |
|
| 16 | + |
|
| 17 | +```sql |
|
| 18 | +CREATE PROCEDURE DoCustomAction ( |
|
| 19 | + @date1 as smalldatetime, |
|
| 20 | + @date2 as smalldatetime |
|
| 21 | + ) |
|
| 22 | + AS |
|
| 23 | + BEGIN |
|
| 24 | + |
|
| 25 | + insert into StoredProcResults |
|
| 26 | + select p.ProductID, p.UnitsOnOrder, (p.UnitPrice * p.UnitsOnOrder), o.RequiredDate |
|
| 27 | + from Orders o |
|
| 28 | + join [Order Details] od on o.OrderID = od.OrderID |
|
| 29 | + join Products p on od.ProductID = p.ProductID |
|
| 30 | + where o.RequiredDate >= @date1 and o.RequiredDate <= @date2 |
|
| 31 | + |
|
| 32 | + END |
|
| 33 | +``` |
|
| 34 | + |
|
| 35 | +Now we will override the [[PreExecuteReportSet|/FAQ/PreExecuteReportSet]] method to access and execute the stored procedure under certain conditions. In our example, we will specify that any time a report is viewed with the name "StoredProcExample", we will update the StoredProcResults table with orders of products whose due dates fall between 1/1/2003 and 12/31/2003. |
|
| 36 | + |
|
| 37 | +```csharp |
|
| 38 | +// Customize a report on the fly prior to execution on a per user basis |
|
| 39 | +public override void PreExecuteReportSet(Izenda.AdHoc.ReportSet reportSet) |
|
| 40 | +{ |
|
| 41 | +string currentReportName = HttpContext.Current.Request.QueryString["rn"]; |
|
| 42 | + |
|
| 43 | + if (currentReportName == "StoredProcExample") |
|
| 44 | + { |
|
| 45 | + SqlConnection myConnection = new SqlConnection(Izenda.AdHoc.AdHocSettings.SqlServerConnectionString); |
|
| 46 | + SqlCommand myCommand = new SqlCommand("DoCustomAction", myConnection); |
|
| 47 | + |
|
| 48 | + // Mark the Command as a SPROC |
|
| 49 | + myCommand.CommandType = System.Data.CommandType.StoredProcedure; |
|
| 50 | + |
|
| 51 | + // Add Parameters to SPROC |
|
| 52 | + SqlParameter parameterdate1 = new SqlParameter |
|
| 53 | + ("@date1", System.Data.SqlDbType.SmallDateTime); |
|
| 54 | + parameterdate1.Value = "1/1/2003"; |
|
| 55 | + myCommand.Parameters.Add(parameterdate1); |
|
| 56 | + |
|
| 57 | + SqlParameter parameterdate2 = new SqlParameter |
|
| 58 | + ("@date2", System.Data.SqlDbType.SmallDateTime); |
|
| 59 | + parameterdate2.Value = "12/31/2003"; |
|
| 60 | + myCommand.Parameters.Add(parameterdate2); |
|
| 61 | + |
|
| 62 | + try |
|
| 63 | + |
|
| 64 | + myConnection.Open(); |
|
| 65 | + myCommand.ExecuteNonQuery(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + finally |
|
| 69 | + { |
|
| 70 | + myConnection.Close(); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | +} |
|
| 74 | +``` |
|
| ... | ... | \ No newline at end of file |