The Challenge
Recently, at a client, I was challenged to create a stored procedure that would process a tabular model. This stored procedure would then be executed from a web application. The process behind it being: a user enters data into a web application, which gets written to a database. That data then needs to be immediately surfaced up into reports, with additional calculations and measures along the way. Therefore the tabular model, which does all the additional calculation and measures, needs to be processed by a user from the web application.
That’s the challenge – process a tabular model quickly, but should be processed by users on-demand.
The Solution
Part one: Method of process
There’s quite a few methods to process a tabular model: use an SSIS package, PowerShell, SQL Agent Job and others. I opted for SQL Agent Job because it was the most simple method of execution without having to reconfigure the server or introduce technologies and languages that weren’t already in use.
First things first, create a SQL Agent Job, I called mine ProcessTabular. Then create a Step. The Type should be SQL Server Analysis Services Command, input the server address and input the refresh script. I’m using SQL Server 2016, so using Tabular Model Scripting Language (TMSL) for my command. XMLA commands also work for older versions. A full list of commands for processing a tabular database can be found here.
Part two: Start the Agent Job
Now that we have a SQL Agent job, we’ll need to start that job using SQL. Luckily, there’s a system stored procedure that can start agent jobs: msdb.dbo.sp_start_job
Method for calling it is
EXEC msdb.dbo.sp_start_job 'ProcessTabular'
producing the following successful message
Part three: The Stored Procedure
sp_start_job works, but it doesn’t accommodate for providing a completion message, or informing a user that a process is in progress.
Introducing the code for the stored procedure:
CREATE PROCEDURE [dbo].[ProcessTabularDatabase]
AS
DECLARE @JobName nvarchar(50) = 'ProcessTabular',
@ResultCheck INT
IF NOT EXISTS(
SELECT 1
FROM msdb.dbo.sysjobs_view job
JOIN msdb.dbo.sysjobactivity activity ON job.job_id = activity.job_id
WHERE
activity.run_Requested_date IS NOT NULL
AND activity.stop_execution_date IS NULL
AND job.name = @JobName
)
BEGIN
PRINT 'Starting job ''' + @JobName + '''';
EXEC msdb.dbo.sp_start_job @JobName;
WHILE (1 = 1)
BEGIN
SELECT @ResultCheck = IIF(stop_execution_date IS NULL,1,0)
FROM msdb.dbo.sysjobactivity AS sja
JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id
WHERE sj.name = @JobName
IF @ResultCheck = 0 BREAK;
END
PRINT 'Successfully Processed Tabular Database'
END
ELSE
BEGIN
PRINT 'Job ''' + @JobName + ''' is already started ';
END
Conclusion
This stored procedure can be executed by the web application, enabling users to process a tabular database on-demand and get feedback as to the success of the task.
How Artificial Intelligence and Data Add Value to Businesses
Knowledge is power. And the data that you collect in the course of your business
May
Databricks Vs Synapse Spark Pools – What, When and Where?
Databricks or Synapse seems to be the question on everyone’s lips, whether its people asking
1 Comment
May
Power BI to Power AI – Part 2
This post is the second part of a blog series on the AI features of
Apr
Geospatial Sample architecture overview
The first blog ‘Part 1 – Introduction to Geospatial data’ gave an overview into geospatial
Apr
Data Lakehouses for Dummies
When we are thinking about data platforms, there are many different services and architectures that
Apr
Enable Smart Facility Management with Azure Digital Twins
Before I started writing this blog, I went to Google and searched for the keywords
Apr
Migrating On-Prem SSIS workload to Azure
Goal of this blog There can be scenario where organization wants to migrate there existing
Mar
Send B2B data with Azure Logic Apps and Enterprise Integration Pack
After creating an integration account that has partners and agreements, we are ready to create
Mar