Documente Academic
Documente Profesional
Documente Cultură
vEmployee (view)
LastName Mensa-Annan Abbas Valdez FirstName Tete Syed Rachel
Cannot be used in a CREATE VIEW definition Cannot be used in views, inline functions, derived
Cannot be referenced in a view CREATE VIEW must be alone in a single batch Can be used in a view definition if the
Indexed Views
An Indexed View is a view for which a unique clustered index has been created.
Should not be used for views whose underlying data is updated frequently using CREATE INDEX Views can be indexed
Should must be listed explicitly Columnsnot be used for views whose underlying data is updated frequently Views must be be listed explicitly SCHEMABINDING option Columns must created with the Views must be created with the SCHEMABINDING option
GO
CREATE UNIQUE CLUSTERED INDEX vwDiscountInd ON vwDiscount (ProductID)
Partitioned Views
A partitioned view joins horizontally partitioned data from a set of member tables across one or more servers, making the data appear as if from one table.
Partitioned View Details Allows the data in a large table to be split into smaller member tables. Data can be partitioned between the member tables Views can be indexed using CREATE INDEX based on ranges of for views whose underlying data is Should not be used data values.
updated frequently Partitioned Views make it easier to maintain the member Columns must be listed tables independently. explicitly Views must be created with the SCHEMABINDING option
Query a view
Generate a script for a view
Functions
Functions
A User-Defined Function is a routine that accepts parameters, performs an action, and returns the result of that action as a value.
Benefits of using User-Defined Functions Modular programming for reusable logic. Complex operations can be optimized for faster execution. Logic performed in database reduces network traffic
Perform transactions
Inline table-valued
Does not have associated return variables select_stmt is the single SELECT statement that
Multi-statement table-valued
statements that populate a TABLE return variable accumulate rows that are returned as the value
Both functions will be called once for each row in the table. If both functions require 0.1 second for each execution, the query will require: 1 second for 5 rows 10 seconds for 50 rows 1 hour for 18,000 rows
Parsing Resolving
Optimizing
Compiling Executing
Running sp_recompile
SQL Server query optimizer builds an execution plan for stored procedures on initial execution Compiled execution plans are stored in the stored procedure cache for future use
Validate all input parameters Avoid building string based SQL within procedure to reduce the risk of SQL injection Use cursors sparingly
A special type of stored procedure that executes when an attempt is made to modify data in a table
Typically used to maintain low-level data integrity and not used to return query results Two categories of DML triggers: INSTEAD OF and AFTER. INSTEAD OF trigger is also known as the BEFORE trigger
Any statement following the ROLLBACK TRANSACTION statement will still be executed
Any modifications that happen after the rollback are not rolled back
Trigger Limitations
INSTEAD OF DELETE/UPDATE triggers cannot be created on a table that has a cascading foreign key defined.
SQL Server
Map Credentials
Associate with linked server Specify local login Map to remote login
Accesses remote heterogeneous data Best if the remote data does not need to be accessed often A permanent link is not required for better security
SELECT ID, SendDate FROM OPENDATASOURCE(MSDAORA, Data Source=OraServer; User=MaryP; Password=d89q3w4u).Catalog.dbo.History SELECT ID, SendDate FROM OPENROWSET(MSDAORA, OraServer, MaryP, d89q3w4u, SELECT * FROM Catalog.dbo.History)
Use a fully qualified four-part name and the OPENQUERY function The OPENQUERY function executes the query on the specified linked server
Logon information
Lab Scenario
You are the database administrator at Adventure Works. You need to create several user-defined functions to meet the custom requirements of your organization. You will create a scalar user-defined function that removes the time part from the datetime() object and present only the date related information. Yet another user-defined function retrieves details about purchase orders of a particular vendor when supplied with a VendorID.
After this, you will create views which present a simplified version of tables with only the relevant columns from the AdventureWorks database. You will also create a stored procedure that can be used to query the view that you created. Then, you will execute an ad hoc distributed query against data in a Microsoft Office Excel spreadsheet. Finally you will create a linked server to that spreadsheet and query the linked server.
Lab Review
What is the difference between scalar and table type user-
Why would a linked server be used instead of an ad hoc How is a table type user-defined function queried? Where are stored procedure execution plans stored after
initial execution?