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: 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...
-- Cấp quyền CONNECT cho user mới (GRANT) GRANT CONNECT TO test; -- Cấm quyền ALTER cho schema dbo đối với user mới (DENY) DENY ALTER ON SCHEMA::dbo TO test; -- Cấm quyền CREATE SEQUENCE cho schema dbo đối với user mới (DENY) DENY CREATE SEQUENCE ON SCHEMA::dbo TO test; -- Cấm quyền DELETE cho schema dbo đối với user mới (DENY) DENY DELETE ON SCHEMA::dbo TO test; -- Cấm quyền EXECUTE cho schema dbo đối với user mới (DENY) DENY EXECUTE ON SCHEMA::dbo TO test; -- Cấm quyền INSERT cho schema dbo đối với user mới (DENY) DENY INSERT ON SCHEMA::dbo TO test; -- Cấm quyền REFERENCES cho schema dbo đối với user mới (DENY) DENY REFERENCES ON SCHEMA::dbo TO test; -- Cấm quyền TAKE OWNERSHIP cho schema dbo đối với user mới (DENY) DENY TAKE OWNERSHIP ON SCHEMA::dbo TO test; -- Cấm quyền UPDATE cho schema dbo đối với user mới (DENY) DENY UPDATE ON SCHEMA::dbo TO test; -- Cấm quyền VIEW DEFINITION cho schema dbo đối với user mới (DENY) DENY VIEW DEFINITION ON SCHEMA::dbo TO test; -- Cấm quyền VI...
Comments
Post a Comment