Creating SQL scalar functions is a task that you would perform when designing a database or when developing applications. SOL scalar functions are generally created when there is an identifiable benefit in encapsulating a piece of reusable logic so that it can be referenced within SQL statements in multiple applications or within database objects.
Specify a name and data type for each input parameter.
Specify the RETURNS keyword and the data type of the scalar return value.
Specify the BEGIN keyword to introduce the function-body. Note: Use of the BEGIN ATOMIC keyword is not recommended for new functions.
Specify the function body. Specify the RETURN clause and a scalar return value or variable.
Specify the END keyword.
Execute the CREATE FUNCTION (scalar) statement from a supported interface.
Results
The CREATE FUNCTION (scalar) statement should execute successfully and the scalar function should be created.
Example
Example 1
The following is an example of a compiled SQL function:
CREATE FUNCTION GetPrice (Vendor CHAR(20), Pid INT)
RETURNS DECIMAL(10,3)
LANGUAGE SQL
MODIFIES SQL
BEGIN
DECLARE price DECIMAL(10,3);
IF Vendor = 'Vendor 1'
THEN SET price = (SELECT ProdPrice FROM V1Table WHERE Id = Pid);
ELSE IF Vendor = 'Vendor 2'
THEN SET price = (SELECT Price
FROM V2Table
WHERE Pid = GetPrice.Pid);
END IF;
RETURN price;
END
This function takes in two input parameters and returns a single scalar value, conditionally based on the input parameter values. It requires the declaration and use of a local variable named price to hold the value to be returned until the function returns.
Example 2
The following example demonstrates a compiled SQL function definition containing a cursor, condition handler statement, and a REPEAT statement:
CREATE FUNCTION exit_func(a INTEGER)
SPECIFIC exit_func
LANGUAGE SQL
RETURNS INTEGER
BEGIN
DECLARE val INTEGER DEFAULT 0;
DECLARE myint INTEGER DEFAULT 0;
DECLARE cur2 CURSOR FOR
SELECT c2 FROM udfd1
WHERE c1 <= a
ORDER BY c1;
DECLARE EXIT HANDLER FOR NOT FOUND
BEGIN
SIGNAL SQLSTATE '70001'
SET MESSAGE_TEXT =
'Exit handler for not found fired';
END;
OPEN cur2;
REPEAT
FETCH cur2 INTO val;
SET myint = myint + val;
UNTIL (myint >= a)
END REPEAT;
CLOSE cur2;
RETURN myint;
END@
What to do next
After creating the scalar function you might want to invoke the function to test it.
When you create a data model in Power BI, you should consider how to properly use naming convention and what columns to include, in order to improve usability and performance. This article provides a quick list of best practices valid for both Power BI and Power Pivot. APR 5, 2018 UPDATED Marco Russo DAX POWER BI POWER PIVOT TABULAR Not all of the suggestions described can be applied to all of your data models. You should adapt these best practices to your specific scenario, looking at how to achieve the goals that are the reason for a certain pattern more than barely apply it without considering the pros and cons of each choice. Even if the article mentions Power BI, all the best practices described are valid for Power Pivot and Analysis Services Tabular models, too. The demo file you can download (at the end of the article) contains a sample Power BI file and the corresponding views defined in a SQL file for ...
IF OBJECT_ID('GenerateTriggers','P') IS NOT NULL DROP PROC GenerateTriggers GO CREATE PROC GenerateTriggers @Schemaname Sysname = 'dbo' ,@Tablename Sysname ,@GenerateScriptOnly bit = 1 ,@ForceDropAuditTable bit = 0 ,@IgnoreExistingColumnMismatch bit = 0 ,@DontAuditforUsers NVARCHAR(4000) = '' ,@DontAuditforColumns NVARCHAR(4000) = '' AS SET NOCOUNT ON /* Parameters @Schemaname - SchemaName to which the table belongs to. Default value 'dbo'. @Tablename - TableName for which the procs needs to be generated. @GenerateScriptOnly - When passed 1 , this will generate the scripts alone.. ...
This post reviews some of the tips and techniques that I covered in my webinar Performance Techniques for Power BI Data Models . To save blog space I have highlighted the first 5 tips from the presentation. There are more tips and techniques, along with expanded explanations and references in the presentation slides . The Problem Power BI is fast, and the columnar data store is forgiving of large data sets. However, if you find that your Power BI desktop solution is slowing down or that the refresh of a published BI data set takes a very long time review these tips to see if they can help you streamline your data model and reports. My example data for this talk is a customized Tasks table from Salesforce. This real life data table comes from a client and is sanitized for confidentiality. The example has over 382,000 rows which isn’t a large table for Power BI. When the data is loaded into Power BI, the stored file size on disk balloons to over 500MB. In ...
Comments
Post a Comment