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.
Nguồn: Hướng dẫn tạo Users, OU và phân quyền quản lý OU trên Windows Server 2016/2012/2008/2003 - sinhvientot.net Hướng dẫn tạo Users, OU và phân quyền quản lý OU trên Windows Server 2016/2012/2008/2003 By Mr Good - April 17, 2016 12 32662 Sau khi xây dựng thành công máy chủ Domain Controller trên Windows Server 2016 và tiến hành join các máy clients vào Domain. Ở bài viết này sinhvientot.net sẽ giới thiệu đến các bạn cách tạo user, OU và phân quyền quản lý OU(Organizational Unit) trên Windows Server 2016. I. Giới thiệu Sau khi xây dựng thành công máy chủ Domain Controller trên Windows Server 2016 và tiến hành join các máy clients vào Domain. Ở bài viết này sinhvientot.net sẽ giới thiệu đến các bạn cách tạo user, OU và phân quyền quản lý OU( Organizational Unit ) trên Windows Server 2016. Tạo các tài khoản người dùng theo OU tương ứng với phòng ban Ban giám đốc : Nguyễn...
Nguồn: Import JSON data into SQL Server (sqlshack.com) Import JSON data into SQL Server January 17, 2020 by Rajendra Gupta This article explores the process of JSON data import in the SQL Server table using T-SQL and SSIS. Introduction Java Script Object Notation is an accessible data exchange format. Many applications support JSON format data nowadays. We can use JSON data for unstructured data such as log files and NoSQL databases. SQL Server also supports JSON format data import and export for exchanging data with different data sources and applications. We can use SQL Server FOR XML PATH or FOR XML Auto clause in the SQL Server query, and it retrieves the results into the JSON format. We have explored this in the article How to import/export JSON data using SQL Server 2016. In the following image, we can see built-in functions for converting text into JSON and vice-versa: Azure Data Studio JSON format output We can also save results in JSON format without spe...
Nguồn: Bulk Insert Update in C# using Stored Procedure - Dot Net Tutorials Bulk Insert Update in C# using Stored Procedure Back to: ADO.NET Tutorial For Beginners and Professionals Bulk INSERT and UPDATE in C# and ADO.NET using Stored Procedure In this article, I am going to discuss How to Perform BULK INSERT and UPDATE in C# and ADO.NET using SQL Server Stored Procedure with Examples. Please read our previous article where we discussed ADO.NET Architecture . Here, in this article, I will explain to you how to perform the Bulk Insert and Update using the Stored Procedure with Examples, and in the next article, I am going to discuss how to Perform Batch Operations Using SqlBulkCopy and DataAdapters. Advertisements × Bulk Insert and Update using Stored Procedure and C# ADO.NET: Let us understand how to perform Bulk Insert and Update using C# and ADO.NET. So, basically what we will do is, we will check if the record does not exist in the database, then w...
Comments
Post a Comment