Stored Procedure, Triggers, Cursor
Stored Procedure
Stored procedure is a database object that is a set of SQL statements stored on the server. Stored procedures are very similar to ordinary procedures in high-level languages, they can have input and output parameters and local variables, they can perform numeric calculations and operations on symbolic data, the results of which can be assigned to variables and parameters. Stored procedures can perform standard database operations (both DDL and DML). In addition, loops and branches are possible in stored procedures, that is, they can use instructions to control the execution process.
Stored procedures improve performance, enhance programmability, and support data security features. In most DBMSs, the first time a stored procedure is run, it is compiled (parsed and a data access plan is generated) and further processing it faster.
Trigger
Trigger is a stored procedure of a special type, which the user does not call directly, but whose execution is conditioned by the action of modifying the data: adding, deleting or changing data in a given table of the relational database. Triggers are used to ensure data integrity and to implement complex business logic. The trigger is automatically started by the server and any modifications it makes to the data are treated as being performed in the transaction in which the action that caused the trigger to fire was performed. Accordingly, if an error or data integrity violation is detected, this transaction can be rolled back.
When a trigger is fired, it is specified using the keywords BEFORE (the trigger is fired before the associated event is executed) or AFTER (after the event). In case the trigger is called before the event, it can make changes to the event-modified record. In addition, triggers can be bound not to a table, but to a VIEW. In this case, they are used to implement the "updatable view" mechanism. In this case, the BEFORE and AFTER keywords only affect the sequence of trigger calls, since the actual event (delete, insert, or update) does not occur.
Cursor
Cursor is a database object that allows applications to work with records one at a time, rather than working with multiple records at once, as is done in regular SQL commands.
The procedure for working with the cursor is as follows:
- Declare cursor (DECLARE)
- Open cursor (OPEN)
- Fetch record from cursor (FETCH)
- Process entry...
- Close cursor (CLOSE)
- Delete cursor reference (DEALLOCATE). When the last reference of the cursor is deleted, SQL frees the data structures that make up the cursor.
Difference between DATETIME and TIMESTAMP data types
DATETIME is for storing an integer: YYYYMMDDHHMMSS. And this time does not depend on the time zone configured on the server. Size: 8 bytes
TIMESTAMP stores a value equal to the number of seconds elapsed since midnight, January 1, 1970, Greenwich Mean Time. When received from the database, it is displayed taking into account the time zone. Size: 4 bytes
For which numeric types is it illegal to use addition/subtraction operations?
The numeric type BIT cannot be used as operands for addition and subtraction operations.
PIVOT and UNPIVOT statements in Transact-SQL
PIVOT and UNPIVOT are non-standard relational operators that are supported by Transact-SQL.
The PIVOT operator expands a table-valued expression by converting the unique values of one column of the expression to multiple output columns, and optionally concatenates the remaining duplicate column values and displays them in the output. The UNPIVOT operator performs the opposite of PIVOT, converting the columns of a table-valued expression to column values.
Basic functions of ranking in Transact-SQL
Ranking functions are functions that return a value for each group record in the result dataset. In practice they can be used, for example, for simple list numbering, ranking or page navigation.
ROW_NUMBER is a Transact-SQL numbering function that simply returns the record number.
RANK returns the rank of each record. In this case, in contrast to ROW_NUMBER, the analysis of the values is already underway and, if the same is found, it returns the same rank with the next one skipping.
DENSE_RANK also returns the rank of each record, but unlike RANK, if the same values are found, it returns the rank without skipping the next one.
NTILE is a Transact-SQL function that divides a result set into groups based on a specific column.
INTERSECT, EXCEPT operators in Transact-SQL
The EXCEPT operator returns unique records from the left input query that are not displayed by the right input query.
The INTERSECT operator returns the unique records displayed by the left and right input queries.
Read also:
Comments
Post a Comment