Differences
This shows you the differences between two versions of the page.
customization:stored-procedures [2016-10-07 13:17] 127.0.0.1 external edit |
customization:stored-procedures [2018-12-02 22:09] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Stored procedures ====== | ||
- | Custom stored procedures can be triggered from the object model (VBA). This can be very useful if much data needs to be processed since the data never has to leave the server. | ||
- | ===== Creating a procedure ===== | ||
- | Only custom stored procedures may be executed from VBA. The system has two requirements for custom procedures: | ||
- | |||
- | * It must have " | ||
- | * It must be flagged with a '' | ||
- | |||
- | The following example creates a custom procedure that given the record ID of a coworker returns the total sum from all time reports from companies that the coworker is responsible for. In addition, an XML document is returned containing information on the ten largest customers for the coworker. If all goes well 0 is returned, otherwise 1. | ||
- | |||
- | <code sql> | ||
- | CREATE PROCEDURE [dbo].[csp_toptencustomers] | ||
- | | ||
- | | ||
- | AS | ||
- | -- FLAG_EXTERNALACCESS -- | ||
- | |||
- | DECLARE @retval int | ||
- | SET @retval = 0 | ||
- | |||
- | -- Make sure the coworker exists and is active | ||
- | |||
- | IF (NOT EXISTS(SELECT * FROM dbo.[coworker] WHERE [status] = 0 AND [inactive] = 0 AND [idcoworker] = @@responsible)) | ||
- | SET @retval = 1 | ||
- | |||
- | IF (@retval = 0) | ||
- | BEGIN | ||
- | -- Retrieve all time reports for the coworker | ||
- | | ||
- | INNER JOIN dbo.[company] c ON c.[idcompany] = t.[company] | ||
- | WHERE c.[status] = 0 AND t.[status] = 0 AND c.[responsible] = @@responsible | ||
- | |||
- | -- Then retrieve the ten biggest customers | ||
- | | ||
- | 1 AS [TAG] | ||
- | , NULL AS [PARENT] | ||
- | , c.[name] AS [topten!1!company] | ||
- | , SUM(t.[units] * t.[price]) AS [topten!1!amount] | ||
- | FROM dbo.[timereport] t | ||
- | INNER JOIN dbo.[company] c ON c.[idcompany] = t.[company] | ||
- | WHERE c.[status] = 0 AND t.[status] = 0 AND c.[responsible] = @@responsible | ||
- | GROUP BY c.[name] | ||
- | ORDER BY [topten!1!amount] | ||
- | FOR XML EXPLICIT, BINARY BASE64 | ||
- | END | ||
- | |||
- | RETURN @retval | ||
- | |||
- | GO | ||
- | </ | ||
- | |||
- | ===== Registering a procedure ===== | ||
- | In order for Lime CRM Server to become of the new custom procedure, these steps must be followed: | ||
- | |||
- | - Execute the '' | ||
- | - Launch //Component Services// from the Start menu. Go to **Computers** | **My Computer** | **COM+ Applications**. Right-click // | ||
- | - Restart Lime CRM Desktop Client. | ||
- | |||
- | The above procedures needs to be done whenever adding or removing custom procedures and also when changing the signature of existing procedures, e.g. changing anything to do with procedure parameters. | ||
- | |||
- | ===== Calling a procedure ===== | ||
- | Custom procedures are available from VBA through the Database.Procedures object. The following example lists all procedures and dumps information about their parameters: | ||
- | |||
- | <code VB> | ||
- | Public Sub DisplayProcedureInfo() | ||
- | |||
- | Dim Procedure As LDE.Procedure | ||
- | Dim Parameter As LDE.ProcedureParameter | ||
- | |||
- | For Each Procedure In Database.Procedures | ||
- | | ||
- | | ||
- | For Each Parameter In Procedure.Parameters | ||
- | | ||
- | & Parameter.DataType _ | ||
- | & IIf (Parameter.IsOutput, | ||
- | Next Parameter | ||
- | Next Procedure | ||
- | |||
- | End Sub | ||
- | </ | ||
- | |||
- | A custom procedure can be accessed either by index or by name, where the latter is the safest alternative. In the following example the procedure created above is executed: | ||
- | |||
- | <code VB> | ||
- | Public Sub ExecuteTopTen() | ||
- | |||
- | Dim Procedure As LDE.Procedure | ||
- | |||
- | Set Procedure = Database.Procedures(" | ||
- | |||
- | | ||
- | |||
- | ' | ||
- | | ||
- | |||
- | ' | ||
- | | ||
- | |||
- | ' | ||
- | | ||
- | |||
- | ' | ||
- | | ||
- | |||
- | End Sub | ||
- | </ | ||
- | |||
- | ==== Logging ==== | ||
- | The '' | ||
- | |||
- | ==== Execution mode ==== | ||
- | Procedures may be run synchronously or asynchronously depending on the boolean flag passed to '' |