C1-Create database programmability objects by using Transact-SQL Flashcards
What is an output parameter?
In SQL an output parameter is an stored procedure parameter that passes to the calling application or syntax.
With the OUTPUT statement: will bring values to a calling procedure.
What does “With Check” option do?
Ensures that once the data has been updated it is still visible by the view thus preventing any update that does’nt meet the criteria of the SELECT statement;
What is COLLATE command?
Collation is a set of rules that tell database engine how to compare and sort the character data in SQL Server.
Collation can be set at different levels in SQL Server. Below are the three levels:
- SQL Server level
- Database level
- Column level
How does the COLLATE SQL_Latin1_General_CP1_CI_AS breakdown?
Break down of the collation setting is as below:
- SQL – All SQL Server collations will have the prefix SQL
- Latin1_General – represents the sort rule;
- CI means case insensitive;
- AS means accent sensitive
What does “With schemabinding option do”?
prevent tables used in the views to make any modifications that can affect the view’s definition; however but does not stop the data from from being amended
What is “WITH METADATA” option?
causes the SQL Server to return the view name when describing columns in the result set and hide the base tables from the client application. However it wont stop the data from being amended.
What is SET TRANSACTION ISOLATION LEVEL?
Controls the locking and row versioning behavior of Transact-SQL statements issued by a connection to SQL Server. There are 5 types of ISOLATION LEVELS: READ UNCOMMITTED; READ COMMITTED; REPEATABLE READ; SNAPSHOT; SERIALIZABLE.
Important:
Choosing a transaction isolation level does not affect the locks acquired to protect data modifications. A transaction always gets an exclusive lock on any data it modifies, and holds that lock until the transaction completes, regardless of the isolation level set for that transaction. Additionally, an update made at the READ_COMMITTED isolation level uses update locks on the data rows selected, whereas an update made at the SNAPSHOT isolation level uses row versions to select rows to update. For read operations, transaction isolation levels primarily define the level of protection from the effects of modifications made by other transactions.
Remarks:
- Only one of the isolation level options can be set at a time, and it remains set for that connection until it is explicitly changed. All read operations performed within the transaction operate under the rules for the specified isolation level unless a table hint in the FROM clause of a statement specifies different locking or versioning behavior for a table.
What is READ COMMITTED isolation level?
Specifies that statements cannot read data that has been modified but not committed by other transactions. This prevents dirty reads. Data can be changed by other transactions between individual statements within the current transaction, resulting in nonrepeatable reads or phantom data. This option is the SQL Server default.
The behavior of READ COMMITTED depends on the setting of the READ_COMMITTED_SNAPSHOT database option:
If READ_COMMITTED_SNAPSHOT is set to OFF (the default on SQL Server), the Database Engine uses shared locks to prevent other transactions from modifying rows while the current transaction is running a read operation. The shared locks also block the statement from reading rows modified by other transactions until the other transaction is completed. The shared lock type determines when it will be released. Row locks are released before the next row is processed. Page locks are released when the next page is read, and table locks are released when the statement finishes.
If READ_COMMITTED_SNAPSHOT is set to ON (the default on Azure SQL Database), the Database Engine uses row versioning to present each statement with a transactionally consistent snapshot of the data as it existed at the start of the statement. Locks are not used to protect the data from updates by other transactions.
What is READ UNCOMMITTED isolation level?
Transactions running at the READ UNCOMMITTED level do not issue shared locks to prevent other transactions from modifying data read by the current transaction.
READ UNCOMMITTED transactions are also not blocked by exclusive locks that would prevent the current transaction from reading rows that have been modified but not committed by other transactions. When this option is set, it is possible to read uncommitted modifications, which are called dirty reads. Values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction. This option has the same effect as setting NOLOCK on all tables in all SELECT statements in a transaction. This is the least restrictive of the isolation levels.
SNAPSHOT-SET TRANSACTION ISOLATION LEVEL
Specifies that data read by any statement in a transaction will be the transactionally consistent version of the data that existed at the start of the transaction. The transaction can only recognize data modifications that were committed before the start of the transaction. Data modifications made by other transactions after the start of the current transaction are not visible to statements executing in the current transaction. The effect is as if the statements in a transaction get a snapshot of the committed data as it existed at the start of the transaction.
Except when a database is being recovered, SNAPSHOT transactions do not request locks when reading data. SNAPSHOT transactions reading data do not block other transactions from writing data. Transactions writing data do not block SNAPSHOT transactions from reading data.
During the roll-back phase of a database recovery, SNAPSHOT transactions will request a lock if an attempt is made to read data that is locked by another transaction that is being rolled back. The SNAPSHOT transaction is blocked until that transaction has been rolled back. The lock is released immediately after it has been granted.
The ALLOW_SNAPSHOT_ISOLATION database option must be set to ON before you can start a transaction that uses the SNAPSHOT isolation level. If a transaction using the SNAPSHOT isolation level accesses data in multiple databases, ALLOW_SNAPSHOT_ISOLATION must be set to ON in each database.
A transaction cannot be set to SNAPSHOT isolation level that started with another isolation level; doing so will cause the transaction to abort. If a transaction starts in the SNAPSHOT isolation level, you can change it to another isolation level and then back to SNAPSHOT. A transaction starts the first time it accesses data.
A transaction running under SNAPSHOT isolation level can view changes made by that transaction. For example, if the transaction performs an UPDATE on a table and then issues a SELECT statement against the same table, the modified data will be included in the result set.
What is REPEATABLE READ ISOLATION LEVEL?
Specifies that statements cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by the current transaction until the current transaction completes.
Shared locks are placed on all data read by each statement in the transaction and are held until the transaction completes. This prevents other transactions from modifying any rows that have been read by the current transaction.
Other transactions can insert new rows that match the search conditions of statements issued by the current transaction. If the current transaction then retries the statement it will retrieve the new rows, which results in phantom reads. Because shared locks are held to the end of a transaction instead of being released at the end of each statement, concurrency is lower than the default READ COMMITTED isolation level. Use this option only when necessary.
SET TRANSACTION ISOLATION LEVEL-SERIALIAZABLE
Specifies the following:
- Statements cannot read data that has been modified but not yet committed by other transactions.
- No other transactions can modify data that has been read by the current transaction until the current transaction completes.
- Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.
Range locks are placed in the range of key values that match the search conditions of each statement executed in a transaction. This blocks other transactions from updating or inserting any rows that would qualify for any of the statements executed by the current transaction. This means that if any of the statements in a transaction are executed a second time, they will read the same set of rows. The range locks are held until the transaction completes. This is the most restrictive of the isolation levels because it locks entire ranges of keys and holds the locks until the transaction completes. Because concurrency is lower, use this option only when necessary. This option has the same effect as setting HOLDLOCK on all tables in all SELECT statements in a transaction.
What Can Stored Procedures Do?
- Accept input parameters and return multiple values in the form of output parameters to the calling procedure or batch.
- Contain programming statements that perform operations in the database, including calling other procedures.
- Return a status value to a calling procedure or batch to indicate success or failure (and the reason for failure).
- Be much more resource efficent than other operations
CREATE PROCEDURE remarks
- Stored procedure determine what operations users are allowed to perform on the underlying tables.
- Stored procedure simplify permissions on the database objects. This means that users do not need permissions to many database objects but only to access them using stored procedures.
- Because stored procedures store their execution plans they execute more efficiently than ad-hoc SELECT statements.
- Stored procedure cannot encrypt data in the underlying tables using the WITH ENCRYPTION option. WITH ENCRIPTION The WITH ENCRYPTION option obfuscates the text of the stored procedure, not the underlying data.
What are the characteristics of parameters?
- If a procedure contains table-valued parameters, and the parameter is missing in the call, an empty table is passed in;
- Parameters can take the place only of constant expressions; they cannot be used instead of table names, column names, or the names of other database objects;
- Parameters cannot be declared if FOR REPLICATION is specified;
- You can use the user-defined table type to create table-valued parameters. Table-valued parameters can only be INPUT parameters and must be accompanied by the READONLY keyword.
What are VARYING, default, OUT|OUTPUT?
- VARYING-Specifies the result set supported as an output parameter. This parameter is dynamically constructed by the procedure and its contents may vary. Applies only to cursor parameters;
- default-A default value for a parameter. If a default value is defined for a parameter, the procedure can be executed without specifying a value for that parameter. The default value must be a constant or it can be NULL. The constant value can be in the form of a wildcard, making it possible to use the LIKE keyword when passing the parameter into the procedure.
- OUT|OUTPUT-Indicates that the parameter is an output parameter. Use OUTPUT parameters to return values to the caller of the procedure. text, ntext, and image parameters cannot be used as OUTPUT parameters, unless the procedure is a CLR procedure. An output parameter can be a cursor placeholder, unless the procedure is a CLR procedure. A table-value data type cannot be specified as an OUTPUT parameter of a procedure.
What are READONLY, RECOMPILE, FOR REPLICATION?
-
READONLY-Indicates that the parameter cannot be updated or modified within the body of the procedure. If the parameter type is a table-value type, READONLY must be specified.
- Table types can only be declared as input parameters must always be declared as READONLY
- RECOMPILE-Indicates that the Database Engine does not cache a query plan for this procedure, forcing it to be compiled each time it is executed. For more information regarding the reasons for forcing a recompile, see Recompile a Stored Procedure.
- FOR REPLICATION-Specifies that the procedure is created for replication. Consequently, it cannot be executed on the Subscriber. A procedure created with the FOR REPLICATION option is used as a procedure filter and is executed only during replication. Parameters cannot be declared if FOR REPLICATION is specified. FOR REPLICATION cannot be specified for CLR procedures. The RECOMPILE option is ignored for procedures created with FOR REPLICATION.
What is ATOMIC WITH?
Indicates atomic stored procedure execution. Changes are either committed or all of the changes rolled back by throwing an exception. The ATOMIC WITH block is required for natively compiled stored procedures. XACT_ABORT is ON by default inside an atomic block and cannot be changed. XACT_ABORT specifies whether SQL Server automatically rolls back the current transaction when a Transact-SQL statement raises a run-time error.
The following SET options are always ON in the ATOMIC block; the options cannot be changed: CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER, ARITHABORT, NOCOUNT, ANSI_NULLS, ANSI_WARNINGS.
SET options cannot be changed inside ATOMIC blocks. The SET options in the user session are not used in the scope of natively compiled stored procedures. These options are fixed at compile time.
BEGIN, ROLLBACK, and COMMIT operations cannot be used inside an atomic block.
What are NATIVE_COMPILATION, SCHEMABINDING, ENCRYPTION?
NATIVE_COMPILATION-Indicates that the procedure is natively compiled- these allows faster data access and more efficient query execution than interpreted (traditional) Transact-SQL;
SCHEMABINDING-Ensures that tables that are referenced by a procedure cannot be dropped or altered. SCHEMABINDING is required in natively compiled stored procedures.The SCHEMABINDING restrictions are the same as they are for user-defined functions.
ENCRYPTION-Indicates that SQL Server converts the original text of the CREATE PROCEDURE statement to an obfuscated format. The output of the obfuscation is not directly visible in any of the catalog views in SQL Server. Users who have no access to system tables or database files cannot retrieve the obfuscated text. However, the text is available to privileged users who can either access system tables over the DAC port or directly access database files. Also, users who can attach a debugger to the server process can retrieve the decrypted procedure from memory at runtime.
How can you return data from a stored procedure?
There are three ways of returning data from a procedure to a calling program: result sets, output parameters, and return codes.
Returning Data Using Result Sets-If you include a SELECT statement in the body of a stored procedure (but not a SELECT… INTO or INSERT… SELECT), the rows specified by the SELECT statement will be sent directly to the client.
Returning Data Using an Output Parameter-If you specify the OUTPUT keyword for a parameter in the procedure definition, the procedure can return the current value of the parameter to the calling program when the procedure exits. To save the value of the parameter in a variable that can be used in the calling program, the calling program must use the OUTPUT keyword when executing the procedure.
Returning Data Using a Return Code-A procedure can return an integer value called a return code to indicate the execution status of a procedure. You specify the return code for a procedure using the RETURN statement. As with OUTPUT parameters, you must save the return code in a variable when the procedure is executed in order to use the return code value in the calling program.
For example, the assignment variable @result of data type int is used to store the return code from the procedure my_proc
What does the CREATE FUNCTION statement does?
Creates a user-defined function in SQL Server and Azure SQL Database. 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.