View - Triggers-Function
View - Triggers-Function
A user-defined function is a Transact-SQL or common language runtime (CLR) routine that accepts
parameters, performs an action, such as a complex calculation, and returns the result of that action as
a value. The return value can either be a scalar (single) value or a table.
[ ; ]
Example 1:
Example 2:
GO
select *, dbo.getTotalSalary(department_id) as totalsalary
from departments;
Lưu ý: khi câu lệnh truy vấn trả ra nhiều bản ghi
SELECT @LastName = last_name
FROM employees; -- chỉ bản ghi cuối cùng được trả ra vào biến last_name
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]
Ví dụ:
WHILE Boolean_expression
{ sql_statement | statement_block | BREAK | CONTINUE }
===================================
DECLARE BusinessCursor as CURSOR
FOR SELECT BusinessEntityID, Name
FROM Sales.Store; -- khi báo và gán giá trị cho con trỏ
@@CURSOR_ROWS: (global variable) returns the number of qualifying rows currently in the last cursor
opened on the connection.
= 0: No cursors have been opened, no rows qualified for the last opened cursor, or the last-opened cursor is closed
or deallocated.
@@FETCH_STATUS: (global variable) returns the status of the last cursor FETCH statement issued against
any cursor currently opened by the connection.
-1: The FETCH statement failed or the row was beyond the result set.
===================================
• Accept input parameters and return multiple values in the form of output parameters
to the calling program.
• Contain programming statements that perform operations in the database. These
include calling other procedures.
• Return a status value to a calling program to indicate success or failure (and the reason
for failure).
<procedure_option> ::=
[ ENCRYPTION ]
[ RECOMPILE ]
[ EXECUTE AS Clause ]
Exemple:
USE HR_test;
-- Or
EXECUTE getEmpInfos @FirstName = 'King', @LastName = 'Steven';
Delete a procedure:
DROP PROCEDURE <stored procedure name>;
-- Trigger on a CREATE, ALTER, DROP, GRANT, DENY, REVOKE or UPDATE statement (DDL Trigger)
FOR or AFTER specifies that the DML trigger fires only when all operations specified in the triggering SQL
statement have launched successfully. All referential cascade actions and constraint checks must also succeed
before this trigger fires.
You can't define AFTER triggers on views.
INSTEAD OF
An INSTEAD OF trigger is a trigger that allows you to skip an INSERT, DELETE, or UPDATE statement to a table or
a view and execute other statements defined in the trigger instead. The actual insert, delete, or update operation
does not occur at all.
SQL Server provides two virtual tables that are available specifically for triggers
called INSERTED and DELETED tables. SQL Server uses these tables to capture the data of the modified
row before and after the event occurs.
The following table shows the content of the INSERTED and DELETED tables before and after each event:
UPDATE new rows modified by the update existing rows modified by the update
-- TRIGGERS
USE HR_test;
GO
CREATE OR ALTER TRIGGER tg_test_insert
ON employees
AFTER INSERT AS
BEGIN
DECLARE @nbrinsertedrow int;
IF @@ROWCOUNT = 1
BEGIN
PRINT 'There is one inserted row';
END
ELSE
BEGIN
PRINT 'There are ' + CAST(@nbrinsertedrow as varchar(3)) + ' inserted rows.' ;
select * from inserted;
END;
END;
GO
-- test
delete from employees where employee_id >= 500;
--insert into employees(employee_id,salary) values(550, 5000);
insert into employees(employee_id,salary) values(550, 5000), (558, 2000);
select * from employees where employee_id >= 500;
-- instead of trigger
DROP TRIGGER IF EXISTS tg_insteadofinsert ;
CREATE OR ALTER TRIGGER tg_insteadofinsert
ON employees
INSTEAD OF INSERT AS
PRINT 'Trigger is fired. Row can not be inserted into employees table!'
GO
-- test
delete from employees where employee_id >= 500;
--insert into employees(employee_id,salary) values(550, 5000);
insert into employees(employee_id,salary) values(550, 5000), (558, 2000);
select * from employees where employee_id >= 500;