Chapter 3. Program databases Flashcards
View
Is a reusable named query, or table expression whose definition is stored in the database. They are accessible to users who have the permission to query it. You can also modify data in underlying tables trough it.
SCHEMABINDING
Prevents structural changes to dependent tables and columns while the view exists.
If you want to change the tables, you need to delete and then to recreate the view
Indexed Views
You need to first add a clustered index for you to add a non-clustered index
User-defined functions
is a routine that accepts parameters, applies calculations, and returns either a scalar-valued or table valued result.
Its an alternative to a view with parameters, since views dont support parameters
User-defined functions - limitations and restrictions
UDF you cannot: Use error handling Modify data Use data definition language (DDL) Use temporary tables Use dynamic SQL Invoke NEWID or RAND within UDF
Scalar-User defined functions
accepts parameters and returns a single value.
It must invoke a RETURN clause to return the value
Types of User defined functions
Scalar UDF
Inline table valued functions
Multistatement table valued functions
UDF requirements - deterministic vs non-deterministic
In order to use a user-defined function in a persisted computed column or in an indexed view, the function needs to be deterministic and it needs to be defined with SCHEMABINDING
Inline table valued user defined function
is very similar in concept to a view in the sense that it is based on a single query and you interact with it like a table expression, but unlike a view it supports input parameters. Paramaterized view
Multistatement table-valued user-defined functions
A multi-statement table-valued function or MSTVF is a table-valued function that returns the result of multiple statements.
Stored Procedures
Are routines that support flow with multiple steps. They can modify data and even make changes to the database DDL, error handling, using temporary tables and more.
They cannot be embedded in queries
Stored procedures - pros
Their results are cached to save time
They are easier to change with a simple ALTER PROC than to change the application
They simplify security handling
Less traffic because it only needs to send the name of the procedure and input parameters
How to make an input parameter optional?
You define them with a default NULL
CREATE OR ALTER PROC dbo.GetOrders
@orderid AS INT = NULL,
@orderdate AS DATE = NULL,
@custid AS INT = NULL,
@empid AS INT = NULL
AS
XACT_ABORT option
determines the effect on run time errors raised by T-Sql statements.
When this is OFF (default in most cases) some errors cause an open transaction to roll back, whereas other errors leave the transaction open. Best practice is to be set ON
NOCOUNT option
Indicates how many rows were affected - they degrade query performance
EXEC procedure - format
EXEC dbo.GetOrders @orderid = 42;
dynamic SQL
building a batch of code as a charachter string usually in a variable and then telling SQL server to excecute the code that resides in that variable - it is not ideal for query performance
One way to get efficient plans is to add the RECOMPILE option
OPTION(RECOMPILE);
create User
CREATE LOGIN login1 WITH PASSWORD = ‘J345#$)thb’;
GO
CREATE USER user1 FOR LOGIN login1;
GO
Grant permission to a user
GRANT EXEC ON dbo.GetOrders TO user1;
EXECUTE AS
you can define the stored procedure with an EXECUTE AS clause, to impersonate the security context of the procedure’s execution to that of the specified entity. For example, using the option EXECUTE AS OWNER
OUTPUT or OUT
you need to provide a local variable to accept the returned value, and indicate the keyword OUTPUT or OUT again in assignment, otherwise the parameter is actually treated as an input one.
Cursors
Cursors allow you to iterate through rows of some query result one at a time